在 C#/WPF 应用程序中,我有一个需要保存到图像的 DataChart 对象。目前,该对象已添加到固定文档并使用以下代码正确显示在该固定文档上:
VisualBrush chartBrush = new VisualBrush(chart);
Rectangle chartRect = new Rectangle();
chartRect.Height = chartClone.Height;
chartRect.Width = chartClone.Width;
chartRect.Fill = chartBrush;
AddBlockUIElement(chartRect, textAlignment);
然而,与其将它作为一个块添加到固定文档中,我现在只需将图像保存到磁盘。我尝试过执行以下操作:
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();
}
然而,这只是给了我图表大小的纯黑色图像,我不知道为什么。
所以我的问题是,有谁知道将 DataChart 对象转换为我可以保存的 PNG 或 BMP 图像的更好方法?我已经尝试搜索从 VisualBrush 或 Rectangle 到图像,但除了上述之外,没有找到任何似乎可以满足我需要的东西。
非常感谢!