Click here to Skip to main content
15,314,594 members
Articles / Programming Languages / C#
Article
Posted 1 Sep 2004

Stats

97.8K views
3.5K downloads
25 bookmarked

How to capture image and print the MSChart

Rate me:
Please Sign up or sign in to vote.
1.05/5 (13 votes)
1 Sep 20041 min read
How to capture image and print the MSChart.

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.

  1. How to capture the image?
  2. 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!

C#
using System.Drawing.Printing;
using System.Drawing.Imaging;

Instructions

  1. Create a normal Windows Form in C#.
  2. Create a MSChart on the Form.
  3. Create a toolbar with three buttons - with names (e.g.: toolBarbtnCapture, toolBarbtnPrintPreview, toolBarbtnPrint).
  4. Create an ImageList.
  5. Load three images into the ImageList.
  6. Link the ImageList with the toolbar through the ToolBarButton property: ImageList.
  7. In the 'ToolBarButton Collection Editor', specify the ImageIndex.

Copy and paste the code below and it will work! Voila! :)

C#
private void printGraph_PrintPage(object sender, 
       System.Drawing.Printing.PrintPageEventArgs e)
{
  MSChart.EditCopy();
  Bitmap chartCapture = 
      (Bitmap) Clipboard.GetDataObject().GetData("Bitmap", true);
  //setting the alignment for the printing session
  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
      {
        //setting the print layout to landscape
        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! :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Kuan Wai Mun
Kenya Kenya
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pin
peteSJ4-Jun-12 19:05
MemberpeteSJ4-Jun-12 19:05 
Generalyou can try "yourChart.Printing.Print(true)" with C# projects Pin
WuYuanli21-Mar-10 22:32
MemberWuYuanli21-Mar-10 22:32 
GeneralMy vote of 1 Pin
Chetan Patel7-Jan-10 0:46
MemberChetan Patel7-Jan-10 0:46 
GeneralRE: How to capture image and print the MSChart Pin
Fissa16-Jul-07 8:26
MemberFissa16-Jul-07 8:26 
GeneralRe: RE: How to capture image and print the MSChart Pin
ricky197110-Sep-08 0:41
Memberricky197110-Sep-08 0:41 
GeneralRe: RE: How to capture image and print the MSChart Pin
dawidf30-Sep-08 1:11
Memberdawidf30-Sep-08 1:11 
GeneralRe: RE: How to capture image and print the MSChart Pin
Ritesh123416-Mar-09 19:49
MemberRitesh123416-Mar-09 19:49 
GeneralRe: RE: How to capture image and print the MSChart Pin
Giochi716-Mar-11 14:04
MemberGiochi716-Mar-11 14:04 
GeneralA solution Pin
Mohamed Y. Elamrani26-Mar-06 5:38
MemberMohamed Y. Elamrani26-Mar-06 5:38 
GeneralThis is the wrong approach Pin
Joe Sonderegger5-Aug-05 1:01
MemberJoe Sonderegger5-Aug-05 1:01 
GeneralRe: This is the wrong approach Pin
Kuan Wai Mun11-Oct-05 23:29
MemberKuan Wai Mun11-Oct-05 23:29 
GeneralRe: This is the wrong approach Pin
Joe Sonderegger12-Oct-05 0:55
MemberJoe Sonderegger12-Oct-05 0:55 
GeneralRe: This is the wrong approach Pin
Kuan Wai Mun12-Oct-05 15:54
MemberKuan Wai Mun12-Oct-05 15:54 
GeneralRe: This is the wrong approach Pin
Joe Sonderegger12-Oct-05 20:29
MemberJoe Sonderegger12-Oct-05 20:29 
GeneralRe: This is the wrong approach Pin
JSchmitty30-Mar-06 11:58
MemberJSchmitty30-Mar-06 11:58 
GeneralRe: This is the wrong approach Pin
Joe Sonderegger3-Apr-06 1:35
MemberJoe Sonderegger3-Apr-06 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.