In a C#/WPF application, I have a DataChart object that I need to save to an image. Currently, the object is added to a Fixed Document and correctly displays on that Fixed Document by using the following code:
VisualBrush chartBrush = new VisualBrush(chart);
Rectangle chartRect = new Rectangle();
chartRect.Height = chartClone.Height;
chartRect.Width = chartClone.Width;
chartRect.Fill = chartBrush;
AddBlockUIElement(chartRect, textAlignment);
However, rather than add it as a block to a Fixed Document, I now need to simply save the image to disk. I've tried doing the following:
RenderTargetBitmap bmp = new RenderTargetBitmap((int)chart.Width, (int)chart.Height, 96, 96, PixelFormats.Default);
bmp.Render(chart);
PngBitmapEncoder image = new PngBitmapEncoder();
image.Frames.Add(BitmapFrame.Create(bmp));
using (Stream fs = File.Create("TestImage.png"))
{
image.Save(fs);
fs.Close();
}
However, this simply gives me a solid black image in the size of my chart and I cannot figure out why.
So my question is, does anyone know of a better way to turn the DataChart object into a PNG or BMP image I can save? I've tried searching on getting from a VisualBrush or a Rectangle to an image, but haven't found anything, other than the above, that seems to do what I need.
Thanks so much!