基线 SVN r240
SVN-Revision: r240
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
/******************************
|
||||
* 说明:图片处理相关类
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2017-07-24
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Drawing;
|
||||
|
||||
namespace Lskj.Util
|
||||
{
|
||||
public static class ImageHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>说明:将byte[]转换为Image</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-07-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="bytes">byte数组</param>
|
||||
/// <returns>转换后图片</returns>
|
||||
public static Image ReadImage(byte[] bytes)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (bytes == null) return null;
|
||||
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes))
|
||||
{
|
||||
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
|
||||
ms.Flush();
|
||||
return returnImage;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static Image ReadImage(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
FileStream fs = File.OpenRead(path); //OpenRead
|
||||
int filelength = 0;
|
||||
filelength = (int)fs.Length; //获得文件长度
|
||||
Byte[] image = new Byte[filelength]; //建立一个字节数组
|
||||
fs.Read(image, 0, filelength); //按字节流读取
|
||||
System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
|
||||
fs.Close();
|
||||
return result;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:将图片转换为byte数组</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2017-07-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="image">图片</param>
|
||||
/// <returns>转换后的byte数组</returns>
|
||||
public static byte[] ConvertImage(Image image)
|
||||
{
|
||||
try
|
||||
{
|
||||
//实例化流
|
||||
System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
|
||||
//将图片的实例保存到流中
|
||||
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Bmp);
|
||||
//image.Save(imageStream, image.RawFormat);
|
||||
//保存流的二进制数组
|
||||
byte[] imageContent = new Byte[imageStream.Length];
|
||||
imageStream.Position = 0;
|
||||
//将流泻如数组中
|
||||
imageStream.Read(imageContent, 0, (int)imageStream.Length);
|
||||
return imageStream.ToArray();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user