WPF bitmap轉bitmapimage 使用 CreateBitmapSourceFromHBitmap內存泄漏


IntPtr f = bmp.GetHbitmap();
img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(f, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

這里要是多次使用 不及時釋放內存就會爆炸~

解決辦法:

用windows下的GDI32.DLL類

使用辦法:

先引用system.runtime.serialzation類

如果解決方案配置是在Debug下面的話

把gdi32.dll放在bing\debug

然后新建一個類

  static  class Class1
    {
      [System.Runtime.InteropServices.DllImport("gdi32.dll")]
      public static extern bool DeleteObject(IntPtr o);
    }

然后每次在轉化以后 

  Class1.DeleteObject(f);

一下就好了

內存就不會爆炸了 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM