您的位置:首页 > 其它

GDI+绘图技术应用之图形的保存和打印

2005-11-05 10:48 323 查看
一、一定要有个图形对象Image
这里,我们定义一个图形对象Bitmap imagemap,假定imagemap就是我们绘制出来的图形对象。
二、给操作界面(WinForm窗体:GraphicsAnalyse)添加一个contextMenu,并给GraphicsAnalyse_Load事件添加脚本
功能:添加上下文菜单,可以右击出现如下菜单(包括保存图形、打印图形、页面设置、关闭)
1、给GraphicsAnalyse的ContextMenu属性添加contextMenu1,使其窗体与contextMenu1关联。
2、在GraphicsAnalyse_Load事件中添加如下脚本,初始化弹出式菜单
contextMenu1.MenuItems.Add("保存图形", new EventHandler(this.Right_Clicked));
contextMenu1.MenuItems.Add("打印图形", new EventHandler(this.Right_Clicked));
contextMenu1.MenuItems.Add("页面设置", new EventHandler(this.Right_Clicked));
contextMenu1.MenuItems.Add("-");
contextMenu1.MenuItems.Add("关闭", new EventHandler(this.Right_Clicked));
3、添加菜单处理程序
//弹出式菜单项处理程序
private void Right_Clicked(object sender, System.EventArgs e)
{
MenuItem miClicked = (MenuItem)sender;
string strMenu = ((MenuItem)sender).Text;
if (strMenu == "保存图形")
{
GraphicSave();
}
else if (strMenu == "打印图形")
{
prePrint();
}
else if (strMenu == "页面设置")
{
pageSetUp();
}
else if (strMenu == "关闭")
{
ClsFrameCommunicate.Close();
}
else
{
return;
}
}
4、给“图形保存”菜单定义一个方法:
//图形保存
private void GraphicSave()
{
if (imagemap == null)
{
return;
}
else
{
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Title = "保存图形为...";
saveDlg.OverwritePrompt = true;
saveDlg.CheckPathExists = true;
saveDlg.Filter = "Bitmap files (*.bmp)|*.bmp|Gif File (*.gif)|*.gif|JPEG File (*.jpg)|*.jpg|Png File (*.png)|*.png" ;
saveDlg.ShowHelp = true;
if (saveDlg.ShowDialog() == DialogResult.OK)
{
string fileName = saveDlg.FileName;
string strFilExtn = fileName.Remove(0, fileName.Length - 3);
try
{
switch (strFilExtn)
{
case "bmp":
imagemap.Save(fileName, ImageFormat.Bmp);
break;
case "jpg":
imagemap.Save(fileName, ImageFormat.Jpeg);
break;
case "png":
imagemap.Save(fileName, ImageFormat.Png);
break;
case "gif":
imagemap.Save(fileName, ImageFormat.Gif);
break;
default:
break;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
5、给“打印图形”菜单定义一个方法:
//图形打印预览
private void prePrint()
{
if (strGraphicType == null || strGraphicType =="")
{
return;
}
try
{
hastb["Bitmap"] = imagemap;
GraphicsPrint gPrint = new GraphicsPrint(hastb);
try
{
gPrint.priviewPrint();
}
catch(Exception ex)
{
MessageBox.Show("print priview start "+ex.Message);
}
}
catch(Exception ex)
{
MessageBox.Show("priview before "+ex.Message);
}
}
6、给“页面设置”定义一个方法
//图形页面设置
private void pageSetUp()
{
if (strGraphicType == null || strGraphicType =="")
{
return;
}
hastb["GraphicsType"] = strGraphicType;
hastb["Bitmap"] = imagemap;
GraphicsPrint gPrint = new GraphicsPrint(hastb);
gPrint.pageSetup();
}

7、定义一个图形打印的类GraphicPrint
using System;
using System.Drawing.Printing;
using System.Configuration;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
namespace DSSAnalyse
{
/// <summary>
/// GraphicsPrint 的摘要说明。
/// </summary>
public class GraphicsPrint
{
private PrintPreviewDialog previewDlg = null;
private PageSetupDialog setupDlg = null;
private PrintDocument printDoc = null;
private PrintDialog printDlg = null;
Image imgPrint;
Hashtable hashTable;
public GraphicsPrint(Hashtable hast)
{
//
// TODO: 在此处添加构造函数逻辑
//
hashTable = hast;
imgPrint =(Image)hashTable["Bitmap"];
//创建打印预览对话框和其它对话框
printDoc = new PrintDocument();
previewDlg = new PrintPreviewDialog();
setupDlg = new PageSetupDialog();
printDlg = new PrintDialog();
printDoc.DocumentName ="图形打印...";
previewDlg.Document = printDoc;
setupDlg.Document = printDoc;
printDlg.Document = printDoc;
printDlg.AllowSelection = true;
printDlg.AllowSomePages = true;
printDoc.PrintPage += new PrintPageEventHandler(this.PrintGraphicsItemsHandler);
}
//打印处理程序
private void PrintGraphicsItemsHandler(object sender, PrintPageEventArgs ppeArgs)
{
Graphics g = ppeArgs.Graphics;
g.DrawImage(imgPrint,0,0);
}
//打印预览程序
public void priviewPrint()
{
previewDlg.UseAntiAlias = true;
previewDlg.ShowDialog();
}
//页面设置程序
public void pageSetup()
{
if (setupDlg.ShowDialog() == DialogResult.OK)
{
printDoc.DefaultPageSettings = setupDlg.PageSettings;
printDoc.PrinterSettings = setupDlg.PrinterSettings;
}
}
}
}
8、说明:strGraphicType变量定义当前是什么图形,hastb["Bitmap"]中定义了当前的图形对象,传入到GraphicPrint类中,以供图形打印。最终图形效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息