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);
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);
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);
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);
}
}
}
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...