我需要将WPF UserControl中制作的图表保存为矢量图形文件格式。
到目前为止,我一直在尝试获取包含图表的UIElement(用户控件),并将其另存为.wmf文件。我知道WPF会以矢量图形格式呈现UserControls,因此似乎保存该文件应该非常简单。无论如何,似乎我可以握住用户控件,但看不到里面有什么,我试图将其放在画布上,但这似乎不起作用。我之所以尝试使用画布,是因为我已经设法将“儿童形状”中的.wmf文件保存在画布中。
private void InterceptChart()
{
TChartCanvas.Children.Add(new VisualHost { Visual=chart1});
}
public class VisualHost : UIElement
{
public Visual Visual { get; set; }
protected override int VisualChildrenCount
{
get { return Visual != null ? 1 : 0; }
}
protected override Visual GetVisualChild(int index)
{
return Visual;
}
}
public void Export(string filePath, Canvas MyCanvas)
{
int w = Convert.ToInt32(this.MyCanvas.Width);
int h = Convert.ToInt32(this.MyCanvas.Height);
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0, w, h);
Bitmap bmp = new Bitmap(16, 16);
Graphics gs = Graphics.FromImage(bmp);
Metafile mf = new Metafile(filePath, gs.GetHdc(), rect, MetafileFrameUnit.Pixel);
Graphics g = Graphics.FromImage(mf);
WPFPainter painter = new WPFPainter(g, MyCanvas);
painter.Draw(); //painter takes the child shapes from the canvas and draws them using System.Drawing.Graphics
g.Save();
g.Dispose();
mf.Dispose();
}
我真的希望有办法做到这一点,因为使用位图格式保存这些图表将使它们无用,并且我无力重写与图形相关的所有类,但是也许我我以错误的方式来解决这个问题,如果可以的话,我将不胜感激。干杯!