LeadTools中文入门教程(3):打印图像(上)

本文讲述了如何使用LeadTools创建一个具有“打印图像”功能的应用程序。

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_INSTALLDIRBinDotNetWin32”,选择以下的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 调整图像适应页面  

leadtools

10. 从工具箱(视图->工具箱)拖拽一个RasterImageViewer实例至窗体。若您的工具箱没有RasterImageViewer,点击工具->选择工具箱项…。点击浏览从“LEADTOOLS_INSTALLDIRBinDotNetWin32”中选择Leadtools.WinForms.DLL,点击打开并确定。修改“RasterImageViewer”的以下属性:

属性
Dock Fill
UseDpi True

11. 切换至Form1的代码视图(右击Form1,选择查看代码),将下面几行代码添加到文件开始处:

       using System.Drawing.Printing;       using Leadtools;       using Leadtools.Codecs;       using Leadtools.WinForms;

12. 为Form1添加以下私有变量:

      //加载图像时使用的 RasterCodecs 对象           private RasterCodecs _rasterCodecs;      //整个演示中我们会用到的 PrintDocument 对象      private PrintDocument _printDocument;      //当前要打印的页码        private int _currentPrintPageNumber;      //当前的图像文件名称      private string _currentImageFileName;

13. 为Form1添加以下代码:

 protected override void OnLoad(EventArgs e) {      // 在加载图像时初始化 RasterCodecs 对象      this._rasterCodecs = new RasterCodecs();            // 检查是否安装打印机              if(PrinterSettings.InstalledPrinters == null || PrinterSettings.InstalledPrinters.Count < 1)      {          MessageBox.Show(this,"此机器上未安装打印机,此程序无法继续运行","打印图像演示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);          this.Close();      }      else      {          //创建我们将要使用的print document对象                     this._printDocument = new PrintDocument();          //为打印事件添加句柄                     this._printDocument.BeginPrint += new PrintEventHandler(_printDocument_BeginPrint);          this._printDocument.PrintPage += new PrintPageEventHandler(_printDocument_PrintPage);          this._printDocument.EndPrint += new PrintEventHandler(_printDocument_EndPrint);      }            base.OnLoad(e); }       protected override void OnFormClosed(FormClosedEventArgs e) {      //释放我们使用的资源           if(this._printDocument != null)      {           this._printDocument.Dispose();      }      if(this._rasterCodecs != null)      {           this._rasterCodecs.Dispose();      }          base.OnFormClosed(e);  }

14. 将下列代码添加到fileToolStripMenuItem菜单项的DropDownOpening事件:

 private void fileToolStripMenuItem_DropDownOpening(object sender, EventArgs e) {     //更新UI状态     printPreviewToolStripMenuItem.Enabled = (rasterImageViewer1.Image != null);     printToolStripMenuItem.Enabled = (rasterImageViewer1.Image != null); }

15. 为openToolStripMenuItem按钮添加Click事件:

 private void openToolStripMenuItem_Click(object sender, EventArgs e) {       //将图像加载至查看器       using(OpenFileDialog dlg = new OpenFileDialog())       {             dlg.Filter = "All Files|*.*";             if(dlg.ShowDialog(this) == DialogResult.OK)             {                   try                   {                         //加载文件中的所有页面                                        rasterImageViewer1.Image = this._rasterCodecs.Load(dlg.FileName);                         this._currentImageFileName = dlg.FileName;                         UpdateCaption();                   }                   catch(Exception ex)                   {                         MessageBox.Show(this, ex.Message, "打印图像演示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                   }             }       } }

DEMO下载:

PrintImageTutorial.zip

本文是“打印图像”教程的上半部分,查看完整教程,请点击教程下半部分:LeadTools中文入门教程(3):打印图像(下) 。

如需帮助,请联系在线客服!

标签:图像缩放图像处理图像打印

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2016年6月8日
下一篇 2016年6月8日

相关推荐

发表回复

登录后才能评论