102 lines
3.3 KiB
C#
102 lines
3.3 KiB
C#
/******************************
|
|
* 说明:图片处理相关类
|
|
* 创建人:龚宇超
|
|
* 创建日期: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))
|
|
using (System.Drawing.Image sourceImage = System.Drawing.Image.FromStream(ms))
|
|
{
|
|
return new Bitmap(sourceImage);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
return null;
|
|
}
|
|
/// <summary>
|
|
/// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static Image ReadImage(string path)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(path) || !File.Exists(path)) return null;
|
|
byte[] image = File.ReadAllBytes(path);
|
|
return ReadImage(image);
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|