如何将C#控件和额外文本保存到磁盘?

如何将C#控件和额外文本保存到磁盘?,c#,.net,microsoft-chart-controls,C#,.net,Microsoft Chart Controls,我有一个C#程序,它是一个控制台应用程序,但使用了System.Windows.Forms.DataVisualization.Charting中的图表对象,而不将图表附加到窗体 基本上,我需要在图表控件上生成一个油炸圈饼图,在油炸圈饼的中心打印一些文本,然后将该图和文本保存到磁盘上的文件中 如果我在PrePaint事件中创建TextAnnotation,然后使用DrawToBitmap将图表保存到磁盘,即使我使用了chart.Flush和chart.Update等多种组合,覆盖的文本也不会显示

我有一个C#程序,它是一个控制台应用程序,但使用了
System.Windows.Forms.DataVisualization.Charting
中的图表对象,而不将
图表
附加到
窗体

基本上,我需要在
图表
控件上生成一个油炸圈饼图,在油炸圈饼的中心打印一些文本,然后将该图和文本保存到磁盘上的文件中

如果我在
PrePaint
事件中创建
TextAnnotation
,然后使用
DrawToBitmap
将图表保存到磁盘,即使我使用了
chart.Flush
chart.Update
等多种组合,覆盖的文本也不会显示在磁盘上的文件中,正在激发事件并运行
TextAnnotation
代码

另一方面,如果我根本不使用事件,而是从
Chart.CreateGraphics
获取
Graphics
对象,然后
Graphics.DrawString
打印文本,然后
Graphics.Flush
Chart.DrawToBitmap
,文本仍然不显示

我猜这是因为
Chart.DrawToBitmap
不知道使用
Chart.CreateGraphics
绘制的额外内容,尽管我假设
Graphics.Flush
会解决这个问题

保存图形和文本的最佳方式是什么

编辑:按要求添加代码:

static void Main(string[] args)
{
    String[] labels = { "Green", "Red", "Yellow", "Dark Red", "Blue" };
    Color[] colours = { Color.Green, Color.Red, Color.Yellow, Color.DarkRed, Color.Blue };
    Random rng = new Random();

    Chart chart = new Chart();
    chart.Size = new Size(320, 320);

    ChartArea area = new ChartArea();
    Series series = new Series();

    chart.ChartAreas.Add(area);

    series.ChartArea = area.Name;
    series.ChartType = SeriesChartType.Doughnut;
    series.IsValueShownAsLabel = true;

    int total = 0;
    for (int i = 0; i != labels.Length; i++)
    {
        int value = rng.Next(0, 50);
        DataPoint p = new DataPoint(0, value);
        total += value;
        p.Color = colours[i];
        p.Label = String.Empty;
        p.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
        series.Points.Add(p);
    }

    series.Tag = total;
    chart.Series.Add(series);
    chart.Refresh();

    using (Graphics g = chart.CreateGraphics())
    {
        String str = series.Tag.ToString();
        Font font = new Font("Microsoft Sans Serif", 32, FontStyle.Bold);
        SizeF strSize = g.MeasureString(str, font);

        int strX = 100; int strY = 100;

        g.DrawString(str, font, new SolidBrush(Color.Black), strX, strY);
        g.DrawRectangle(new Pen(Color.Black), new Rectangle(strX, strY, (int)strSize.Width, (int)strSize.Height));

        g.Flush();
    }

    String chartFilename = "chart.bmp";
    String chartPath = Path.Combine("C:\\Temp", chartFilename);

    using (Bitmap bmp = new Bitmap(chart.Width, chart.Height))
    {
        chart.DrawToBitmap(bmp, chart.Bounds);
        bmp.Save(chartPath);
    }

    System.Diagnostics.Process.Start("mspaint.exe", chartPath);
}

我怀疑这张图表是在你的标签上方画的。当你调用DrawToBitmap时,图表只会考虑它所知道的视觉元素,而不是事后绘制的元素。

您需要反转绘图顺序。i、 e

  • 将图表绘制到位图中
  • 将位图与预渲染图表一起放在手上,在顶部绘制标签
  • 代码:

    使用(位图bmp=新位图(chart.Width、chart.Height))
    {
    chart.DrawToBitmap(bmp,chart.Bounds);//首先将图表绘制到位图中!
    
    使用(Graphics g=Graphics.FromImage(bmp))//到目前为止,您尝试的两件事情可能有错误,但在没有看到您的代码的情况下,我们无法帮助您。我认为最好也参考
    System.Drawing.Imaging
    ,使
    PixelFormat
    ImageFormat
    可用。此时,图像以
    .bmp
    扩展名保存,虽然它是一个
    .png
    文件(默认值)。这就做到了,谢谢!这让我沮丧了好几个小时,但逆转这个过程确实有效。
    using (Bitmap bmp = new Bitmap(chart.Width, chart.Height))
    {
        chart.DrawToBitmap(bmp, chart.Bounds); // draw chart into bitmap first!
    
        using (Graphics g = Graphics.FromImage(bmp)) // <--- new
        {
            // now draw label
            String str = series.Tag.ToString();
            Font font = new Font("Microsoft Sans Serif", 32, FontStyle.Bold);
            SizeF strSize = g.MeasureString(str, font);
    
            int strX = 100; int strY = 100;
    
            g.DrawString(str, font, new SolidBrush(Color.Black), strX, strY);
            g.DrawRectangle(new Pen(Color.Black), new Rectangle(strX, strY, (int)strSize.Width, (int)strSize.Height));
    
            g.Flush();
        }
    
        bmp.Save(chartPath);
    }