Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#使用LiveCharts从图表到图像_C#_Wpf_Image_Livecharts - Fatal编程技术网

c#使用LiveCharts从图表到图像

c#使用LiveCharts从图表到图像,c#,wpf,image,livecharts,C#,Wpf,Image,Livecharts,我有个小问题。我想将我的图表导出为图像。我知道这在beto rodriguez()给出的代码中是可能的 但是我有一个问题,我不能通过在屏幕上显示来获得我所能得到的。见上图。在右下角,我将图像保存为png格式。在图像的左边,我有一张图表,上面显示了我想要的所有参数 您知道在保存的图像中是否可能有相同的图表(在左侧)? 实际上,我使用线程自动捕获(复制/粘贴)图像 你知道我必须设置哪些参数才能获得正确的图像吗 提前谢谢。 问候 *********************************编辑

我有个小问题。我想将我的图表导出为图像。我知道这在beto rodriguez()给出的代码中是可能的

但是我有一个问题,我不能通过在屏幕上显示来获得我所能得到的。见上图。在右下角,我将图像保存为png格式。在图像的左边,我有一张图表,上面显示了我想要的所有参数

您知道在保存的图像中是否可能有相同的图表(在左侧)? 实际上,我使用线程自动捕获(复制/粘贴)图像

你知道我必须设置哪些参数才能获得正确的图像吗

提前谢谢。 问候

*********************************编辑后

对不起,我迟到了。我试过你说的,但遗憾的是我做不到我想要的。 我将解释我的工作: 我有一个类“MakeReport”,它可以调用另一个类“GraphMaker”:

类“MakeReport”将使用子程序“GiveAGraph()”使用一些值完成图表:

“GiveAGRaph()”的结尾

现在我已经准备好显示一个图表,我想用它来制作一个图像,并为测试(和调试)显示它:

// Chart to Image
MyGraph.GiveMeAnImageFromChart();      <-- to make a picture with the chart
// Show the graph
MyGraph.Show();                        <-- for debug and test, i display it.
“myChart”变量是公共的。使用的“SaveToPng”程序来自您的示例()

为了保存图片,我尝试以下方法:

public System.Drawing.Bitmap ControlToImage(Visual target, double dpiX, double dpiY)
    {
        if (target == null)
        {
            return null;
        }
        // render control content
        Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
        Console.WriteLine("Bounds width = " + bounds.Width + " et bounds height = " + bounds.Height);
        RenderTargetBitmap rtb = new RenderTargetBitmap((int)(bounds.Width * dpiX / 96.0),
                                                        (int)(bounds.Height * dpiY / 96.0),
                                                        dpiX,
                                                        dpiY,
                                                        PixelFormats.Pbgra32);
        DrawingVisual dv = new DrawingVisual();
        using (DrawingContext ctx = dv.RenderOpen())
        {
            VisualBrush vb = new VisualBrush(target);
            ctx.DrawRectangle(vb, null, new Rect(new System.Windows.Point(), bounds.Size));
        }
        rtb.Render(dv);

        //convert image format
        MemoryStream stream = new MemoryStream();
        BitmapEncoder encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(rtb));
        encoder.Save(stream);

        return new System.Drawing.Bitmap(stream);
    }
与:

Bitmap ImageChart = MyGraph.ControlToImage(MyGraph, MyGraph.Width, MyGraph.Height);
使用这个方法,我有一个错误,因为“bounds.Width”和“bounds.Height”等于-8。最后,我没有任何图表可以转换

我想我在“控制图像”中给出了一个错误的“视觉目标”,我错过了一些东西,但我不知道是什么。如果你需要更多的信息,问我。 提前感谢你的帮助


PS:对不起我的英语。请不要犹豫,请纠正我。

您所指的示例是在内存中创建的一个新图表实例,这就是为什么您会得到一个不同的图像,您可以在两个图表中复制相同的属性,一个在UI中显示,另一个在内存中创建,或者您可以在UI中打印当前实例的属性,如本期所述:


