日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Image、Byte[]、Bitmap相互转换

發布時間:2024/4/14 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Image、Byte[]、Bitmap相互转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Image、Byte[]、Bitmap相互轉換

/// <summary>
??????? /// 將圖片Image轉換成Byte[]
??????? /// </summary>
??????? /// <param name="Image">image對象</param>
??????? /// <param name="imageFormat">后綴名</param>
??????? /// <returns></returns>
??????? public static byte[] ImageToBytes(Image Image, System.Drawing.Imaging.ImageFormat imageFormat)
??????? {

???????????if (Image == null) { return null; }

???????????byte[] data = null;

???????????using (MemoryStream ms= new MemoryStream())
??????????? {

????????????????using (Bitmap Bitmap = new Bitmap(Image))
??????????????? {

????????????????????Bitmap.Save(ms, imageFormat);

???????????????????ms.Position = 0;

????????????????????data = new byte[ms.Length];

???????????????????ms.Read(data, 0, Convert.ToInt32(ms.Length));

???????????????????ms.Flush();

???????????????}

???????????}

???????????return data;

???????}

?

?

?????/// <summary>
??????????? /// byte[]轉換成Image
??????????? /// </summary>
??????????? /// <param name="byteArrayIn">二進制圖片流</param>
??????????? /// <returns>Image</returns>
??????????? public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
??????????? {
??????????????? if (byteArrayIn == null)
??????????????????? return null;
??????????????? using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
??????????????? {
??????????????????? System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
??????????????????? ms.Flush();
??????????????????? return returnImage;
??????????????? }
??????????? }

?

//Image轉換Bitmap

???1. Bitmap img = new Bitmap(imgSelect.Image);

???2. Bitmap bmp = (Bitmap)pictureBox1.Image;

?

//Bitmap轉換成Image

using System.IO;

private static System.Windows.Controls.Image Bitmap2Image(System.Drawing.Bitmap Bi)
??????? {???????????
??????????? MemoryStream ms = new MemoryStream();
??????????? Bi.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
??????????? BitmapImage bImage = new BitmapImage();
??????????? bImage.BeginInit();
??????????? bImage.StreamSource = new MemoryStream(ms.ToArray());
??????????? bImage.EndInit();
??????????? ms.Dispose();
??????????? Bi.Dispose();
??????????? System.Windows.Controls.Image i = new System.Windows.Controls.Image();
??????????? i.Source = bImage;
??????????? return i ;
??????? }

?

//byte[] 轉換 Bitmap
?public static Bitmap BytesToBitmap(byte[] Bytes)?
??????? {?
??????????? MemoryStream stream = null;?
??????????? try?
??????????? {?
??????????????? stream = new MemoryStream(Bytes);?
??????????????? return new Bitmap((Image)new Bitmap(stream));?
??????????? }?
??????????? catch (ArgumentNullException ex)?
??????????? {?
??????????????? throw ex;?
??????????? }?
??????????? catch (ArgumentException ex)?
??????????? {?
??????????????? throw ex;?
??????????? }?
??????????? finally?
??????????? {?
??????????????? stream.Close();?
??????????? }?
??????? }??
?
//Bitmap轉byte[]??
??????? public static byte[] BitmapToBytes(Bitmap Bitmap)?
??????? {?
??????????? MemoryStream ms = null;?
??????????? try?
??????????? {?
??????????????? ms = new MemoryStream();?
??????????????? Bitmap.Save(ms, Bitmap.RawFormat);?
??????????????? byte[] byteImage = new Byte[ms.Length];?
??????????????? byteImage = ms.ToArray();?
??????????????? return byteImage;?
??????????? }?
??????????? catch (ArgumentNullException ex)?
??????????? {?
??????????????? throw ex;?
??????????? }?
??????????? finally?
??????????? {?
??????????????? ms.Close();?
??????????? }?
??????? }?
??? }

轉載于:https://www.cnblogs.com/vmyspace/archive/2012/03/19/2405666.html

總結

以上是生活随笔為你收集整理的Image、Byte[]、Bitmap相互转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。