6

我有一个要向用户显示的图表,我希望能够将图表作为图像导出到磁盘,以便他们可以在应用程序之外使用它(用于演示或其他用途)。

我已经设法使用 PngBitmapEncoder 和 RenderTargetBitmap 获得基本想法,但是我从中得到的图像太小而无法真正使用,我想要获得更大的图像。

我试图简单地增加我想要渲染的控件的高度和宽度,但父级似乎可以直接控制渲染大小。从这里我尝试在内存中复制 UIElement 但渲染大小为 (0,0) 并且当我尝试使用方法来渲染它时,例如 Measure() Arrange() 和 UpdateLayout() 他们抛出异常需要解耦父级来调用这些,但是因为它在内存中并且没有渲染,所以不应该有父级?

这一切都通过 Visiblox 图表 API 完成

这是我目前所拥有的,但它不起作用:(

var width = 1600;
var height = 1200;

var newChart = new Chart { Width = width, Height = height, Title = chart.Title, XAxis = chart.XAxis, YAxis = chart.YAxis, Series = chart.Series};
Debug.WriteLine(newChart.RenderSize);
var size = new Size(width, height);
newChart.Measure(size);
newChart.Arrange(new Rect(size));
newChart.UpdateLayout();
Debug.WriteLine(newChart.RenderSize);

var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
            rtb.Render(newChart);

var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));

using (var stream = fileDialog.OpenFile())
   encoder.Save(stream);

我已经接近了一点,它现在将图形背景渲染为轴等,但不是正在绘制的实际线条。以下是更新的来源

public static void RenderChartToImage(Chart elementToRender, string filename)
{
    if (elementToRender == null)
        return;
    Debug.Write(elementToRender.RenderSize);
    var clone = new Chart();

    clone.Width = clone.Height = double.NaN;
    clone.HorizontalAlignment = HorizontalAlignment.Stretch;
    clone.VerticalAlignment = VerticalAlignment.Stretch;
    clone.Margin = new Thickness();

    clone.Title = elementToRender.Title;
    clone.XAxis = new DateTimeAxis();
    clone.YAxis = new LinearAxis() { Range = Range<double>)elementToRender.YAxis.Range};

    foreach (var series in elementToRender.Series)
    {
        var lineSeries = new LineSeries
                             {
                                 LineStroke = (series as LineSeries).LineStroke,
                                 DataSeries = series.DataSeries
                             };
        clone.Series.Add(lineSeries);
    }


    var size = new Size(1600, 1200);

    clone.Measure(size);
    clone.Arrange(new Rect(size));
    clone.UpdateLayout();

    Debug.Write(clone.RenderSize);

    var height = (int)clone.ActualHeight;
    var width = (int)clone.ActualWidth;

    var renderer = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Pbgra32);

    renderer.Render(clone);

    var pngEncoder = new PngBitmapEncoder();
    pngEncoder.Frames.Add(BitmapFrame.Create(renderer));

    using (var file = File.Create(filename))
    {
        pngEncoder.Save(file);
    }
}

所以我得到了这样的东西: 图表

虽然很大,但没有用,因为它没有任何图表。

4

3 回答 3

3

http://www.visiblox.com/blog/2011/05/printing-visiblox-charts

我错过的主要一点是

InvalidationHandler.ForceImmediateInvalidate = true;

在我在内存中渲染图表之前设置它,然后在完成后恢复它。从那里开始一帆风顺:D

于 2011-10-03T00:39:33.977 回答
2
RenderTargetBitmap DrawToImage<T>(T source, double scale) where T:FrameworkElement
{
    var clone = Clone(source);
    clone.Width = clone.Height = Double.NaN;
    clone.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
    clone.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
    clone.Margin = new Thickness();
    var size = new Size(source.ActualWidth * scale, source.ActualHeight * scale);
    clone.Measure(size);
    clone.Arrange(new Rect(size));

    var renderBitmap = new RenderTargetBitmap((int)clone.ActualWidth, (int)clone.ActualHeight, 96, 96, PixelFormats.Pbgra32);
    renderBitmap.Render(clone);
    return renderBitmap;
}

static T Clone<T>(T source) where T:UIElement
{
    if (source == null)
        return null;
    string xaml = XamlWriter.Save(source);
    var reader = new StringReader(xaml);
    var xmlReader = XmlTextReader.Create(reader, new XmlReaderSettings());
    return (T)XamlReader.Load(xmlReader);
}
于 2011-09-27T04:55:01.523 回答
1

我认为这些可能会有所帮助:

使用可下载的silverlight 解决方案将Visifire Silverlight 图表导出为图像

在 WPF 中将图表导出为图像

于 2011-09-27T04:12:01.353 回答