/****************************** * 说明:图片处理相关类 * 创建人:龚宇超 * 创建日期: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 { /// /// 说明:将byte[]转换为Image /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// byte数组 /// 转换后图片 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; } /// /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件 /// /// /// 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; } /// /// 说明:将图片转换为byte数组 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// 图片 /// 转换后的byte数组 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; } } }