using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.IO; namespace Stone.Common { public class MyImage { /// /// 图片转换为byte[] /// /// /// /// public static byte[] ImageToBuffer(Image Image, System.Drawing.Imaging.ImageFormat imageFormat) { if (Image == null) { return null; } byte[] data = null; using (MemoryStream oMemoryStream = new MemoryStream()) { using (Bitmap oBitmap = new Bitmap(Image)) { oBitmap.Save(oMemoryStream, imageFormat); oMemoryStream.Position = 0; data = new byte[oMemoryStream.Length]; oMemoryStream.Read(data, 0, Convert.ToInt32(oMemoryStream.Length)); oMemoryStream.Flush(); } } return data; } /// /// byte[]转换为图片 /// /// /// public static Image BufferToImage(byte[] Buffer) { if (Buffer == null || Buffer.Length == 0) { return null; } byte[] data = null; Image oImage = null; Bitmap oBitmap = null; data = (byte[])Buffer.Clone(); try { MemoryStream oMemoryStream = new MemoryStream(Buffer); oMemoryStream.Position = 0; oImage = System.Drawing.Image.FromStream(oMemoryStream); oBitmap = new Bitmap(oImage); } catch { throw; } return oBitmap; } /// /// 从文件读取图片到Image中(以非独占的方式) /// /// /// public static Image ReadImageFile(string FileName) { FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read); Image image = Image.FromStream(fs); fs.Close(); return image; } } }