- LeadTools中文入门教程(1):加载、显示和保存图像 点击进入
- LeadTools中文入门教程(2):缩放图像 点击进入
- LeadTools中文入门教程(3):打印图像 点击进入
- LeadTools中文入门教程(4):使用OCR识别英文 点击进入
- LeadTools中文入门教程(5):读取和编写条形码 点击进入
- LeadTools中文入门教程(6):从TWAIN源获取图像 点击进入
- LeadTools中文入门教程(7):使用OCR识别图像中的中文 点击进入
- LeadTools中文入门教程(8):使用OCR识别扫描文件中的中文 点击进入
LeadTools图像处理开发工具,在医学、DICOM、PACS、栅格、矢量和多媒体图像处理领域都处于世界领先的地位。LeadTools包括Raster Imaging、Document Imaging、Medical Imaging和Multimedia Imaging四个产品系列。
下面就让我们使用LeadTools创建一个具有“打印图像”功能的应用程序,具体步骤如下:
1. 打开Visual Studio .NET。
2. 点击 文件->新建->项目…。
3. 打开新建项目对话框后,在模板中选择“Visual C#”或“Visual Basic”,随后选择“Windows窗体应用程序”。在名称栏中输入项目名称“PrintImageTutorial”,并使用“浏览”按钮选择您工程的存储路径,点击“确定”。
4. 在“解决方案资源管理器”中,右击“引用”,选择“添加引用”。在“引用管理器”中,浏览选择Leadtools For .NET文件夹” <LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32”,选择以下的DLL:
- 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部分。
5.由于我们将使用Win32(x86)程序集引用,因此我们必须将这个项目的build目标改为x86。选择
项目->PrintImageTutorial 属性,按照以下步骤设置:
- 若为C#工程,选择生成选项栏,在配置下拉框选择“所有配置”,在目标平台下拉框选择“x86” 。
- 若为VB工程,选择编译选项卡,在配置下拉框选择“所有配置”。点击高级编译选项按钮,在目标CPU中选择“x86”。点击确定关闭对话框。
6.将Form1调整到设计视图,从工具箱(视图->工具箱)拖拽一个MenuStrip控件到窗体。点击新的menuStrip1控件,添加以下顶级菜单项:
| 类型 | Name | Text | Shortcut Keys |
| ToolStripMenuItem | fileToolStripMenuItem | 文件 | |
| ToolStripMenuItem | pageToolStripMenuItem | 页面 | |
| ToolStripMenuItem | optionsToolStripMenuItem | 选项 |
7. 选择“文件”菜单项添加以下子项:
| 类型 | Name | Text | Shortcut Keys |
| ToolStripMenuItem | openToolStripMenuItem | 打开... | Ctrl+O |
| ToolStripSeparator | toolStripMenuItem1 | ||
| ToolStripMenuItem | printPreviewToolStripMenuItem | 打印预览 | |
| ToolStripMenuItem | printSetupToolStripMenuItem | 打印设置... | |
| ToolStripMenuItem | printToolStripMenuItem | 打印... | Ctrl+P |
| ToolStripSeparator | toolStripMenuItem2 | ||
| ToolStripMenuItem | exitToolStripMenuItem | 退出 |
8. 选择“页面”菜单项添加以下子项:
| 类型 | Name | Text | Shortcut Keys |
| ToolStripMenuItem | firstPageToolStripMenuItem | 首页 | |
| ToolStripMenuItem | previousPageToolStripMenuItem | 上一页 | |
| ToolStripMenuItem | nextPageToolStripMenuItem | 下一页 | |
| ToolStripMenuItem | lastPageToolStripMenuItem | 尾页 |
9. 选择“选项”菜单项添加以下子项:
| 类型 | Name | Text | Shortcut Keys |
| ToolStripMenuItem | usePageMarginsToolStripMenuItem | 使用页边距 | |
| ToolStripMenuItem | fitImageToPageToolStripMenuItem | 调整图像适应页面 |
10. 从工具箱(视图->工具箱)拖拽一个RasterImageViewer实例至窗体。若您的工具箱没有RasterImageViewer,点击工具->选择工具箱项…。点击浏览从“<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32”中选择Leadtools.WinForms.DLL,点击打开并确定。修改“RasterImageViewer”的以下属性:
| 属性 | 值 |
| Dock | Fill |
| UseDpi | True |
11. 切换至Form1的代码视图(右击Form1,选择查看代码),将下面几行代码添加到文件开始处:
1: [C#]2:3: using System.Drawing.Printing;4: using Leadtools;5: using Leadtools.Codecs;6: using Leadtools.WinForms;
12. 为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;
13. 为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: else15: {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: }
14. 将下列代码添加到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: }
15. 为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: try12: {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: }
16. 添加以下代码至printPreviewToolStripMenuItem菜单项的Click事件:
1: [C#]2:3: private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)4: {5: //设置打印文档6: SetupPrintDocument();7:8: // 使用 .NET PrintPreviewDialog9: using(PrintPreviewDialog dlg = new PrintPreviewDialog())10: {11: // 显示对话框12: dlg.Document = this._printDocument;13: dlg.WindowState = FormWindowState.Maximized;14: dlg.ShowDialog(this);15: }16: }
17. 将以下代码添加到printSetupToolStripMenuItem菜单项的Click事件中:
1: [C#]2:3: private void printSetupToolStripMenuItem_Click(object sender, EventArgs e)4: {5: // 使用.NET PageSetupDialog6: using(PageSetupDialog dlg = new PageSetupDialog())7: {8: dlg.Document = this._printDocument;9: dlg.ShowDialog(this);10: }11: }
18. 将以下代码添加到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: }
19. 将以下代码添加到exitToolStripMenuItem菜单项的Click事件中:
1: [C#]2:3: private void exitToolStripMenuItem_Click(object sender, EventArgs e)4: {5: // 关闭应用程序6: Close();7: }
20. 将以下代码添加到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: }
21. 将以下代码添加到firstPageToolStripMenuItem菜单项的Click事件中:
1: [C#]2: private void firstPageToolStripMenuItem_Click(object sender, EventArgs e)3: {4: //转到图像的首页5: rasterImageViewer1.Image.Page = 1;6: UpdateCaption();7: }
22. 将以下代码添加到previousPageToolStripMenuItem菜单项的Click事件中:
1: [C#]2:3: private void previousPageToolStripMenuItem_Click(object sender, EventArgs e)4: {5: // 转到图像的上一页6: rasterImageViewer1.Image.Page--;7: UpdateCaption();8: }
23. 将以下代码添加到nextPageToolStripMenuItem菜单项的Click事件中:
1: [C#]2:3: private void nextPageToolStripMenuItem_Click(object sender, EventArgs e)4: {5: // 转到图像的下一页6: rasterImageViewer1.Image.Page++;7: UpdateCaption();8: }
24. 将以下代码添加到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: }
25. 将以下代码添加到usePageMarginsToolStripMenuItem菜单项的Click事件中:
1: [C#]2:3: private void usePageMarginsToolStripMenuItem_Click(object sender, EventArgs e)4: {5: // 切换选项6: usePageMarginsToolStripMenuItem.Checked = !usePageMarginsToolStripMenuItem.Checked;7: }
26. 将以下代码添加到fitImageToPageToolStripMenuItem菜单项的Click事件中:
1: [C#]2:3: private void fitImageToPageToolStripMenuItem_Click(object sender, EventArgs e)4: {5: // 切换选项6: fitImageToPageToolStripMenuItem.Checked = !fitImageToPageToolStripMenuItem.Checked;7: }
27. 将以下代码添加到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: else15: {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: else63: {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: else93: {94: e.HasMorePages = false;95: }96: }97:98: private void _printDocument_EndPrint(object sender, PrintEventArgs e)99: {100: //此教程中此处不执行任何操作101: }
28. 编译并运行程序。结果如下图所示。
提醒:若您遇到"Invalid File Format" 或"Feature Not Supported"异常,请查阅参考文档Invalid File Format/Feature Not Supported.
DEMO下载:
本文Demo提供了使用LeadTools打印图像的全部代码。为了运行Demo代码,你需要下载LeadTools全功能试用版。
若您在使用LeadTools的过程中遇到任何问题,欢迎在葡萄城开发者社区LeadTools板块提问,我们的专业技术团队期待您的到来!