LeadTools图像处理开发工具,在医学、DICOM、PACS、栅格、矢量和多媒体图像处理领域都处于世界领先的地位,在开发“打印图像”功能方面具有显著的优势。
打开Visual Studio .NET。
点击 文件->新建->项目…。
打开新建项目对话框后,在模板中选择“Visual C#”或“Visual Basic”,随后选择“Windows窗体应用程序”。在名称栏中输入项目名称“PrintImageTutorial”,并使用“浏览”按钮选择您工程的存储路径,点击“确定”。
在“解决方案资源管理器”中,右击“引用”,选择“添加引用”。在“引用管理器”中,浏览选择Leadtools For .NET文件夹”
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.WinForms.dll
Leadtools.Codecs.Bmp.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Codecs.Fax.dll
Leadtools.Codecs.Tif.dll
点击“确定”按钮,将以上所有的DLL添加到应用程序中。
以上的引用允许您使用BMP、JPG和TIF文件。如果您想使用更多的文件格式,可参考帮助文档Files To Be Included With Your Application的File Formats部分。
由于我们将使用Win32(x86)程序集引用,因此我们必须将这个项目的build目标改为x86。选择
项目->PrintImageTutorial 属性,按照以下步骤设置:
若为C#工程,选择生成选项栏,在配置下拉框选择“所有配置”,在目标平台下拉框选择“x86” 。
若为VB工程,选择编译选项卡,在配置下拉框选择“所有配置”。点击高级编译选项按钮,在目标CPU中选择“x86”。点击确定关闭对话框。
将Form1调整到设计视图,从工具箱(视图->工具箱)拖拽一个MenuStrip控件到窗体。点击新的menuStrip1控件,添加以下顶级菜单项:
选择“文件”菜单项添加以下子项:
选择“页面”菜单项添加以下子项:
选择“选项”菜单项添加以下子项:
从工具箱(视图->工具箱)拖拽一个RasterImageViewer实例至窗体。若您的工具箱没有RasterImageViewer,点击工具->选择工具箱项…。点击浏览从“
切换至Form1的代码视图(右击Form1,选择查看代码),将下面几行代码添加到文件开始处:
1: [C#]
2:
3: using System.Drawing.Printing;
4: using Leadtools;
5: using Leadtools.Codecs;
6: using Leadtools.WinForms;
为Form1添加以下私有变量:
1: [C#]
2: //加载图像时使用的 RasterCodecs 对象
3: private RasterCodecs _rasterCodecs;
4: //整个演示中我们会用到的 PrintDocument 对象
5: private PrintDocument _printDocument;
6: //当前要打印的页码
7: private int _currentPrintPageNumber;
8: //当前的图像文件名称
9: private string _currentImageFileName;
为Form1添加以下代码:
1: [C#]
2:
3: protected override void OnLoad(EventArgs e)
4: {
5: // 在加载图像时初始化 RasterCodecs 对象
6: this._rasterCodecs = new RasterCodecs();
7:
8: // 检查是否安装打印机
9: if(PrinterSettings.InstalledPrinters == null || PrinterSettings.InstalledPrinters.Count < 1)
10: {
11: MessageBox.Show(this,"此机器上未安装打印机,此程序无法继续运行","打印图像演示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
12: this.Close();
13: }
14: else
15: {
16: //创建我们将要使用的print document对象
17: this._printDocument = new PrintDocument();
18: //为打印事件添加句柄
19: this._printDocument.BeginPrint += new PrintEventHandler(_printDocument_BeginPrint);
20: this._printDocument.PrintPage += new PrintPageEventHandler(_printDocument_PrintPage);
21: this._printDocument.EndPrint += new PrintEventHandler(_printDocument_EndPrint);
22: }
23:
24: base.OnLoad(e);
25: }
26:
27: protected override void OnFormClosed(FormClosedEventArgs e)
28: {
29: //释放我们使用的资源
30: if(this._printDocument != null)
31: {
32: this._printDocument.Dispose();
33: }
34: if(this._rasterCodecs != null)
35: {
36: this._rasterCodecs.Dispose();
37: }
38: base.OnFormClosed(e);
39: }
将下列代码添加到fileToolStripMenuItem菜单项的DropDownOpening事件:
1: [C#]
2:
3: private void fileToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
4: {
5: //更新UI状态
6: printPreviewToolStripMenuItem.Enabled = (rasterImageViewer1.Image != null);
7: printToolStripMenuItem.Enabled = (rasterImageViewer1.Image != null);
8: }
为openToolStripMenuItem按钮添加Click事件:
1: [C#]
2:
3: private void openToolStripMenuItem_Click(object sender, EventArgs e)
4: {
5: //将图像加载至查看器
6: using(OpenFileDialog dlg = new OpenFileDialog())
7: {
8: dlg.Filter = "All Files|*.*";
9: if(dlg.ShowDialog(this) == DialogResult.OK)
10: {
11: try
12: {
13: //加载文件中的所有页面
14: rasterImageViewer1.Image = this._rasterCodecs.Load(dlg.FileName);
15: this._currentImageFileName = dlg.FileName;
16: UpdateCaption();
17: }
18: catch(Exception ex)
19: {
20: MessageBox.Show(this, ex.Message, "打印图像演示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
21: }
22: }
23: }
24: }
添加以下代码至printPreviewToolStripMenuItem菜单项的Click事件:
1: [C#]
2:
3: private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
4: {
5: //设置打印文档
6: SetupPrintDocument();
7:
8: // 使用 .NET PrintPreviewDialog
9: using(PrintPreviewDialog dlg = new PrintPreviewDialog())
10: {
11: // 显示对话框
12: dlg.Document = this._printDocument;
13: dlg.WindowState = FormWindowState.Maximized;
14: dlg.ShowDialog(this);
15: }
16: }
将以下代码添加到printSetupToolStripMenuItem菜单项的Click事件中:
1: [C#]
2:
3: private void printSetupToolStripMenuItem_Click(object sender, EventArgs e)
4: {
5: // 使用.NET PageSetupDialog
6: using(PageSetupDialog dlg = new PageSetupDialog())
7: {
8: dlg.Document = this._printDocument;
9: dlg.ShowDialog(this);
10: }
11: }
将以下代码添加到printToolStripMenuItem菜单项的Click事件中:
1: [C#]
2:
3: private void printToolStripMenuItem_Click(object sender, EventArgs e)
4: {
5: //用户可能会使用快捷键,因此即使我们禁用了菜单项,也需要检查打印状态是否可用
6: if(rasterImageViewer1.Image == null)
7: {
8: return;
9: }
10:
11: // 显示打印预览对话框,然后直接打印
12:
13: // 设置打印文档
14: SetupPrintDocument();
15:
16: this._printDocument.Print();
17: }
将以下代码添加到exitToolStripMenuItem菜单项的Click事件中:
1: [C#]
2:
3: private void exitToolStripMenuItem_Click(object sender, EventArgs e)
4: {
5: // 关闭应用程序
6: Close();
7: }
将以下代码添加到pageToolStripMenuItem菜单项的DropDownOpening事件中:
1: [C#]
2:
3: private void pageToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
4: {
5: // 更新UI 状态
6: firstPageToolStripMenuItem.Enabled = (rasterImageViewer1.Image != null && rasterImageViewer1.Image.Page > 1);
7: previousPageToolStripMenuItem.Enabled = firstPageToolStripMenuItem.Enabled;
8: nextPageToolStripMenuItem.Enabled = (rasterImageViewer1.Image != null && rasterImageViewer1.Image.Page < rasterImageViewer1.Image.PageCount);
9: lastPageToolStripMenuItem.Enabled = nextPageToolStripMenuItem.Enabled;
10: }
将以下代码添加到firstPageToolStripMenuItem菜单项的Click事件中:
1: [C#]
2: private void firstPageToolStripMenuItem_Click(object sender, EventArgs e)
3: {
4: //转到图像的首页
5: rasterImageViewer1.Image.Page = 1;
6: UpdateCaption();
7: }
将以下代码添加到previousPageToolStripMenuItem菜单项的Click事件中:
1: [C#]
2:
3: private void previousPageToolStripMenuItem_Click(object sender, EventArgs e)
4: {
5: // 转到图像的上一页
6: rasterImageViewer1.Image.Page--;
7: UpdateCaption();
8: }
将以下代码添加到nextPageToolStripMenuItem菜单项的Click事件中:
1: [C#]
2:
3: private void nextPageToolStripMenuItem_Click(object sender, EventArgs e)
4: {
5: // 转到图像的下一页
6: rasterImageViewer1.Image.Page++;
7: UpdateCaption();
8: }
将以下代码添加到lastPageToolStripMenuItem菜单项的Click事件中:
1: [C#]
2: private void lastPageToolStripMenuItem_Click(object sender, EventArgs e)
3: {
4: // 转到图像的尾页
5: rasterImageViewer1.Image.Page = rasterImageViewer1.Image.PageCount;
6: UpdateCaption();
7: }
将以下代码添加到usePageMarginsToolStripMenuItem菜单项的Click事件中:
1: [C#]
2:
3: private void usePageMarginsToolStripMenuItem_Click(object sender, EventArgs e)
4: {
5: // 切换选项
6: usePageMarginsToolStripMenuItem.Checked = !usePageMarginsToolStripMenuItem.Checked;
7: }
将以下代码添加到fitImageToPageToolStripMenuItem菜单项的Click事件中:
1: [C#]
2:
3: private void fitImageToPageToolStripMenuItem_Click(object sender, EventArgs e)
4: {
5: // 切换选项
6: fitImageToPageToolStripMenuItem.Checked = !fitImageToPageToolStripMenuItem.Checked;
7: }
将以下代码添加到Form1:
1: [C#]
2:
3: private void UpdateCaption()
4: {
5: //更新演示的标题,显示加载的图像文件名字和当前页码
6: if(rasterImageViewer1.Image != null)
7: {
8: Text = string.Format(
9: "{0} - 第 {1} 页 (共 {2} 页) - 打印图像演示",
10: this._currentImageFileName,
11: rasterImageViewer1.Image.Page,
12: rasterImageViewer1.Image.PageCount);
13: }
14: else
15: {
16: Text = "打印图像演示";
17: }
18: }
19:
20: private void SetupPrintDocument()
21: {
22: //在打印预览前或打印前调用,用于设置文档
23: //最小值/最大值 为图像的页面数
24: this._printDocument.PrinterSettings.MinimumPage = 1;
25: this._printDocument.PrinterSettings.MaximumPage = rasterImageViewer1.Image.PageCount;
26:
27: //默认打印所有页面
28: this._printDocument.PrinterSettings.FromPage = this._printDocument.PrinterSettings.MinimumPage;
29: this._printDocument.PrinterSettings.ToPage = this._printDocument.PrinterSettings.MaximumPage;
30:
31: //设置文档名字
32: this._printDocument.DocumentName = this._currentImageFileName;
33: }
34:
35: private void _printDocument_BeginPrint(object sender, PrintEventArgs e)
36: {
37: // 重置页码为首页
38: this._currentPrintPageNumber = 1;
39: }
40:
41: private void _printDocument_PrintPage(object sender, PrintPageEventArgs e)
42: {
43: // 打印一页
44:
45: // 获取print document 对象
46: PrintDocument document = sender as PrintDocument;
47:
48: // 创建一个新的LEADTOOLS image printer类
49: RasterImagePrinter printer = new RasterImagePrinter();
50:
51: //设置 document 对象以便进行页面计算
52: printer.PrintDocument = document;
53:
54: //检查我们是否想要使图像适应
55: if(fitImageToPageToolStripMenuItem.Checked)
56: {
57: //若为Yes, 将图像调整到最大打印区域并居中
58: printer.SizeMode = RasterPaintSizeMode.FitAlways;
59: printer.HorizontalAlignMode = RasterPaintAlignMode.Center;
60: printer.VerticalAlignMode = RasterPaintAlignMode.Center;
61: }
62: else
63: {
64: //若为No, 正常打印 (原始大小)
65: printer.SizeMode = RasterPaintSizeMode.Normal;
66: printer.HorizontalAlignMode = RasterPaintAlignMode.Near;
67: printer.VerticalAlignMode = RasterPaintAlignMode.Near;
68: }
69:
70: // 考虑具有不同水平和垂直分辨率的传真图像
71: printer.UseDpi = true;
72:
73: // 打印整个图像
74: printer.ImageRectangle = Rectangle.Empty;
75:
76: //使用最大页面维度,这和使用Windows照片库打印等效
77: printer.PageRectangle = RectangleF.Empty;
78: //无论我们是否要忽略页边距,都会通知打印机
79: printer.UseMargins = usePageMarginsToolStripMenuItem.Checked;
80:
81: // 打印当前页
82: printer.Print(rasterImageViewer1.Image, this._currentPrintPageNumber, e);
83:
84: // 转到下一页
85: this._currentPrintPageNumber++;
86:
87: //无论我们是否要打印更多的页面,都通知打印机
88: if(this._currentPrintPageNumber <= document.PrinterSettings.ToPage)
89: {
90: e.HasMorePages = true;
91: }
92: else
93: {
94: e.HasMorePages = false;
95: }
96: }
97:
98: private void _printDocument_EndPrint(object sender, PrintEventArgs e)
99: {
100: //此教程中此处不执行任何操作
101: }
编译并运行程序。结果如下图所示。