第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

WPF CreateBitmapSourceFromHBitmap()內(nèi)存泄漏

WPF CreateBitmapSourceFromHBitmap()內(nèi)存泄漏

12345678_0001 2019-08-02 17:33:35
WPF CreateBitmapSourceFromHBitmap()內(nèi)存泄漏我需要逐個(gè)像素地繪制圖像并將其顯示在WPF中。我試圖通過使用System.Drawing.Bitmap然后使用CreateBitmapSourceFromHBitmap()創(chuàng)建BitmapSource一個(gè)WPF圖像控件來做到這一點(diǎn)。我在某處有內(nèi)存泄漏,因?yàn)楫?dāng)CreateBitmapSourceFromBitmap()重復(fù)調(diào)用時(shí),內(nèi)存使用率會(huì)上升,并且在應(yīng)用程序結(jié)束之前不會(huì)下降。如果我不打電話CreateBitmapSourceFromBitmap(),內(nèi)存使用量沒有明顯變化。for (int i = 0; i < 100; i++){     var bmp = new System.Drawing.Bitmap(1000, 1000);     var source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(         bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,         System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());     source = null;     bmp.Dispose();     bmp = null;}我該怎么做才能釋放BitmapSource記憶?
查看完整描述

3 回答

?
翻閱古今

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊

每當(dāng)處理非托管句柄時(shí),使用“安全句柄”包裝器是個(gè)好主意:

public class SafeHBitmapHandle : SafeHandleZeroOrMinusOneIsInvalid{
    [SecurityCritical]
    public SafeHBitmapHandle(IntPtr preexistingHandle, bool ownsHandle)
        : base(ownsHandle)
    {
        SetHandle(preexistingHandle);
    }

    protected override bool ReleaseHandle()
    {
        return GdiNative.DeleteObject(handle) > 0;
    }}

只要你展示一個(gè)句柄就構(gòu)造一個(gè)(理想情況下你的API永遠(yuǎn)不會(huì)暴露IntPtr,它們總是返回安全句柄):

IntPtr hbitmap = bitmap.GetHbitmap();var handle = new SafeHBitmapHandle(hbitmap , true);

并像這樣使用它:

using (handle){
  ... Imaging.CreateBitmapSourceFromHBitmap(handle.DangerousGetHandle(), ...)}

SafeHandle基礎(chǔ)為您提供自動(dòng)一次性/終結(jié)器模式,您需要做的就是覆蓋ReleaseHandle方法。


查看完整回答
反對(duì) 回復(fù) 2019-08-02
?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

我有相同的要求和問題(內(nèi)存泄漏)。我實(shí)施了標(biāo)記為答案的相同解決方案。但是雖然解決方案有效,但它對(duì)性能造成了不可接受的打擊。在i7上運(yùn)行,我的測(cè)試應(yīng)用程序看到了穩(wěn)定的30-40%CPU,200-400MB RAM增加,垃圾收集器幾乎每毫秒運(yùn)行一次。

由于我正在進(jìn)行視頻處理,因此我需要更好的性能。我想出了以下內(nèi)容,以為我會(huì)分享。

可重用的全局對(duì)象

//set up your Bitmap and WritableBitmap as you see fitBitmap colorBitmap = new Bitmap(..);WriteableBitmap colorWB = new WriteableBitmap(..);//choose appropriate bytes as per your pixel format, I'll cheat here an just pick 4int bytesPerPixel = 4;//rectangles will be used to identify what bits changeRectangle colorBitmapRectangle = new Rectangle(0, 0, colorBitmap.Width, colorBitmap.Height);Int32Rect colorBitmapInt32Rect = new Int32Rect(0, 0, colorWB.PixelWidth, colorWB.PixelHeight);

轉(zhuǎn)換代碼

private void ConvertBitmapToWritableBitmap(){
    BitmapData data = colorBitmap.LockBits(colorBitmapRectangle, ImageLockMode.WriteOnly, colorBitmap.PixelFormat);

    colorWB.WritePixels(colorBitmapInt32Rect, data.Scan0, data.Width * data.Height * bytesPerPixel, data.Stride);

    colorBitmap.UnlockBits(data); }

實(shí)施例

//do stuff to your bitmapConvertBitmapToWritableBitmap();Image.Source = colorWB;

結(jié)果是穩(wěn)定的10-13%CPU,70-150MB RAM,垃圾收集器在6分鐘運(yùn)行中僅運(yùn)行兩次。


查看完整回答
反對(duì) 回復(fù) 2019-08-02
  • 3 回答
  • 0 關(guān)注
  • 1352 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)