Click here to Skip to main content
Click here to Skip to main content

How to capture image and print the MSChart

, 1 Sep 2004
Rate this:
Please Sign up or sign in to vote.
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! Smile | :)

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!

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! Smile | :)

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! Smile | :)

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 PinmemberpeteSJ4-Jun-12 20:05 
Generalyou can try "yourChart.Printing.Print(true)" with C# projects PinmemberWuYuanli21-Mar-10 23:32 
GeneralMy vote of 1 PinmemberChetan Patel7-Jan-10 1:46 
GeneralRE: How to capture image and print the MSChart PinmemberFissa16-Jul-07 9:26 
GeneralRe: RE: How to capture image and print the MSChart Pinmemberricky197110-Sep-08 1:41 
GeneralRe: RE: How to capture image and print the MSChart Pinmemberdawidf30-Sep-08 2:11 
GeneralRe: RE: How to capture image and print the MSChart PinmemberRitesh123416-Mar-09 20:49 
GeneralRe: RE: How to capture image and print the MSChart PinmemberGiochi716-Mar-11 15:04 
GeneralA solution PinmemberAbu Abdillah26-Mar-06 6:38 
GeneralThis is the wrong approach PinmemberNice Life5-Aug-05 2:01 
GeneralRe: This is the wrong approach PinmemberKuan Wai Mun12-Oct-05 0:29 
GeneralRe: This is the wrong approach PinmemberNice Life12-Oct-05 1:55 
GeneralRe: This is the wrong approach PinmemberKuan Wai Mun12-Oct-05 16:54 
GeneralRe: This is the wrong approach PinmemberNice Life12-Oct-05 21:29 
GeneralRe: This is the wrong approach PinmemberJSchmitty30-Mar-06 12:58 
GeneralRe: This is the wrong approach PinmemberNice Life3-Apr-06 2:35 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 2 Sep 2004
Article Copyright 2004 by Kuan Wai Mun
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid