Click here to Skip to main content
12,068,967 members (69,036 online)
Click here to Skip to main content
Add your own
alternative version

Stats

82.7K views
3.2K downloads
25 bookmarked
Posted

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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
peteSJ4-Jun-12 20:05
memberpeteSJ4-Jun-12 20:05 
Generalyou can try "yourChart.Printing.Print(true)" with C# projects Pin
WuYuanli21-Mar-10 23:32
memberWuYuanli21-Mar-10 23:32 
GeneralMy vote of 1 Pin
Chetan Patel7-Jan-10 1:46
memberChetan Patel7-Jan-10 1:46 
GeneralRE: How to capture image and print the MSChart Pin
Fissa16-Jul-07 9:26
memberFissa16-Jul-07 9:26 
GeneralRe: RE: How to capture image and print the MSChart Pin
ricky197110-Sep-08 1:41
memberricky197110-Sep-08 1:41 
GeneralRe: RE: How to capture image and print the MSChart Pin
dawidf30-Sep-08 2:11
memberdawidf30-Sep-08 2:11 
GeneralRe: RE: How to capture image and print the MSChart Pin
Ritesh123416-Mar-09 20:49
memberRitesh123416-Mar-09 20:49 
GeneralRe: RE: How to capture image and print the MSChart Pin
Giochi716-Mar-11 15:04
memberGiochi716-Mar-11 15:04 
GeneralA solution Pin
Abu Abdillah26-Mar-06 6:38
memberAbu Abdillah26-Mar-06 6:38 
GeneralThis is the wrong approach Pin
Nice Life5-Aug-05 2:01
memberNice Life5-Aug-05 2:01 
GeneralRe: This is the wrong approach Pin
Kuan Wai Mun12-Oct-05 0:29
memberKuan Wai Mun12-Oct-05 0:29 
GeneralRe: This is the wrong approach Pin
Nice Life12-Oct-05 1:55
memberNice Life12-Oct-05 1:55 
GeneralRe: This is the wrong approach Pin
Kuan Wai Mun12-Oct-05 16:54
memberKuan Wai Mun12-Oct-05 16:54 
GeneralRe: This is the wrong approach Pin
Nice Life12-Oct-05 21:29
memberNice Life12-Oct-05 21:29 
GeneralRe: This is the wrong approach Pin
JSchmitty30-Mar-06 12:58
memberJSchmitty30-Mar-06 12:58 
GeneralRe: This is the wrong approach Pin
Nice Life3-Apr-06 2:35
memberNice Life3-Apr-06 2: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.

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