本教程演示如何使用LEADTOOLS SDK创建WinForms c#应用程序来创建自定义注释。
| 概述 | |
|---|---|
| 总结 | 本教程演示如何在c# WinForms应用程序中使用LEADTOOLS自定义注释技术。 |
| 完成时间 | 45分钟 |
| Visual Studio项目 | 下载教程项目(13kb) |
| 平台 | WinForms c#应用程序 |
| IDE | Visual Studio 2017, 2019 |
| 开发许可 | 下载LEADTOOLS |
| 用另一种语言试试 |
|
在开始之前创建自定义注释- WinForms c#本教程中,通过回顾添加引用和设置License教程。
创建一个新的c# Windows Winforms项目,并添加以下必要的LEADTOOLS引用。
所需要的参考资料取决于项目的目的。引用可以通过以下两种方法中的一种添加(但不能同时添加)。本项目需要参考资料如下:
如果使用NuGet引用,本教程将需要以下NuGet包:
Leadtools.Annotations.WinFormsLeadtools.Viewer.Controls.WinForms如果使用本地DLL引用,则需要以下DLL。dll位于< INSTALL_DIR > \ LEADTOOLS21 \ Bin \ Dotnet4 \ x64:
Leadtools.dllLeadtools.Annotations.Automation.dllLeadtools.Annotations.Designers.dllLeadtools.Annotations.Engine.dllLeadtools.Annotations.Rendering.WinForms.dllLeadtools.Annotations.WinForms.dllLeadtools.Codecs.dllLeadtools.Codecs.Cmp.dllLeadtools.Controls.WinForms.dllLeadtools.Drawing.dllLeadtools.WinForms.CommonDialogs.File.dll有关特定格式所需的Codec dll的完整列表,请参阅文件格式支持.
许可证解锁项目所需的特性。它必须在调用任何工具箱函数之前设置。有关包括针对不同平台的教程的详细信息,请参阅设置运行时License.
有两种类型的运行时许可证:
请注意
中详细介绍了添加LEADTOOLS NuGet和本地引用以及设置许可证添加引用和设置License.
随着项目的创建、引用的添加和许可的设置,编码就可以开始了。右键单击Form1.cs在解决方案资源管理器,并选择视图代码来显示表单后面的代码。在Using块中添加以下代码并声明全局成员。
//在顶部使用块使用系统;使用System.Drawing;使用System.Windows.Forms;使用Leadtools;使用Leadtools.Codecs;使用Leadtools.Controls;使用Leadtools.Annotations.Engine;使用Leadtools.Annotations.Automation;使用Leadtools.Annotations.Designers;使用Leadtools.Annotations.Rendering;使用Leadtools.Annotations.WinForms;
//声明这些全局成员私人imageview imageview;私人RasterCodecs编解码器;私人AutomationInteractiveMode annInteractiveMode;私人IAnnAutomationControl automationControl;私人AnnAutomationManager annManager;私人AnnAutomation AnnAutomation;
在解决方案资源管理器,双击Form1.cs要在设计器中显示它,请单击事件图标。属性窗口.然后,双击负载事件来创建事件处理程序(如果不存在)。
控件中添加以下代码Form1_Load事件处理程序来初始化imageview,创建一个新的空白RasterImage,并在查看器中进行设置。
私人无效Form1_Load (对象发送者,EventArgs{初始化imageViewer对象imageview =新imageview ();imageview。Dock = DockStyle.Fill;imageview。BackColor = color .深灰色;Controls.Add (imageview);imageViewer.BringToFront ();//创建一个纯白色的背景来绘制imageview。Image = RasterImage。创建(1000,1000,24,96,RasterColor.White);//初始化codecs对象编解码器=新RasterCodecs ();}
在解决方案资源管理器,往下掉属性选项卡,双击Resources.resx.
点击添加资源然后点击添加现有文件….
下载下面的圆形图标PNG图像,浏览到该位置并单击开放.
导入资源之后,项目将创建一个资源文件夹中。
这个图像需要嵌入到项目中。为此,请导航到解决方案资源管理器,往下掉资源文件夹,然后单击Create-a-Custom-Annotation-Circle-Icon.png.在属性中,更改建立行动到嵌入式资源。
在解决方案资源管理器,右键单击项目,突出添加,并单击创建文件夹.将文件夹重命名为AnnCircleObject.
在解决方案资源管理器,右键单击AnnCircleObject文件夹,突出添加,并单击类…….
添加三个新类:
AnnCircleDrawDesigner.csAnnCircleObject.csAnnCircleObjectRenderer.cs请注意
确保为每个类更改名称空间以匹配中的名称空间
Form1.cs.
将下面的代码添加到AnnCircleDrawDesigner类:
使用系统;使用Leadtools;使用Leadtools.Annotations.Engine;使用Leadtools.Annotations.Designers;名称空间Create_a_Custom_Annotation{类AnnCircleDrawDesigner: AnnRectangleDrawDesigner{//我们需要两个点,起点和终点私人LeadPointD begin = leadpoint . empty;私人LeadPointD end = leadpoint . empty;///<摘要>///AnnCircleDrawDesigner的构造函数///> < /总结公共AnnCircleDrawDesigner(IAnnAutomationControl automationControl, AnnContainer container, AnnCircleObject annObject):基地(automationControl, container, annObject) {}///<摘要>///重写指针向下事件///将开始点和结束点设置为第一次单击的位置///> < /总结公共覆盖保龄球OnPointerDown(AnnContainer sender, AnnPointerEventArgs e){begin = e.位置;结束=开始;返回基地.OnPointerDown(发送方,e);}///<摘要>///重写指针移动事件///将新的鼠标点设置为结束变量///做一些计算来创建一个圆对象(宽度/高度保持相等)///> < /总结公共覆盖保龄球OnPointerMove(AnnContainer sender, AnnPointerEventArgs e){end = e.位置;AnnCircleObject circle = (AnnCircleObject)目标对象;双X = (end。X - start .X);双y = Math.Abs(结束。Y - start .Y);双scaleX = 1;双scaleY = 1;如果(x < y)scaleX = y / x;其他的scaleY = x / y;圆。Rect = LeadRectD.Create(开始。X,开始。Y, Math.Abs(结束。X - start .X) * Math.Abs(scaleX), Math.Abs(结束。Y - start .Y) * Math.Abs(scaleY));无效(LeadRectD.Empty);返回真正的;}}}
将下面的代码添加到AnnCircleObject类:
使用Leadtools.Annotations.Engine;名称空间Create_a_Custom_Annotation{类AnnCircleObject: AnnEllipseObject{//设置id为UserObjectID公共常量intCircleObjectId = UserObjectId;///<摘要>///对象的构造函数。///将对象的id设置为圆形对象id///> < /总结公共AnnCircleObject ():基地(){SetId (CircleObjectId);标签=零;}受保护的覆盖AnnObject Create (){返回新AnnCircleObject ();}}}
将下面的代码添加到AnnCircleObjectRenderer类:
使用System.Collections.Generic;使用Leadtools.Annotations.Rendering;使用Leadtools.Annotations.Automation;使用Leadtools.Annotations.Engine;使用Leadtools;名称空间Create_a_Custom_Annotation{类AnnCircleObjectRenderer: AnnEllipseObjectRenderer{///<摘要>///呈现器的构造函数///获取椭圆对象渲染器,并对圆使用相同的样式///> < /总结公共AnnCircleObjectRenderer (AnnAutomationManager经理):基地(){IAnnObjectRenderer annEllipseObjRenderer = manager.RenderingEngine.Renderers[AnnObject.EllipseObjectId];LabelRenderer = annEllipseObjRenderer.LabelRenderer;LocationsThumbStyle = annEllipseObjRenderer.LocationsThumbStyle;RotateCenterThumbStyle = annEllipseObjRenderer.RotateCenterThumbStyle;RotateGripperThumbStyle = annEllipseObjRenderer.RotateGripperThumbStyle;//下面的代码片段改变注释的缩略图大小//铅条大小newThumbSize =铅条大小。创建(300、300);//缩略图的新大小,根据需要进行更改/ / LocationsThumbStyle。大小= newThumbSize;/ / RotateCenterThumbStyle。大小= newThumbSize;/ / RotateGripperThumbStyle。大小= newThumbSize;}///<摘要>///重写RenderThumbs方法///穿过拇指,去掉顶部,底部,左边和右边的拇指,这样就不能从一个圆圈改变///> < /总结公共覆盖无效RenderThumbs(AnnContainerMapper mapper, LeadPointD[] thumbLocations, AnnFixedStateOperations操作){ListnewThumbs =新列表< LeadPointD > (); 为(intI = 0;i < thumbLocations.Length;I += 2)newThumbs.Add (thumbLocations[我]);基地.RenderThumbs(mapper, newThumbs.ToArray(), operations);}}}
右键单击Form1.cs在解决方案资源管理器并选择视图代码来显示表单后面的代码。添加一个新方法,命名它InitAnnotations ()对象中的新方法Form1_Load事件处理程序如下imageViewer.BringToFront ().
私人无效Form1_Load (对象发送者,EventArgs{初始化imageViewer对象imageview =新imageview ();imageview。Dock = DockStyle.Fill;imageview。BackColor = color .深灰色;Controls.Add (imageview);imageViewer.BringToFront ();InitAnnotations ();//创建一个纯白色的背景来绘制imageview。Image = RasterImage。创建(1000,1000,24,96,RasterColor.White);//初始化codecs对象编解码器=新RasterCodecs ();}
控件中添加以下代码InitAnnotations ()方法:
无效InitAnnotations (){//先初始化AnnManagerannManager =新AnnAutomationManager ();annManager。RestrictDesigners =真正的;annManager。RenderingEngine =新AnnWinFormsRenderingEngine ();//创建自定义对象CreateCustomObject (annManager);//创建助手和工具栏,然后将它们添加到表单中AutomationManagerHelper annHelper =新AutomationManagerHelper (annManager);annHelper.CreateToolBar ();Controls.Add (annHelper.ToolBar);//创建自动控制并将查看器附加到它automationControl =新ImageViewerAutomationControl ();((ImageViewerAutomationControl) automationControl)。ImageViewer = ImageViewer;初始化交互模式并将其添加到查看器annInteractiveMode =新AutomationInteractiveMode ();annInteractiveMode。AutomationControl = AutomationControl;imageViewer.InteractiveModes.BeginUpdate ();imageViewer.InteractiveModes.Add (annInteractiveMode);imageViewer.InteractiveModes.EndUpdate ();//初始化自动化annAutomation =新AnnAutomation (annManager automationControl);//当加载一个新的图像时,在容器中设置新的大小imageview。ItemChanged += ImageViewer_ItemChanged;annAutomation。活跃的=真正的;}私人无效ImageViewer_ItemChanged (对象sender, ImageViewerItemChangedEventArgs{如果(e.Reason == ImageViewerItemChangedReason.Image)annAutomation.Container.Size = annAutomation.Container.Mapper.SizeToContainerCoordinates(imageviewer . imagesize . toleadsize ());}
类中添加一个新方法Form1类命名CreateCustomObject (AnnAutomationManager annManager).方法中调用此方法InitAnnotations ()方法,如上所示。将以下代码添加到新方法中:
私人无效CreateCustomObject (AnnAutomationManager annManager){//创建circle对象,分配设计器,设置工具栏图像,然后将其添加到管理器和渲染引擎AnnAutomationObject circleAutomationObject =新AnnAutomationObject ();circleAutomationObject。Id = AnnCircleObject.CircleObjectId;circleAutomationObject。Name =“圆”;circleAutomationObject。ToolBarToolTipText = circleAutomationObject.Name;circleAutomationObject。DrawDesignerType =typeof(AnnCircleDrawDesigner);circleAutomationObject。EditDesignerType =typeof(AnnRectangleEditDesigner);circleAutomationObject。RunDesignerType =typeof(AnnRunDesigner);circleAutomationObject。ObjectTemplate =新AnnCircleObject ();circleAutomationObject。ToolBarImage =新位图(typeof(Form1),“Resources.Create-a-Custom-Annotation-Circle-Icon.png”);circleAutomationObject。快捷菜单=新Leadtools.Annotations.WinForms.ObjectContextMenu ();annManager.Objects.Add (circleAutomationObject);annManager.RenderingEngine.Renderers.Add (AnnCircleObject.CircleObjectId新AnnCircleObjectRenderer (annManager));}
按下运行项目F5,或选择调试->开始调试.
如果正确地执行了这些步骤,应用程序应该运行并显示空白RasterImage.若要绘制自定义圆注释,请单击窗体左上方的按钮。
本教程介绍了如何使用LEADTOOLS Annotations SDK技术创建自定义圆注释。