我正在创建和应用需要添加和删除很多 UIElementCanvas。基本上是 Canvas 包含一个集合 UIElement 并根据包含的内容自动在屏幕上渲染/更新它。

为了避免有很多 UIElements 谁在屏幕上相互重叠 Canvas 然后创建一个 Image 从中 WritableBitmap)。最后我加了这个 Image 在我的当前 Canvas。通过在我的画布上只有很少的图像,我期望的性能更好。不幸的是,看来我无法完全删除 WritableBitmap, ,即使我将其设置为 null.

以下代码说明了:

//My constructor
public  WP8Graphics() 
{
    //Here my collection DataBinded with the Canvas from the Mainpage
    this.UIElements = new ObservableCollection<UIElement>();
    //The secondary Canvas
    GraphicCanvas = new Canvas();
    GraphicCanvas.Height = MainPage.CurrentCanvasHeight;
    GraphicCanvas.Width = MainPage.CurrentCanvasWidth;
}


///This method can be hit thousand times, it basically create a rectangle 
public void fillRect(int x, int y, int width, int height)
{

// some code
// CREATE THE RECTANGLE rect

   GraphicCanvas.Children.Add(rect); // My secondary Canvas



   WriteableBitmap wb1 = new WriteableBitmap(GraphicCanvas, null);
   wb1.Invalidate();

   WriteableBitmap wb2 = new WriteableBitmap((int)MainPage.CurrentCanvasWidth, (int)MainPage.CurrentCanvasHeight);

  for (int i = 0; i < wb2.Pixels.Length; i++)
  {
       wb2.Pixels[i] = wb1.Pixels[i];
  }
  wb2.Invalidate();

  wb1 = null;

  Image thumbnail = new Image();
  thumbnail.Height = MainPage.CurrentCanvasHeight;
  thumbnail.Width = MainPage.CurrentCanvasWidth;
  thumbnail.Source = wb2;



  this.UIElements.Add(thumbnail);

}

大约24岁之后 WriteableBitmap 创建一个 OutofmemoryException 出现。我阅读了许多有关此问题的文章,就我而言 WriteableBitmap 取决于我 GraphicCanvas 仍然因为仍然有参考。我无法删除我的图形画布也无法设置 myimage 源为null。

我有2个问题:

  • 是否有另一种方法可以从画布或uielement集合中创建图像?
  • 是否可以删除使该可写入的人存活的参考?

我希望能够清晰易阅读。

谢谢您的阅读。

用阿托马拉斯的建议编辑,但仍然存在同样的问题

WriteableBitmap wb1 = new WriteableBitmap(GraphicCanvas, null);这条线仍然会引发OutofmemoryException。

有帮助吗?

解决方案

您需要将原始的WritableBitmap(将保留在GraphicsCanvas上)的像素复制到新的可写入bitbitmap。

看看这个很棒的帖子 http://www.wintellect.com/blogs/jprosise/silverlight-s-big-image-problem-problem-and-whit----------------------------------------------------------------------------------------------------------------------------------------------

另外,为什么您将所有可写入的次数保留在Uielements Collection中?最新的一个不够吗?在添加最新/新的位图之前,您无法直接清除UIELEMENTS集合吗?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top