WPF与GDI内存的增长 [英] WPF with GDI Memory growth

查看:109
本文介绍了WPF与GDI内存的增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用WPF(C#)编写一个绘制某些类型的分形的应用程序.数据集是3d点,需要表示为单个像素.用户可以单击并拖动以旋转图.点的数量有所不同,但可能非常大(五百万个图或更多).目前,我正在使用Graphics对象绘制到System.Drawing.Bitmap上,然后将WPF图像的Source属性设置为该位图.我遇到的问题是,当我反复重画图像时,内存消耗稳步上升.当用户拖动鼠标时,将重复调用Plot()方法.

I'm writing an app in WPF (C#) that plots certain types of fractals. The data sets are 3d points and need to represented as single pixels. The user can click and drag to rotate the plots The amount of points varies but can be quite large (five million plots or more). Currently I am drawing onto a System.Drawing.Bitmap using a Graphics object then setting a WPF Image's Source property to that bitmap. The problem I have is that when i repeatedly redraw the image memory consumption steadily climbs. The Plot() method is called repeated when the user is dragging the mouse.

public void Plot()
{
    Graphics g = Graphics.FromImage(bmpCanvas);

    //Draw on the graphics object
    //...
    //...
    //...

    g.Dispose();
    Canvas.Source = loadBitmap(bmpCanvas);
    GC.Collect();
}

private BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
    return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
        System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}

具体来说,我想知道如何防止内存不受控制地攀升.

Specifically I want to know what I can do to prevent the memory from climbing uncontrollably.

总的来说,我首先想知道这是否是一个好方法.我想在wpf应用程序中显示自定义绘制的图像,并能够快速执行(即体面的帧速率)

In general I want to know if this a good approach in the first place. I want to display custom drawn images inside a wpf app and be able to do so quickly (IE. a decent frame rate)

在WPF框架中是否有更本地化的方法?我应该放弃WPF并返回到可以更精确地控制事物的Windows窗体吗?

Is there a more native way to do this inside the WPF framework? Should I ditch WPF and go back to windows forms where I can control things more precisely?

推荐答案

已解决.通过使用DeleteObject清理hbitmap实例,解决了该问题.有关更多详细信息,请参考 WPF CreateBitmapSourceFromHBitmap()内存泄漏.

Solved. The problem was solved by using DeleteObject to clean up the instance of the hbitmap. Please refer to WPF CreateBitmapSourceFromHBitmap() memory leak for more details.

这篇关于WPF与GDI内存的增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