C#byte數(shù)組與Image的相互轉(zhuǎn)換實(shí)例代碼
C#byte數(shù)組與Image的相互轉(zhuǎn)換實(shí)例代碼
功能需求:
1、把一張圖片(png bmp jpeg bmp gif)轉(zhuǎn)換為byte數(shù)組存放到數(shù)據(jù)庫(kù)。
2、把從數(shù)據(jù)庫(kù)讀取的byte數(shù)組轉(zhuǎn)換為Image對(duì)象,賦值給相應(yīng)的控件顯示。
3、從圖片byte數(shù)組得到對(duì)應(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");
以下三個(gè)函數(shù)分別實(shí)現(xiàn)了上述三個(gè)需求:
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()會(huì)改變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;
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- C#截圖程序類似騰訊QQ截圖實(shí)現(xiàn)代碼
- 詳解c# 中的DateTime
- C#中decimal保留2位有效小數(shù)的實(shí)現(xiàn)方法
- C# [ImportDll()] 知識(shí)小結(jié)
- C#使用TimeSpan時(shí)間計(jì)算的簡(jiǎn)單實(shí)現(xiàn)
- 使用c#+IMap實(shí)現(xiàn)收取163郵件
- 詳解C#中的System.Timers.Timer定時(shí)器的使用和定時(shí)自動(dòng)清理內(nèi)存應(yīng)用
- C# DateTime與時(shí)間戳轉(zhuǎn)換實(shí)例
- C#中的DateTime是值類型還是引用類型
- C#無損轉(zhuǎn)換Image為Icon的方法
- C# 調(diào)用騰訊即時(shí)通信 IM的示例
相關(guān)文章
C#中派生類調(diào)用基類構(gòu)造函數(shù)用法分析
這篇文章主要介紹了C#中派生類調(diào)用基類構(gòu)造函數(shù)用法,實(shí)例分析了派生類調(diào)用基類構(gòu)造函數(shù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
Unity給物體添加多個(gè)Tag的實(shí)現(xiàn)
這篇文章主要介紹了Unity給物體添加多個(gè)Tag的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
分享WCF聊天程序--WCFChat實(shí)現(xiàn)代碼
無意中在一個(gè)國(guó)外的站點(diǎn)下到了一個(gè)利用WCF實(shí)現(xiàn)聊天的程序,作者是:Nikola Paljetak。研究了一下,自己做了測(cè)試和部分修改,感覺還不錯(cuò),分享給大家2015-11-11
C#獲取ListView鼠標(biāo)下的Item實(shí)例
下面小編就為大家?guī)硪黄狢#獲取ListView鼠標(biāo)下的Item實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01

