无法在屏幕外将xamChart渲染为位图

本文关键字:位图 xamChart 屏幕 | 更新日期: 2023-09-27 17:59:18

我正在尝试在屏幕外渲染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();
        }
    }
}

它给出了空图表。请帮助

无法在屏幕外将xamChart渲染为位图

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

试试这样的东西:

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