Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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



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      
        }
    }
}


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...
Posted

1 solution

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!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900