当前位置:   article > 正文

10、wpf显示图片方式一: Image控件_wpf image

wpf image

前言:整理下wpf image控件显示图片的方式,分为本地图片和内存图片(来自于网络交互中的图片)

一、Image

命名空间:System.Windows.Controls

程序集:PresentationFramework.dll

表示用于显示图像的控件,这个图像一般就是指我们本地文件存储的照片或来自于网络请求中的照片资源。

  1. [System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
  2. public class Image : System.Windows.FrameworkElement, System.Windows.Markup.IUriContext

继承 Object → DispatcherObject → DependencyObject → Visual → UIElement →

FrameworkElement → Image

Image类可用于加载以下图像类型: .bmp、.gif、.ico、.jpg、.png、wdp 和 tiff。

二、 Image赋值-本地图像资源

Image赋值方式有多种,这里分为本地图像资源赋值和内存(网络请求图像资源)

查看Image的属性可以看到,有一个Source属性,其类型为ImageSource类型

  1. //
  2. // 摘要:
  3. // 获取或设置图像的 System.Windows.Media.ImageSource。
  4. //
  5. // 返回结果:
  6. // 所绘制图像的源。 默认值为 null。
  7. public ImageSource Source { get; set; }

导航到ImageSource类,可以看到他在Media名下,不是在Drawing名下,说明是一种多媒体文件格式。因为是一个抽象类,我们不能直接使用他,需要使用他的实现类。同时他有个ToString方法,所以在XAML中经常为Image的Source属性附上一个string(本地图片地址)就可以。

2.1 XAML引用资源

  1. <Grid>
  2. <Grid.RowDefinitions>
  3. <RowDefinition Height="*"/>
  4. <RowDefinition Height="*"/>
  5. </Grid.RowDefinitions>
  6. <Image Source="{Binding ImgPath}"/>
  7. <Image Grid.Row="1" Source="pack://application:,,,/Resource/ButtonClick_16x.png"/>
  8. </Grid>
  9. //踩坑:Source这里使用的相对位置,但是如果不加"pack://application:,,,"就不能显示成功,具体的参考这篇博客:https://www.cnblogs.com/g120/p/4688101.html
  1. //code-behind
  2. public string ImgPath { get; set; }
  3. public MainWindowViewModel()
  4. {
  5. ImgPath = "pack://application:,,,/Resource/Brush_16x.png";
  6. }

2.2 ImageSource方式--用代码引用资源

前面说了,ImageSource是一个抽象类,不能直接使用他,而是使用他的继承类来代替,查阅MSDN如下:

 BitmapSource(也是个抽象类)派生自ImageSource,就用它的子类BitmapImage来实现了

  1. //code-behind
  2. image.Source = new BitmapImage(new Uri("imgPath", UriKind.Absolute));

三、Image赋值-内存(网络请求资源)

当图像来自于摄像头或者屏幕截图或者网页图片时,就需要在内存中进行操作。WPF的Image控件的Source属性的类型为ImageSource,只要是继承自ImageSource的对象都可以赋值给Source属性,前面讲过BitmapImage继承自BitmapSource,而BitmapSource又继承自ImageSource,因此为了能够将内存中的Bitmap位图显示在Image控件中,需要将Bitmap转换为ImageSource类型。

转换方式有多种,笔者这里将查询到的三种方式做下介绍。

3.1 Bitmap类定义

命名空间:System.Drawing

程序集:System.Drawing.dll

封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。 Bitmap 是用于处理由像素数据定义的图像的对象。

  1. [System.Runtime.InteropServices.ComVisible(true)]
  2. [System.Serializable]
  3. public sealed class Bitmap : System.Drawing.Image

继承 Object→MarshalByRefObject→Image→Bitmap

属性 ComVisibleAttribute SerializableAttribute

3.2 WriteableBitmap

这种方式不安全,有可能会把内存搞炸,慎用。个人理解是将bitmap中数据导入到WriteableBitmap中,然后再赋值给Image使用。

 

因为是从bitmap导数据到WriteableBitmap中,因此需要创建一个与Bitmap大小相同,像素格式兼容的WriteableBitmap。

WriteableBitmap wb = new WriteableBitmap(bitmap.Width, bitmap.Height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null);

 然后调用Bitmap的LockBits获取其内部的图像数据,通过WritePixels的方式写入WriteableBitmap,这样即完成了转换。

  1. var data = bitmap.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), bitmap.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, src.PixelFormat);
  2. wb.WritePixels(new Int32Rect(srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height), data.Scan0, data.Height * data.Stride, data.Stride, destinationX, destinationY);
  3. bitmap.UnlockBits(data);
  1. //将Bitmap 转换成WriteableBitmap
  2. public static WriteableBitmap BitmapToWriteableBitmap(System.Drawing.Bitmap src)
  3. {
  4. var wb = CreateCompatibleWriteableBitmap(src);
  5. System.Drawing.Imaging.PixelFormat format = src.PixelFormat;
  6. if (wb == null)
  7. {
  8. wb = new WriteableBitmap(src.Width, src.Height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null);
  9. format = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
  10. }
  11. BitmapCopyToWriteableBitmap(src, wb, new System.Drawing.Rectangle(0, 0, src.Width, src.Height), 0, 0, format);
  12. return wb;
  13. }
  14. //创建尺寸和格式与Bitmap兼容的WriteableBitmap
  15. public static WriteableBitmap CreateCompatibleWriteableBitmap(System.Drawing.Bitmap src)
  16. {
  17. System.Windows.Media.PixelFormat format;
  18. switch (src.PixelFormat)
  19. {
  20. case System.Drawing.Imaging.PixelFormat.Format16bppRgb555:
  21. format = System.Windows.Media.PixelFormats.Bgr555;
  22. break;
  23. case System.Drawing.Imaging.PixelFormat.Format16bppRgb565:
  24. format = System.Windows.Media.PixelFormats.Bgr565;
  25. break;
  26. case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
  27. format = System.Windows.Media.PixelFormats.Bgr24;
  28. break;
  29. case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
  30. format = System.Windows.Media.PixelFormats.Bgr32;
  31. break;
  32. case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
  33. format = System.Windows.Media.PixelFormats.Pbgra32;
  34. break;
  35. case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
  36. format = System.Windows.Media.PixelFormats.Bgra32;
  37. break;
  38. default:
  39. return null;
  40. }
  41. return new WriteableBitmap(src.Width, src.Height, 0, 0, format, null);
  42. }
  43. //将Bitmap数据写入WriteableBitmap中
  44. public static void BitmapCopyToWriteableBitmap(System.Drawing.Bitmap src, WriteableBitmap dst, System.Drawing.Rectangle srcRect, int destinationX, int destinationY, System.Drawing.Imaging.PixelFormat srcPixelFormat)
  45. {
  46. var data = src.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), src.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, srcPixelFormat);
  47. dst.WritePixels(new Int32Rect(srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height), data.Scan0, data.Height * data.Stride, data.Stride, destinationX, destinationY);
  48. src.UnlockBits(data);
  49. }

