C#将多个图表保存为一张图像

时间:2019-03-21 03:05:20

标签: c# winforms

当前,我正在尝试开发一个Winform,该Winform在流面板布局上显示多个图表控件。我知道如何一一保存图表控件,但是如何将多个图表控件保存为一种图像格式(例如png / jpg)?我尝试先将它们保存在流中,但无法将它们全部保存到一张图像中。

请指导我进行努力。谢谢

代码:

 try
            {
                if (dgvDataLog.Rows.Count > 0)
                {
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.Title = "Save PNG Files";
                    saveFileDialog1.CheckPathExists = true;
                    saveFileDialog1.DefaultExt = "png";
                    saveFileDialog1.Filter = "PNG files (*.png)|*.png";
                    saveFileDialog1.FilterIndex = 2;
                    saveFileDialog1.RestoreDirectory = true;
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        using (var chartimage = new MemoryStream())
                        {
                            tabPageChart1.SaveImage(chartimage, ChartImageFormat.Png);
                            tabPageChart2.SaveImage(chartimage, ChartImageFormat.Png);
                            tabPageChart3.SaveImage(chartimage, ChartImageFormat.Png);
                        }

                        MessageBox.Show("Image Saved At " + saveFileDialog1.FileName, "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("No Record To Be Export.", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            catch (SystemException errMsg)
            {
                MessageBox.Show(errMsg.ToString());
            }

1 个答案:

答案 0 :(得分:2)

首先编写一种将图表转换为图像的方法:

    List<Image> ChartsToImages(List<Chart> charts)
    {
        var imageList = new List<Image>();            
        foreach (var c in charts)
        {
            using (var ms = new MemoryStream())
            {
                c.SaveImage(ms, ChartImageFormat.Png);
                var bmp = System.Drawing.Bitmap.FromStream(ms);
                imageList.Add(bmp);
            }
        }
        return imageList;
    }

然后是一种将所有图像拼接在一起的方法:

    private static Image MergeImages(List<Image> imageList)
    {
        var finalSize = new Size();
        foreach (var image in imageList)
        {
            if (image.Width > finalSize.Width)
            {
                finalSize.Width = image.Width;
            }
            finalSize.Height += image.Height;
        }
        var outputImage = new Bitmap(finalSize.Width, finalSize.Height);
        using (var gfx = Graphics.FromImage(outputImage))
        {
            var y = 0;
            foreach (var image in imageList)
            {
                gfx.DrawImage(image, 0, y);
                y += image.Height;
            }
        }
        return outputImage;
    }

最后,将它们放在一起:

        var chartList = new List<Chart> {tabPageChart1, tabPageChart2, tabPageChart3};
        var imageList = ChartsToImages(chartList);
        var finalImage = MergeImages(imageList);
        finalImage.Save("C:\\Junk\\Junk.png", ImageFormat.Png);