WPF RenderTargetBitmap.Render中的OutOfMemoryException [英] OutOfMemoryException in WPF RenderTargetBitmap.Render

查看:323
本文介绍了WPF RenderTargetBitmap.Render中的OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF,它会泄漏大量内存,并调用RenderTargetBitmap.Render.我在互联网上看到有关如何解决此问题的文章,但事实并非如此.我已经做了很多事情,但找不到解决方法.这是.net 3.5 sp1

我将inkCanvas对象子类化以处理多页tiff.我将每个页面另存为一个bitmapImage数组中.我还保存了一个对应的StrokesCollection数组.当需要保存多重tiff时,我调用了一个名为BindImage的方法,该方法将图像和相应的StrokesCollection加载到画布中,并将其保存为tiff的页面.

该行:

I have a WPF that leaks a lot of memory calling RenderTargetBitmap.Render. I see posts all over the internet about how this was supposed to be fixed but isn''t. I have done a lot of looking but can''t find a workaround. This is .net 3.5 sp1

I subclassed the inkCanvas object to handle multi-page tiffs. I save each page as a bitmapImage in an array. I also save a corresponding StrokesCollection array. When it is time to save the multi-tiff I call a method called BindImage that loads the image and corresponding StrokesCollection into the canvas and saves it as a page of a tiff.

the line:

rtb.Render(this);


每次页面迭代都会泄漏内存.我的代码在较小的tiff上效果很好,但是有很多页面的tiff会导致OutOfMemoryException.

我试过了:
-在.net 4下重新编译
-快速创建一个新的画布,然后对其进行调用并销毁它,然后调用carbage collection.


leaks memory at each page iteration. My code works well with smaller tiffs, but those with many pages cause OutOfMemoryException.

I have tried:
-recompiling under .net 4
-creating a new inkcanvas on the fly, then calling render on it and destroying it, then calling carbage collection.

private void BindImage()
       {
           System.Windows.Controls.Image imgTemp;
           bmpSavePages = new BitmapImage[bmpPages.Length];

           for (int i = 0; i < bmpPages.Length; i++)
           {
               this.Children.Clear();
               this.Strokes.Clear();

               imgTemp = new System.Windows.Controls.Image();

               imgTemp.Source = bmpPages[i];
               this.Children.Add(imgTemp);
               this.Strokes.Add(strWriting[i]);

               this.Children[0].UpdateLayout();
               MemoryStream ms = new MemoryStream();

               RenderTargetBitmap rtb = new RenderTargetBitmap((int)dbImageWidth, (int)dbImageHeight, dbPixelWidth, dbPixelDepth, pxfFormat);

               rtb.Render(this);

               BitmapEncoder encoder = new TiffBitmapEncoder();
               try
               {
                   encoder.Frames.Add(BitmapFrame.Create(rtb));
                   encoder.Save(ms);

                   bmpSavePages[i] = new BitmapImage();
                   bmpSavePages[i].BeginInit();
                   bmpSavePages[i].StreamSource = ms;
                   bmpSavePages[i].CacheOption = BitmapCacheOption.OnLoad;
                   bmpSavePages[i].DecodePixelHeight = (int)dbImageHeight;
                   bmpSavePages[i].EndInit();
               }
               catch (Exception e)
               {
                   System.Windows.MessageBox.Show("There was an error binding the final image.\n\n" + e.Message);
               }
               finally
               {
                   ms.Dispose();
                   ms = null;
               }
           }
       }

推荐答案

希望
Hope this[^] might help you.


这篇关于WPF RenderTargetBitmap.Render中的OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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