C#/WPF项目中,用到图像相关的功能时,涉及到多种图像数据类型的相互转换问题,这里做了个整理。包含的内容如下:
- Bitmap和BitmapImage相互转换。
- RenderTargetBitmap –> BitmapImage
- ImageSource –> Bitmap
- BitmapImage和byte[]相互转换。
- byte[] –> Bitmap
StackOverflow上有很多解决方案,这里选择了试过可行的方法:
- Bitmap和BitmapImage相互转换
- 谷歌上搜关键字 C# WPF Convert Bitmap BitmapImage
// Bitmap --> BitmapImagepublic static BitmapImage BitmapToBitmapImage(Bitmap bitmap){ using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); // 坑点:格式选Bmp时,不带透明度 stream.Position = 0; BitmapImage result = new BitmapImage(); result.BeginInit(); // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed." // Force the bitmap to load right now so we can dispose the stream. result.CacheOption = BitmapCacheOption.OnLoad; result.StreamSource = stream; result.EndInit(); result.Freeze(); return result; }}// BitmapImage --> Bitmappublic static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage){ // BitmapImage bitmapImage = new BitmapImage(new Uri("../Images/test.png", UriKind.Relative)); using (MemoryStream outStream = new MemoryStream()) { BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(bitmapImage)); enc.Save(outStream); Bitmap bitmap = new Bitmap(outStream); return new Bitmap(bitmap); }}
- RenderTargetBitmap –> BitmapImage
// RenderTargetBitmap --> BitmapImagepublic static BitmapImage ConvertRenderTargetBitmapToBitmapImage(RenderTargetBitmap wbm){ BitmapImage bmp = new BitmapImage(); using (MemoryStream stream = new MemoryStream()) { BmpBitmapEncoder encoder = new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(wbm)); encoder.Save(stream); bmp.BeginInit(); bmp.CacheOption = BitmapCacheOption.OnLoad; bmp.CreateOptions = BitmapCreateOptions.PreservePixelFormat; bmp.StreamSource = new MemoryStream(stream.ToArray()); //stream; bmp.EndInit(); bmp.Freeze(); } return bmp;}// RenderTargetBitmap --> BitmapImagepublic static BitmapImage RenderTargetBitmapToBitmapImage(RenderTargetBitmap rtb){ var renderTargetBitmap = rtb; var bitmapImage = new BitmapImage(); var bitmapEncoder = new PngBitmapEncoder(); bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); using (var stream = new MemoryStream()) { bitmapEncoder.Save(stream); stream.Seek(0, SeekOrigin.Begin); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = stream; bitmapImage.EndInit(); } return bitmapImage;}
- ImageSource –> Bitmap
// ImageSource --> Bitmappublic static System.Drawing.Bitmap ImageSourceToBitmap(ImageSource imageSource){ BitmapSource m = (BitmapSource)imageSource; System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m.PixelWidth, m.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); // 坑点:选Format32bppRgb将不带透明度 System.Drawing.Imaging.BitmapData data = bmp.LockBits( new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); m.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data); return bmp;}
- BitmapImage和byte[]相互转换
// BitmapImage --> byte[]public static byte[] BitmapImageToByteArray(BitmapImage bmp){ byte[] bytearray = null; try { Stream smarket = bmp.StreamSource; ; if (smarket != null && smarket.Length > 0) { //设置当前位置 smarket.Position = 0; using (BinaryReader br = new BinaryReader(smarket)) { bytearray = br.ReadBytes((int)smarket.Length); } } } catch (Exception ex) { Console.WriteLine(ex); } return bytearray;}// byte[] --> BitmapImagepublic static BitmapImage ByteArrayToBitmapImage(byte[] array){ using (var ms = new System.IO.MemoryStream(array)) { var image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; // here image.StreamSource = ms; image.EndInit(); image.Freeze(); return image; }}
- byte[] –> Bitmap
public static System.Drawing.Bitmap ConvertByteArrayToBitmap(byte[] bytes){ System.Drawing.Bitmap img = null; try { if (bytes != null && bytes.Length != 0) { MemoryStream ms = new MemoryStream(bytes); img = new System.Drawing.Bitmap(ms); } } catch (Exception ex) { Console.WriteLine(ex); } return img;}