質問

私は多くを追加して削除する必要がある私は作成とアプリケーションです UIElementCanvas。基本的にa Canvas のコレクションが含まれています UIElement 内容に応じて、画面上で自動的にレンダリング/更新します。

大量のものを持つことを避けるため UIElements 画面上でお互いに重複している人 Canvas 次に、aを作成します 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 私に依存します グラフィックカンバス そして、まだそれを参照しているからです。グラフィックキャンバスを削除したり、設定したりすることはできません マイメージ nullへのソース。

私には2つの質問があります:

  • CanvasまたはUieLementsのコレクションから画像を作成する別の方法はありますか?
  • そのWriteableBitMapを生かし続ける参照を削除することは可能ですか?

私は十分に明確で読みやすくなりたいと思っています。

読んでくれてありがとう。

Atomarasの提案で編集されたが、それでも同じ問題

WriteableBitmap wb1 = new WriteableBitmap(GraphicCanvas, null);この行はまだoffmemoryexceptionを投げます。

役に立ちましたか?

解決

元のWriteableBitMap(GraphicsCanvasに保持される)のピクセルを新しいwriteableBitMapにコピーする必要があります。

この素晴らしい投稿を見てください http://www.wintellect.com/blogs/jprosise/silverlight-s-big-image-problem-and-what-you-can-do-about-it

また、なぜuielementsコレクションのすべてのwriteablebitmapsを保持するのですか?最新のものは十分ではないでしょうか?最新/新しいビットマップを追加する直前に、UieLementsコレクションをクリアできませんか?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top