Click here to Skip to main content
Licence 
First Posted 1 Sep 2004
Views 68,656
Bookmarked 24 times

How to capture image and print the MSChart

By | 1 Sep 2004 | Article
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!

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

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

About the Author

Kuan Wai Mun



Kenya Kenya

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalyou can try "yourChart.Printing.Print(true)" with C# projects PinmemberWuYuanli22:32 21 Mar '10  
GeneralMy vote of 1 PinmemberChetan Patel0:46 7 Jan '10  
GeneralRE: How to capture image and print the MSChart PinmemberFissa8:26 16 Jul '07  
GeneralRe: RE: How to capture image and print the MSChart Pinmemberricky19710:41 10 Sep '08  
GeneralRe: RE: How to capture image and print the MSChart Pinmemberdawidf1:11 30 Sep '08  
GeneralRe: RE: How to capture image and print the MSChart PinmemberRitesh123419:49 16 Mar '09  
GeneralRe: RE: How to capture image and print the MSChart PinmemberGiochi714:04 16 Mar '11  
GeneralA solution PinmemberAbu Abdillah5:38 26 Mar '06  
GeneralThis is the wrong approach PinmemberNice Life1:01 5 Aug '05  
GeneralRe: This is the wrong approach PinmemberKuan Wai Mun23:29 11 Oct '05  
GeneralRe: This is the wrong approach PinmemberNice Life0:55 12 Oct '05  
GeneralRe: This is the wrong approach PinmemberKuan Wai Mun15:54 12 Oct '05  
GeneralRe: This is the wrong approach PinmemberNice Life20:29 12 Oct '05  
GeneralRe: This is the wrong approach PinmemberJSchmitty11:58 30 Mar '06  
GeneralRe: This is the wrong approach PinmemberNice Life1:35 3 Apr '06  

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.

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