WPF Bitmapsource图像内存问题。 [英] WPF Bitmapsource Image memory issue.

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

问题描述

我需要从RAW字节数组中显示8k RGB图像,这是我从wpf控制中的帧抓取器以高fps速度获得的。我能够成功地将字节数组转换为BitmapSource并将其显示在WPF的图像窗口中。但是由于抓取器
在显示后生成大约5 FPS的图像我释放对象的保持但GC需要时间收集我的应用程序内存增加的内存并冻结应用程序和我的系统。如何正确处理使用以下方法创建的BitmapSource(CachedBitmap)
。请帮我处理使用后的物品。



我在改变图像时尝试了GC.Collect(),但它不起作用。



这是我的字节数组到位图转换代码,

 int Width = 7680; 
int高度= 4320;
PixelFormat PixelFormat = PixelFormats.Bgr24;
int BytesPerPixel = 3;

public BitmapSource ByteToImage(byte [] imageData)
{
var stride = BytesPerPixel * Width;
BitmapSource bitmapSource = BitmapSource.Create(Width,Height,96d,96d,PixelFormat,null,imageData,stride);
bitmapSource.Freeze();
返回bitmapSource;
}



解决方案



嗨  Tamil Prakash,



您可以参考以下链接,可能会给您一些帮助。



C#从项目资源中获取图像源会造成内存泄漏

$
WPF CreateBitmapSourceFromHBitmap()内存泄漏
$


此外,您可以尝试在应用程序启动时创建BitmapSource 或者什么,只需存储对原始BitmapSource的引用,并且不要在每次需要时创建一个新的.Br


如果我有任何误解,你可以上传一个可运行的演示(从头开始包括问题以及详细说明)。我们可以下载并调试。 
$


最好的问候,



永禄

I need to display 8k RGB image from a RAW byte array which I get from a framegrabber in wpf control at High fps speed. I am able to successfully convert and the byte array to BitmapSource and display it in the image windows in WPF. But since the grabber is generating images at around 5 FPS after display i am releasing the hold to the object but GC takes time collect the memory within which my application memory increases and freezes the application and my system. How to properly dispose the BitmapSource(CachedBitmap) created using the below method. Please help me out in disposing the object after use.

I tried GC.Collect() while image change but it doesn't work.

Here is my byte array to bitmap conversion code,

    int Width = 7680;
    int Height = 4320;
    PixelFormat PixelFormat = PixelFormats.Bgr24;
    int BytesPerPixel = 3;

    public BitmapSource ByteToImage(byte[] imageData)
    {            
        var stride = BytesPerPixel * Width;
        BitmapSource bitmapSource = BitmapSource.Create(Width, Height, 96d, 96d, PixelFormat, null, imageData, stride);
        bitmapSource.Freeze();
        return bitmapSource;
    }


解决方案


Hi  Tamil Prakash,

You can refer the following links may be give you some helps.

C# Get image source from project resources creates memory leak

WPF CreateBitmapSourceFromHBitmap() memory leak

Besides, you can try to create a BitmapSource on application startup  or something, just store a reference to the original BitmapSource and Don't create a new one every time it is needed.

If I have any misunderstanding, you can upload a runnable demo ( include the issue from scratch along with a detailed description). We can download it and debugging. 

Best Regards,

Yong Lu


这篇关于WPF Bitmapsource图像内存问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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