希望能有所帮助

多亏了bto rdz,我制定了一个解决方案。我发现我没有正确使用Livecharts。 因此,如果有人需要,我会发布我的代码

这是我的GraphMaker.xaml文件:

<UserControl x:Class="MyProject.GraphMaker"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:local="clr-namespace:MyProject"
        mc:Ignorable="d" 
        d:DesignHeight="730" d:DesignWidth="1660">
<Grid>
</Grid>
</UserControl>

还有我的GraphMaker.xaml.cs文件

    using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using LiveCharts;
using LiveCharts.Wpf;
using System.Collections.Generic;
using System.ComponentModel;


namespace MyProject
{    
    public partial class GraphMaker : UserControl
    {
        // PUBLIC
        public CartesianChart MyTestChart;
        public SeriesCollection MySeriesCollection { get; set; }
        public string[] Labels { get; set; }
        public string AxisTitle { get; set; }
        public Func<double, string> YFormatter { get; set; }
        public Axis Axis1, Axis2, Axis3, Axis4, Axis5, Axis6, Axis7, Axis8, Axis9, Axis10, AxisXChart;


        public GraphMaker()
        {
            InitializeComponent();
            MySeriesCollection = new SeriesCollection();

            MyTestChart = new CartesianChart
            {
                DisableAnimations = true,
                Width = 1600,
                Height = 700,
                Series = MySeriesCollection
            };

            MyTestChart.LegendLocation = LegendLocation.Right;

            // *** Axis 1 ***
            Axis1 = new Axis();
            Axis1.Foreground = Brushes.DodgerBlue;
            Axis1.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis1.LabelFormatter = YFormatter;
            MyTestChart.AxisY.Add(Axis1);

            // *** Axis 2 ***
            Axis2 = new Axis();
            Axis2.Foreground = Brushes.IndianRed;
            Axis2.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis2.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis2);

            // *** Axis 3 ***
            Axis3 = new Axis();
            Axis3.Foreground = Brushes.Gold;
            Axis3.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis3.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis3);

