一、一定要有個圖形對象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類中,以供圖形打印。最終圖形效果如下: