I am looking to render a DrawingVisual
(visual in the example) to a bitmap using RenderTargetBitmap
with the view to set this bitmap as the background to a Canvas
as below:
var bmp = new RenderTargetBitmap(2000, 50, 120, 96, PixelFormats.Indexed2);
bmp.Render(visual);
var brush = new ImageBrush(bmp) { Stretch = Stretch.Fill };
Canvas.Background = brush;
When using PixelFormats.Default
as the last argument to RenderTargetBitmap
, the image renders as expected. However, when I choose PixelFormats.Indexed2
(or any of the PixelFormats.IndexedX
), my code seems to exit the method without an exception, the bmp.Render
line is never called and hence the image is not displayed on the Canvas
.
How to use the IndexedX
pixel formats with RenderTargetBitmap
? Or are there other ways to reduce the memory footprint of the image? It only uses three colors, so using a palette rather than 32bit RGB seemed the way to go.