            // *** Axis 4 ***
            Axis4 = new Axis();
            Axis4.Foreground = Brushes.Gray;
            Axis4.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis4.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis4);

            // *** Axis 5 ***
            Axis5 = new Axis();
            Axis5.Foreground = Brushes.DeepSkyBlue;
            Axis5.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis5.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis5);

            // *** Axis 6 ***
            Axis6 = new Axis();
            Axis6.Foreground = Brushes.HotPink;
            Axis6.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis6.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis6);

            // *** Axis 7 ***
            Axis7 = new Axis();
            Axis7.Foreground = Brushes.Orange;
            Axis7.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis7.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis7);

            // *** Axis 8 ***
            Axis8 = new Axis();
            Axis8.Foreground = Brushes.RoyalBlue;
            Axis8.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis8.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis8);

            // *** Axis 9 ***
            Axis9 = new Axis();
            Axis9.Foreground = Brushes.Black;
            Axis9.Position = AxisPosition.RightTop;
            Axis9.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis9);

            // *** Axis 10 ***
            Axis10 = new Axis();
            Axis10.Foreground = Brushes.DarkTurquoise;
            Axis10.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis10.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis10);

            AxisXChart = new Axis();
            AxisXChart.Title = AxisTitle;
            AxisXChart.Labels = Labels;
        }


        public void TakeTheChart()
        {
            var viewbox = new Viewbox();
            viewbox.Child = MyTestChart;
            viewbox.Measure(MyTestChart.RenderSize);
            viewbox.Arrange(new Rect(new Point(0, 0), MyTestChart.RenderSize));
            MyTestChart.Update(true, true); //force chart redraw
            viewbox.UpdateLayout();

            SaveToPng(MyTestChart, "Chart.png");
            //png file was created at the root directory.
        }

        public void SaveToPng(FrameworkElement visual, string fileName)
        {
            var encoder = new PngBitmapEncoder();
            EncodeVisual(visual, fileName, encoder);
        }

        private static void EncodeVisual(FrameworkElement visual, string fileName, BitmapEncoder encoder)
        {
            var bitmap = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight, 96, 96, PixelFormats.Pbgra32);
            bitmap.Render(visual);
            var frame = BitmapFrame.Create(bitmap);
            encoder.Frames.Add(frame);
            using (var stream = File.Create(fileName)) encoder.Save(stream);
        }
    }
}
使用系统;
使用System.IO;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用动态图表;
使用LiveCharts.Wpf;
使用System.Collections.Generic;
使用系统组件模型;
名称空间MyProject
{    
公共部分类GraphMaker:UserControl
{
//公开的
公共卡特尔夏特MyTestChart;
公共序列集合MySeriesCollection{get;set;}
公共字符串[]标签{get;set;}
公共字符串AxisTitle{get;set;}
公共函数YFormatter{get;set;}
公共轴Axis1、Axis2、Axis3、Axis4、Axis5、Axis6、Axis7、Axis8、Axis9、Axis10、AxisXChart;
公共绘图机()
{
初始化组件();
MySeriesCollection=新系列集合();
MyTestChart=新的CartesianChart
{
否定=正确,
宽度=1600,
高度=700,
系列=我的经验集合
};
MyTestChart.LegendLocation=LegendLocation.Right;
//***轴1***
Axis1=新轴();
Axis1.前景=画笔.DodgerBlue;
Axis1.Position=AxisPosition.RightTop;
YFormatter=value=>value.ToString(“N2”);
Axis1.LabelFormatter=YFormatter;
MyTestChart.AxisY.Add(Axis1);
//***轴2***
Axis2=新轴();
Axis2.前景=画笔.IndianRed;
Axis2.Position=AxisPosition.RightTop;
YFormatter=value=>value.ToString(“N2”);
Axis2.LabelFormatter=YFormatter;
//MyTestChart.AxisY.Add(Axis2);
//***轴3***
Axis3=新轴();
Axis3.前景=画笔.Gold;
Axis3.Position=AxisPosition.RightTop;
YFormatter=value=>value.ToString(“N2”);
Axis3.LabelFormatter=YFormatter;
//MyTestChart.AxisY.Add(Axis3);
//***轴4***
Axis4=新轴();
Axis4.前景=画笔。灰色;
Axis4.Position=AxisPosition.RightTop;
YFormatter=value=>value.ToString(“N2”);
Axis4.LabelFormatter=YFormatter;
//MyTestChart.AxisY.Add(Axis4);
//***轴5***
Axis5=新轴();
Axis5.前景=画笔.DeepSkyBlue;
Axis5.Position=AxisPosition.RightTop;
YFormatter=value=>value.ToString(“N2”);
Axis5.LabelFormatter=YFormatter;
//MyTestChart.AxisY.Add(Axis5);
//***轴6***
Axis6=新轴();
Axis6.前景=画笔.HotPink;
Axis6.Position=AxisPosition.RightTop;
YFormatter=value=>value.ToString(“N2”);
Axis6.LabelFormatter=YFormatter;
//MyTestChart.AxisY.Add(Axis6);
//***轴7***
Axis7=新轴();
Axis7.前景=画笔。橙色;
Axis7.Position=AxisPosition.RightTop;
YFormatter=value=>value.ToString(“N2”);
Axis7.LabelFormatter=YFormatter;
//MyTestChart.AxisY.Add(Axis7);
//***轴8***
Axis8=新轴();
Axis8.前景=画笔。蓝色;
Axis8.Position=AxisPosition.RightTop;
YFormatter=value=>value.ToString(“N2”);
Axis8.LabelFormatter=YFormatter;
//MyTestChart.AxisY.Add(Axis8);
Bitmap ImageChart = MyGraph.ControlToImage(MyGraph, MyGraph.Width, MyGraph.Height);
<UserControl x:Class="MyProject.GraphMaker"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:local="clr-namespace:MyProject"
        mc:Ignorable="d" 
        d:DesignHeight="730" d:DesignWidth="1660">
<Grid>
</Grid>
</UserControl>
    using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using LiveCharts;
using LiveCharts.Wpf;
using System.Collections.Generic;
using System.ComponentModel;


namespace MyProject
{    
    public partial class GraphMaker : UserControl
    {
        // PUBLIC
        public CartesianChart MyTestChart;
        public SeriesCollection MySeriesCollection { get; set; }
        public string[] Labels { get; set; }
        public string AxisTitle { get; set; }
        public Func<double, string> YFormatter { get; set; }
        public Axis Axis1, Axis2, Axis3, Axis4, Axis5, Axis6, Axis7, Axis8, Axis9, Axis10, AxisXChart;


        public GraphMaker()
        {
            InitializeComponent();
            MySeriesCollection = new SeriesCollection();

            MyTestChart = new CartesianChart
            {
                DisableAnimations = true,
                Width = 1600,
                Height = 700,
                Series = MySeriesCollection
            };

            MyTestChart.LegendLocation = LegendLocation.Right;

            // *** Axis 1 ***
            Axis1 = new Axis();
            Axis1.Foreground = Brushes.DodgerBlue;
            Axis1.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis1.LabelFormatter = YFormatter;
            MyTestChart.AxisY.Add(Axis1);

            // *** Axis 2 ***
            Axis2 = new Axis();
            Axis2.Foreground = Brushes.IndianRed;
            Axis2.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis2.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis2);

            // *** Axis 3 ***
            Axis3 = new Axis();
            Axis3.Foreground = Brushes.Gold;
            Axis3.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis3.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis3);

            // *** Axis 4 ***
            Axis4 = new Axis();
            Axis4.Foreground = Brushes.Gray;
            Axis4.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis4.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis4);

            // *** Axis 5 ***
            Axis5 = new Axis();
            Axis5.Foreground = Brushes.DeepSkyBlue;
            Axis5.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis5.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis5);

            // *** Axis 6 ***
            Axis6 = new Axis();
            Axis6.Foreground = Brushes.HotPink;
            Axis6.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis6.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis6);

            // *** Axis 7 ***
            Axis7 = new Axis();
            Axis7.Foreground = Brushes.Orange;
            Axis7.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis7.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis7);

            // *** Axis 8 ***
            Axis8 = new Axis();
            Axis8.Foreground = Brushes.RoyalBlue;
            Axis8.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis8.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis8);

            // *** Axis 9 ***
            Axis9 = new Axis();
            Axis9.Foreground = Brushes.Black;
            Axis9.Position = AxisPosition.RightTop;
            Axis9.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis9);

            // *** Axis 10 ***
            Axis10 = new Axis();
            Axis10.Foreground = Brushes.DarkTurquoise;
            Axis10.Position = AxisPosition.RightTop;
            YFormatter = value => value.ToString("N2");
            Axis10.LabelFormatter = YFormatter;
            //MyTestChart.AxisY.Add(Axis10);

            AxisXChart = new Axis();
            AxisXChart.Title = AxisTitle;
            AxisXChart.Labels = Labels;
        }


        public void TakeTheChart()
        {
            var viewbox = new Viewbox();
            viewbox.Child = MyTestChart;
            viewbox.Measure(MyTestChart.RenderSize);
            viewbox.Arrange(new Rect(new Point(0, 0), MyTestChart.RenderSize));
            MyTestChart.Update(true, true); //force chart redraw
            viewbox.UpdateLayout();

            SaveToPng(MyTestChart, "Chart.png");
            //png file was created at the root directory.
        }

        public void SaveToPng(FrameworkElement visual, string fileName)
        {
            var encoder = new PngBitmapEncoder();
            EncodeVisual(visual, fileName, encoder);
        }

        private static void EncodeVisual(FrameworkElement visual, string fileName, BitmapEncoder encoder)
        {
            var bitmap = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight, 96, 96, PixelFormats.Pbgra32);
            bitmap.Render(visual);
            var frame = BitmapFrame.Create(bitmap);
            encoder.Frames.Add(frame);
            using (var stream = File.Create(fileName)) encoder.Save(stream);
        }
    }
}
        public static bool ChartToImage(this LiveCharts.WinForms.CartesianChart cartesianChart, LineSeries data, Axis AxisX, Axis AxisY,double Width, double Height, string fileName, string targetPath, out string locationOfImage, out Exception returnEx)
    {

        bool status = false;
        returnEx = null;
        locationPath = null;
        try
        {
            var myChart = new LiveCharts.Wpf.CartesianChart
            {
                DisableAnimations = true,
                Width = Width,
                Height = Height,
                Series = new SeriesCollection(cartesianChart.Series.Configuration)
                {
                   new LineSeries
                   {
                       Title = data.Title,
                       LineSmoothness = data.LineSmoothness,
                       StrokeThickness = data.StrokeThickness,
                       PointGeometrySize = data.PointGeometrySize,
                       Stroke = data.Stroke,
                       Values=data.Values
                   }
                }

            };
            myChart.AxisX.Add(new Axis { IsMerged = AxisX.IsMerged, FontSize = AxisX.FontSize, FontWeight = AxisX.FontWeight, Foreground = AxisX.Foreground, Separator = new LiveCharts.Wpf.Separator { Step = AxisX.Separator.Step, StrokeThickness = AxisX.Separator.StrokeThickness, StrokeDashArray = AxisX.Separator.StrokeDashArray, Stroke = AxisX.Separator.Stroke }, Title = AxisX.Title, MinValue = AxisX.MinValue, MaxValue = AxisX.MaxValue });
            myChart.AxisY.Add(new Axis { IsMerged = AxisY.IsMerged, FontSize = AxisY.FontSize, FontWeight = AxisY.FontWeight, Foreground = AxisY.Foreground, Separator = new LiveCharts.Wpf.Separator { Step = AxisY.Separator.Step, StrokeThickness = AxisY.Separator.StrokeThickness, StrokeDashArray = AxisY.Separator.StrokeDashArray, Stroke = AxisX.Separator.Stroke }, Title = AxisY.Title, MinValue = AxisY.MinValue, MaxValue = AxisY.MaxValue });


            var viewbox = new Viewbox();
            viewbox.Child = myChart;
            viewbox.Measure(myChart.RenderSize);
            viewbox.Arrange(new Rect(new System.Windows.Point(0, 0), myChart.RenderSize));
            myChart.Update(true, true); //force chart redraw
            viewbox.UpdateLayout();

            var encoder = new PngBitmapEncoder();
            var bitmap = new RenderTargetBitmap((int)myChart.ActualWidth, (int)myChart.ActualHeight, 96, 96, PixelFormats.Pbgra32);
            bitmap.Render(myChart);
            var frame = BitmapFrame.Create(bitmap);
            encoder.Frames.Add(frame);
            string path = Path.Combine(targetPath, fileName);
            using (var stream = File.Create(path))
            {
                encoder.Save(stream);
                locationPath = path;
            }
            myChart = null;
            viewbox = null;
            encoder = null;
            bitmap = null;
            frame = null;
            status = true;
        }
        catch (Exception ex)
        {
            returnEx = ex;
            status = false;
        }
        return status;
    }
                    if(cartesianChart1.ChartToImage(data,axisX,axisY,600,200,"test.png", locationImage, out string locationOfFile, out Exception returnEx))
                {
                    System.Windows.Forms.MessageBox.Show(locationOfFile);
                }