0

I'm trying to make an Image out of one of my Canvas (myPrintingCanvas). But the Width of the image is getting atleast twice wider than the Canvas, and the extra space that is created is back. If I try on another Canavas (LayoutRoot), it works as intended.

My observation is that on myPrintingCanvas the ActualWidth is always 0. LayoutRoot has a correct ActualWidth. Not sure if it has anything to do with the extra padding, and I have failed on getting the ActualWidth for myPrintingCanvas (using UpdateLayout and Measure).

Code:

//Code to render the content of myPrintingCanvas
...

//Make the WriteableBitmap 
WriteableBitmap myWriteableBitmap = new WriteableBitmap(myPrintingCanvas, null);
4

1 に答える 1

0

固定サイズでを作成してからWriteableBitmap、そのメソッドを呼び出しRenderてビットマップをレンダリングしようとしましたか? Silverlight と WPFのアンチパターンCanvasであり、レイアウトに関する多くの規則に違反しているため、いくつかの奇妙な動作があります。固定サイズを作成してレンダリングするためのコードは、次のようになります。WriteableBitmap

        int width = 640;
        int height = 480;
        WriteableBitmap bmp = new WriteableBitmap(width, height);
        bmp.Render(myPrintingCanvas, null);
        bmp.Invalidate();

この手法の利点の 1 つは、複数の画像を取得する必要がある場合に、WriteableBitmap毎回画像を再作成するのではなく、同じ画像を再利用できることです。

于 2011-11-16T15:43:21.797 に答える