用c#做图像处理的时候需要用到System.Drawing.Bitmap。在WPF中显示图像的Image控件接受的数据源是ImageSource,因此使用System.Drawing.Bitmap进行图像处理之后要把System.Drawing.Bitmap转换成ImageSource,转换方法如下:
System.Drawing.Bitmap m_Bitmap = new System.Drawing.Bitmap("c:\temp\test.jpg", false);
IntPtr ip = m_Bitmap.GetHbitmap();
BitmapSource bitmapSource 
= System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
    ip, IntPtr.Zero, Int32Rect.Empty,
    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ip);
imageLarge.Source 
= bitmapSource;
其中DeleteObject的声明如下:
[DllImport("gdi32")]
static extern int DeleteObject(IntPtr o);

使用过System.Drawing.Bitmap后一定要用DeleteObject释放掉对象,不然内存不释放,很快系统内存就消耗光了。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
  • 2022-12-23
  • 2022-02-04
  • 2021-10-24
  • 2021-06-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-04-21
  • 2021-08-05
  • 2022-01-09
  • 2021-11-28
相关资源
相似解决方案