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);
Have you tried Creating the
WriteableBitmap
with a fixed size and then calling itsRender
method to render the bitmap?Canvas
has some weird behaviors because it is an anti-pattern in Silverlight and WPF and breaks many rules about layout. The code for creating a fixed sizeWriteableBitmap
and rendering to it would look something like this:One advantage of this technique is that if you need to grab multiple images you can reuse the same
WriteableBitmap
instead of recreating it every time.