Visual Studio .NET 2002.NET 1.0Visual Studio .NET 2003.NET 1.1Windows 2000中级开发Visual StudioWindows.NETC#
如何捕获图像并打印 MSChart






1.05/5 (11投票s)
2004年9月2日
1分钟阅读

101698

3515
如何捕获图像并打印 MSChart。
引言
MSChart 是 .NET framework 附带的一个控件,可用于用 C# 或 VB.NET 编写的应用程序。以下是我从 MSDN 摘取的 MSChart 描述。
MSChart 控件支持以下功能
- 真正的三维表示。
- 支持所有主要的图表类型。
- 通过随机数据和数据数组填充数据网格。
这个来自我...
MSChart 控件是免费的!:)
我想分享一些我使用 MSChart 的经验。
- 如何捕获图像?
- 如何进行图表预览和打印?
希望这对公众有用。开始吧...
提醒
提醒:包含这两个重要的类!
using System.Drawing.Printing;
using System.Drawing.Imaging;
说明
- 在 C# 中创建一个普通的 Windows Form。
- 在 Form 上创建一个 MSChart。
- 创建一个工具栏,包含三个按钮 - 命名为(例如:
toolBarbtnCapture
、toolBarbtnPrintPreview
、toolBarbtnPrint
)。 - 创建一个
ImageList
。 - 将三个图像加载到
ImageList
中。 - 通过
ToolBarButton
属性将ImageList
与工具栏关联:ImageList
。 - 在“工具栏按钮集合编辑器”中,指定
ImageIndex
。
复制并粘贴下面的代码,它就能工作了!太棒了!:)
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;
}
}
该代码将检测按下的图像索引(不是工具栏按钮索引)。可能是一种笨拙的方法,但请包涵这个初学者!:)