Introduction
MSChart is a control that comes with .NET framework and it can be used for applications written in C# or VB.NET. Below is the description of MSChart which I took from MSDN.
The MSChart control supports the following features:
- True three-dimensional representation.
- Support for all major chart types.
- Data grid population via random data and data arrays.
And this one comes from me...
MSChart control is free of charge! :)
I would like to share some of my experiences dealing with MSChart.
- How to capture the image?
- How to Print Preview and Print the Chart?
Hope this will be useful to the public. Here it goes...
Reminder
A reminder to include these two important classes!
using System.Drawing.Printing;
using System.Drawing.Imaging;
Instructions
- Create a normal Windows Form in C#.
- Create a MSChart on the Form.
- Create a toolbar with three buttons - with names (e.g.:
toolBarbtnCapture
, toolBarbtnPrintPreview
, toolBarbtnPrint
).
- Create an
ImageList
.
- Load three images into the
ImageList
.
- Link the
ImageList
with the toolbar through the ToolBarButton
property: ImageList
.
- In the 'ToolBarButton Collection Editor', specify the
ImageIndex
.
Copy and paste the code below and it will work! Voila! :)
private void printGraph_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
MSChart.EditCopy();
Bitmap chartCapture =
(Bitmap) Clipboard.GetDataObject().GetData("Bitmap", true);
e.Graphics.DrawImage(chartCapture, 8,80);
chartCapture.Dispose();
}
private void toolBar_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (e.Button.ImageIndex)
{
case 0:
MSChart.EditCopy();
Bitmap chartCapture =
(Bitmap) Clipboard.GetDataObject().GetData("Bitmap", true);
chartCapture.Save("Image.Jpeg");
break;
case 1:
try
{
printGraph.DefaultPageSettings.Landscape = true;
printPreviewDialog.Document = this.printGraph;
printPreviewDialog.ShowDialog();
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString());
}
break;
case 2:
printGraph.DefaultPageSettings.Landscape = true;
printDialog.Document = printGraph;
if(printDialog.ShowDialog() == DialogResult.OK)
{
printGraph.Print();
}
break;
}
}
The code will detect the image index (not the toolbar button index) pressed. Might be a lame way to do it but please bear with the beginner! :)