C# byte數(shù)組與Image相互轉(zhuǎn)換的方法
功能需求:
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。
以下三個(gè)函數(shù)分別實(shí)現(xiàn)了上述三個(gè)需求:
// Convert Image to Byte[]
private byte[] ImageToByte(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;
}
}
// Convert Byte[] to Image
private Image ByteToImage(byte[] buffer)
{
MemoryStream ms = new MemoryStream(buffer);
Image image = System.Drawing.Image.FromStream(ms);
return image;
}
// Convert Byte[] to a picture
private string CreateImageFromByte(string fileName, byte[] buffer)
{
string file = fileName; //文件名(不包含擴(kuò)展名)
Image image = ByteToImage(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";
}
//文件路徑目錄必須存在,否則先用Directory創(chuàng)建目錄
File.WriteAllBytes(file, buffer);
return file;
}
- C# Stream 和 byte[] 之間的轉(zhuǎn)換
- C# 字符串string和內(nèi)存流MemoryStream及比特?cái)?shù)組byte[]之間相互轉(zhuǎn)換
- C#中Byte[]和String之間轉(zhuǎn)換的方法
- C#實(shí)現(xiàn)Stream與byte[]之間的轉(zhuǎn)換實(shí)例教程
- C#中string與byte[]的轉(zhuǎn)換幫助類-.NET教程,C#語(yǔ)言
- C#中兩個(gè)byte如何相加
- C#中圖片.BYTE[]和base64string的轉(zhuǎn)換方法
- C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)
- C#如何從byte[]中直接讀取Structure實(shí)例詳解
相關(guān)文章
基于WPF實(shí)現(xiàn)帶蒙版的MessageBox消息提示框
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)帶蒙版的MessageBox消息提示框,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-08-08c# winform讀取xml文件創(chuàng)建菜單的代碼
動(dòng)態(tài)創(chuàng)建菜單使得程序靈活性大大增加,本文根據(jù)讀取xml文件中的配置菜單項(xiàng)來動(dòng)態(tài)創(chuàng)建菜單,代碼如下2013-09-09C#實(shí)現(xiàn)獲取一年中是第幾個(gè)星期的方法
這篇文章主要介紹了C#實(shí)現(xiàn)獲取一年中是第幾個(gè)星期的方法,比較實(shí)用的功能,需要的朋友可以參考下2014-08-08C#和.NET生成和使用異步流的方法實(shí)現(xiàn)
異步流可以簡(jiǎn)化異步文件的讀取、寫入和處理,本文主要介紹了C#和.NET生成和使用異步流的方法實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03C#使用Consul集群進(jìn)行服務(wù)注冊(cè)與發(fā)現(xiàn)
這篇文章主要介紹了C#使用Consul集群進(jìn)行服務(wù)注冊(cè)與發(fā)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12C#后端接收form-data,創(chuàng)建實(shí)體類教程
這篇文章主要介紹了C#后端接收form-data,創(chuàng)建實(shí)體類教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06