无法将xamChart渲染到屏幕外的位图

时间:2015-06-24 11:59:17

标签: c# wpf xaml

我正在尝试将XamChart渲染到屏幕外 我尝试使用标准WPF控件(如网格,椭圆,文本框)的相同方法,它工作正常。 但是当我尝试使用XamChart控件时,它显示的是空图表 我的要求是"在后面的代码中绘制图表并将其保存为图像以便在报告中进一步使用。" 这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infragistics.Windows.Chart;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Collections;
using System.IO;
using System.Windows;
namespace XamChartrendering
{
    class Program
    {
        private const string imageFileName = @"d:\chart.png";
        [STAThread]
        static void Main(string[] args)
        {
            Canonical chart = new Canonical();
            chart.runIt();
        }
    }
    public class Canonical
    {
        private const string imageFileName = "imageCan.png";
        public void runIt()
        {
            XamChart chart = getFixedChart(800, 500);
            BitmapSource bmp = getBitmap(chart, 800, 500);
            writeToFile(bmp);
        }
        private XamChart getFixedChart(int width, int height)
        {
            XamChart chart = new XamChart();
            chart.BeginInit();
            chart.Width = width;
            chart.Height = height;
            chart.View3D = false;
            getDataSeries(chart);
            chart.EndInit();
            chart.Measure(new Size(width, height));
            chart.Arrange(new Rect(0, 0, width, height));
            chart.DataSourceRefresh();
            chart.Refresh();
            chart.UpdateLayout();
            return chart;
        }
        private BitmapSource getBitmap(Visual testChart, int width, int height)
        {
            RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
            bmp.Render(testChart);
            return bmp;
        }
        private void getDataSeries(XamChart chart)
        {
            var series = new Series();
            series.DataSource = new ArrayList { new { number = 1, name = "Peter" }, new { number = 2, name = "Susan" }, new { number = 4.6, name = "Edmund" }, new { number = 14.6, name = "Lucy" } };
            series.DataMapping = "Value=number; Label=name";
            series.Label = "Test Data";
            chart.Series.Add(series);
        }
        private void writeToFile(BitmapSource bmp)
        {
            File.Delete(imageFileName);
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));
            FileStream stream = new FileStream(imageFileName, FileMode.Create);
            encoder.Save(stream);
            stream.Flush();
            stream.Close();
        }
    }
}

它给出了空图表。请帮忙

1 个答案:

答案 0 :(得分:0)

在WPF应用程序中,当在后面的代码中创建控件并且实际在UI中呈现而不是时,它们没有大小。为了使控件能够自行调整大小,您需要在其上调用UIElement.MeasureUIElement.Arrange方法,就像Framework在渲染之前所做的那样。

尝试这样的事情:

chart.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
chart.Arrange(new Rect(0, 0, yourDesiredWidth, yourDesiredHeight));