将图表另存为图片 [英] Saving Chart as a Picture

查看:103
本文介绍了将图表另存为图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试从饼图创建图像文件.在步骤一和步骤二中,单击按钮时,将使用彩色创建饼图.这很好.第三步说将图表保存为图片.问题是创建的图像只是黑色.

步骤3说编辑createPieChart方法并定义位图对象以将图像保存到流中"

Hi All,

I am trying to create an image file from pie chart. In step one, and two the pie chart is created with colours when the button is clicked. This works fine. Step three says to save the Chart as a picture.The problem, the image that is created is just black.

Step 3 Says "Edit the createPieChart method and define a bitmap object to save the images to a stream"

Bitmap bmp = new Bitmap(600, 600); // Step 3




接下来,它显示编辑createPieChart方法,并通过调用Bitmap添加代码以保存图像.在foreach循环之后保存,如图所示"




The next it says "Edit the createPieChart method, and add the code to save the image by calling the Bitmap.Save after the foreach loop as shown"

bmp.Save("Survey.jpg" ,ImageFormat.Jpeg);     //Step 3




这是代码:




Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Creating_A_Pie_Chart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnChart_Click(object sender, EventArgs e)
        {
            creatPieChart();
           
        }
        Hashtable SurveyData = new Hashtable();

        private void Form1_Load(object sender, EventArgs e)
        {
            SurveyData.Add("Arts","10");
            SurveyData.Add("Science","30");
            SurveyData.Add("Commerce", "20");
            SurveyData.Add("Computer Science", "40");
            
        }

         void creatPieChart()
        {

           
            Bitmap bmp = new Bitmap(600, 600); // Step 3
            
            
            Color[] colorList = {Color.DarkMagenta, Color.Yellow, Color.Green, Color.Blue, Color.Lavender, Color.Wheat};

            float total = 0;

            foreach (DictionaryEntry de in SurveyData)
            {                
                total = total + float.Parse(de.Value.ToString());
            }
            Graphics graphicS = this.CreateGraphics();

            Pen pen = new Pen(Color.Black, 2);
            Rectangle rec = new Rectangle(btnChart.Location.X, btnChart.Location.Y + 25, 150, 150);

            float startDegrees = 0;
            int colorNum = 0;

            foreach (DictionaryEntry de in SurveyData)
            {
                float sweepDegrees = (float.Parse(de.Value.ToString()) / total) * 360;
                Brush b = new LinearGradientBrush(rec, colorList[colorNum++], Color.White, (float)45);
                graphicS.FillPie(b, rec, startDegrees, sweepDegrees);
                graphicS.DrawPie(pen, rec, startDegrees, sweepDegrees);
                startDegrees = startDegrees + sweepDegrees;
            }            
            bmp.Save("Survey.jpg" ,ImageFormat.Jpeg);     //Step 3      
        }
    }
}



我尝试使用memorystream和streamwriter,但无法正常工作.
这是带有Button btnChart的Windows窗体应用程序.

在此,我将非常感谢您的帮助,在此先感谢...



I tried using a memorystream, and a streamwriter, but could not get it to work.
It is a windows form application with a Button btnChart.

I would really appreciate a little help with this, thanks in advance...

推荐答案

您正在使用此方法.CreateGraphics(顺便说一句,您忘记了处置).您要绘制到Graphics.FromImage(bmp).另外,您可以将bmp用作表单的BackgroundImage,而不必绘制两次! (只需在设置它之后设置this.BackgroundImage = bmp.)

另外,由于图形是纯矢量数据,因此您可能需要考虑使用图元文件,而不是栅格格式(位图).并且不要将其另存为JPEG,对于纯色块而言,这是最糟糕的格式!
You''re drawing to this.CreateGraphics (which, incidentally, you forget to dispose). You want to be drawing to Graphics.FromImage(bmp). As an added bonus you can use bmp as the BackgroundImage of the form and not have to draw it twice! (Just set this.BackgroundImage = bmp after you''ve drawn to it.)

Also you might want to consider a Metafile, not a raster format (Bitmap), as a graph is pure vector data. And don''t save it as a JPEG, that''s the worst possible format for something which is blocks of solid colour!


这篇关于将图表另存为图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