C#byte數(shù)組與Image的相互轉(zhuǎn)換實例代碼
更新時間:2017年04月23日 16:44:57 投稿:lqh
這篇文章主要介紹了C#byte數(shù)組與Image的相互轉(zhuǎn)換實例代碼的相關(guān)資料,需要的朋友可以參考下
C#byte數(shù)組與Image的相互轉(zhuǎn)換實例代碼
功能需求:
1、把一張圖片(png bmp jpeg bmp gif)轉(zhuǎn)換為byte數(shù)組存放到數(shù)據(jù)庫。
2、把從數(shù)據(jù)庫讀取的byte數(shù)組轉(zhuǎn)換為Image對象,賦值給相應(yīng)的控件顯示。
3、從圖片byte數(shù)組得到對應(yīng)圖片的格式,生成一張圖片保存到磁盤上。
這里的Image是System.Drawing.Image。
//Get an image from file Image image = Image.FromFile("D:\\test.jpg"); Bitmap bitmap = new Bitmap("D:\\test.jpg");
以下三個函數(shù)分別實現(xiàn)了上述三個需求:
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; namespace NetUtilityLib { public static class ImageHelper { /// <summary> /// Convert Image to Byte[] /// </summary> /// <param name="image"></param> /// <returns></returns> public static byte[] ImageToBytes(Image image) { ImageFormat format = image.RawFormat; using (MemoryStream ms = new MemoryStream()) { if (format.Equals(ImageFormat.Jpeg)) { image.Save(ms, ImageFormat.Jpeg); } else if (format.Equals(ImageFormat.Png)) { image.Save(ms, ImageFormat.Png); } else if (format.Equals(ImageFormat.Bmp)) { image.Save(ms, ImageFormat.Bmp); } else if (format.Equals(ImageFormat.Gif)) { image.Save(ms, ImageFormat.Gif); } else if (format.Equals(ImageFormat.Icon)) { image.Save(ms, ImageFormat.Icon); } byte[] buffer = new byte[ms.Length]; //Image.Save()會改變MemoryStream的Position,需要重新Seek到Begin ms.Seek(0, SeekOrigin.Begin); ms.Read(buffer, 0, buffer.Length); return buffer; } } /// <summary> /// Convert Byte[] to Image /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static Image BytesToImage(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); Image image = System.Drawing.Image.FromStream(ms); return image; } /// <summary> /// Convert Byte[] to a picture and Store it in file /// </summary> /// <param name="fileName"></param> /// <param name="buffer"></param> /// <returns></returns> public static string CreateImageFromBytes(string fileName, byte[] buffer) { string file = fileName; Image image = BytesToImage(buffer); ImageFormat format = image.RawFormat; if (format.Equals(ImageFormat.Jpeg)) { file += ".jpeg"; } else if (format.Equals(ImageFormat.Png)) { file += ".png"; } else if (format.Equals(ImageFormat.Bmp)) { file += ".bmp"; } else if (format.Equals(ImageFormat.Gif)) { file += ".gif"; } else if (format.Equals(ImageFormat.Icon)) { file += ".icon"; } System.IO.FileInfo info = new System.IO.FileInfo(file); System.IO.Directory.CreateDirectory(info.Directory.FullName); File.WriteAllBytes(file, buffer); return file; } } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- C#截圖程序類似騰訊QQ截圖實現(xiàn)代碼
- 詳解c# 中的DateTime
- C#中decimal保留2位有效小數(shù)的實現(xiàn)方法
- C# [ImportDll()] 知識小結(jié)
- C#使用TimeSpan時間計算的簡單實現(xiàn)
- 使用c#+IMap實現(xiàn)收取163郵件
- 詳解C#中的System.Timers.Timer定時器的使用和定時自動清理內(nèi)存應(yīng)用
- C# DateTime與時間戳轉(zhuǎn)換實例
- C#中的DateTime是值類型還是引用類型
- C#無損轉(zhuǎn)換Image為Icon的方法
- C# 調(diào)用騰訊即時通信 IM的示例
相關(guān)文章
C#中派生類調(diào)用基類構(gòu)造函數(shù)用法分析
這篇文章主要介紹了C#中派生類調(diào)用基類構(gòu)造函數(shù)用法,實例分析了派生類調(diào)用基類構(gòu)造函數(shù)的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04