3.3 Imaging.CreateBitmapSourceFromHBitmap

  1. [DllImport("gdi32.dll", SetLastError = true)]
  2. private static extern bool DeleteObject(IntPtr hObject);
  3. /// <summary>
  4. /// 从bitmap转换成ImageSource
  5. /// </summary>
  6. /// <param name="icon"></param>
  7. /// <returns></returns>
  8. public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
  9. {
  10. //Bitmap bitmap = icon.ToBitmap();
  11. IntPtr hBitmap = bitmap.GetHbitmap();
  12. ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
  13. hBitmap,
  14. IntPtr.Zero,
  15. Int32Rect.Empty,
  16. BitmapSizeOptions.FromEmptyOptions());
  17. if (!DeleteObject(hBitmap))
  18. {
  19. throw new System.ComponentModel.Win32Exception();
  20. }
  21. //一定要卸载IntPtr,否则内存泄漏很快就没有内存了。网上有人说这个方法有时不能卸载掉,我短时间测试是能够及时卸载掉内存的。需要说明的是:DeleteObject()方法不会影响imgSource和bmp中的数据,也就是DeleteObject()执行过后,imgSource和bmp中的图像数据依然完整地存在。为了能使用DeleteObject方法,需要声明来自于gdi32的外部函数 [DllImport("gdi32")]static extern int DeleteObject(IntPtr o),参考MSDN说明https://msdn.microsoft.com/zh-tw/library/1dz311e4(v=vs.80).aspx?cs-save-lang=1&cs-
  22. return wpfBitmap;
  23. }

3.4 MemoryStream

先将Bitmap数据Save到Memory Stream中,然后再用MemoryStream将其转换为字节数组,再利用MemoryStream来赋值给BitmapImage的StreamSource属性,从而间接将Bitmap对象转换为BitmapImage对象

  1. private BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
  2. {
  3. Bitmap b = new Bitmap(bCode);
  4. MemoryStream ms = new MemoryStream();
  5. b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
  6. byte[] bytes = ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以
  7. ms.Close();
  8. //Convert it to BitmapImage
  9. BitmapImage image = new BitmapImage();
  10. image.BeginInit();
  11. image.StreamSource = new MemoryStream(bytes);
  12. image.EndInit();
  13. return image ;
  14. }

引用文章:C# wpf Bitmap转换成WriteableBitmap(BitmapSource)的方法_Alfred-N的博客-CSDN博客

使用不安全代码将 Bitmap 位图转为 WPF 的 ImageSource 以获得高性能和持续小的内存占用 - walterlvWPF 使用不安全代码快速从数组转 WriteableBitmap

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号