I'm trying to render a canvas from my ViewModel. After passing the canvas to my VM I render it using this function:
private void GenerateTextures(object obj)
{
if (!(obj is UIElement))
return;
UIElement control = (UIElement)obj;
for (int i = 0; i <= _textureCount - 1; ++i)
{
string path = OutPath + i.ToString() + TextureFormat;
FontText = i.ToString();
using (FileStream fs = new FileStream(path,FileMode.Create))
{
control.Measure(new Size(_textureSize, _textureSize));
//control.Measure(size);
control.Arrange(new Rect(new Size(_textureSize, _textureSize)));
control.UpdateLayout();
RenderTargetBitmap bitmap = new RenderTargetBitmap(_textureSize, _textureSize, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(control);
BitmapEncoder encoder;
switch (TextureFormat)
{
case ".png":
encoder = new PngBitmapEncoder();
break;
case ".jpg":
encoder = new JpegBitmapEncoder();
break;
case ".gif":
encoder = new GifBitmapEncoder();
break;
default:
encoder = new PngBitmapEncoder();
break;
}
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Save(fs);
}
}
}
This works fine and all but the arrange() function causes my canvas to move to the top left of it's parent which visually breaks my UI. If I remove the call to arrange() than my rendered bitmap is just a small corner of the cavas.
How can I render my cavas without moving it on the UI?