创建自定义注释- WinForms c#

本教程演示如何使用LEADTOOLS SDK创建WinForms c#应用程序来创建自定义注释。

概述
总结 本教程演示如何在c# WinForms应用程序中使用LEADTOOLS自定义注释技术。
完成时间 45分钟
Visual Studio项目 下载教程项目(13kb)
平台 WinForms c#应用程序
IDE Visual Studio 2017, 2019
开发许可 下载LEADTOOLS
用另一种语言试试
  • c#:.NET Framework (WinForms)WPF

所需的知识

在开始之前创建自定义注释- WinForms c#本教程中,通过回顾添加引用和设置License教程。

创建项目并添加LEADTOOLS引用

创建一个新的c# Windows Winforms项目,并添加以下必要的LEADTOOLS引用。

所需要的参考资料取决于项目的目的。引用可以通过以下两种方法中的一种添加(但不能同时添加)。本项目需要参考资料如下:

如果使用NuGet引用,本教程将需要以下NuGet包:

如果使用本地DLL引用,则需要以下DLL。dll位于< INSTALL_DIR > \ LEADTOOLS21 \ Bin \ Dotnet4 \ x64

有关特定格式所需的Codec dll的完整列表,请参阅文件格式支持

设置License文件

许可证解锁项目所需的特性。它必须在调用任何工具箱函数之前设置。有关包括针对不同平台的教程的详细信息,请参阅设置运行时License

有两种类型的运行时许可证:

请注意

中详细介绍了添加LEADTOOLS NuGet和本地引用以及设置许可证添加引用和设置License

初始化图像查看器

随着项目的创建、引用的添加和许可的设置,编码就可以开始了。右键单击Form1.cs解决方案资源管理器,并选择视图代码来显示表单后面的代码。在Using块中添加以下代码并声明全局成员。

c#
//在顶部使用块使用系统;使用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;
c#
//声明这些全局成员私人imageview imageview;私人RasterCodecs编解码器;私人AutomationInteractiveMode annInteractiveMode;私人IAnnAutomationControl automationControl;私人AnnAutomationManager annManager;私人AnnAutomation AnnAutomation;

解决方案资源管理器,双击Form1.cs要在设计器中显示它,请单击事件图标。属性窗口.然后,双击负载事件来创建事件处理程序(如果不存在)。

创建一个加载事件

控件中添加以下代码Form1_Load事件处理程序来初始化imageview,创建一个新的空白RasterImage,并在查看器中进行设置。

c#
私人无效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文件夹,突出添加,并单击类……

添加类

添加三个新类:

请注意

确保为每个类更改名称空间以匹配中的名称空间Form1.cs

将下面的代码添加到AnnCircleDrawDesigner类:

c#
使用系统;使用Leadtools;使用Leadtools.Annotations.Engine;使用Leadtools.Annotations.Designers;名称空间Create_a_Custom_AnnotationAnnCircleDrawDesigner: 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类:

c#
使用Leadtools.Annotations.Engine;名称空间Create_a_Custom_AnnotationAnnCircleObject: AnnEllipseObject//设置id为UserObjectID公共常量intCircleObjectId = UserObjectId;///<摘要>///对象的构造函数。///将对象的id设置为圆形对象id///> < /总结公共AnnCircleObject ()基地()SetId (CircleObjectId);标签=受保护的覆盖AnnObject Create ()返回AnnCircleObject ();

将下面的代码添加到AnnCircleObjectRenderer类:

c#
使用System.Collections.Generic;使用Leadtools.Annotations.Rendering;使用Leadtools.Annotations.Automation;使用Leadtools.Annotations.Engine;使用Leadtools;名称空间Create_a_Custom_AnnotationAnnCircleObjectRenderer: 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操作)List newThumbs =列表< LeadPointD > ();intI = 0;i < thumbLocations.Length;I += 2)newThumbs.Add (thumbLocations[我]);基地.RenderThumbs(mapper, newThumbs.ToArray(), operations);

初始化注释和创建自定义注释代码

右键单击Form1.cs解决方案资源管理器并选择视图代码来显示表单后面的代码。添加一个新方法,命名它InitAnnotations ()对象中的新方法Form1_Load事件处理程序如下imageViewer.BringToFront ()

c#
私人无效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 ()方法:

c#
无效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 ()方法,如上所示。将以下代码添加到新方法中:

c#
私人无效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.CircleObjectIdAnnCircleObjectRenderer (annManager));

运行项目

按下运行项目F5,或选择调试->开始调试

如果正确地执行了这些步骤,应用程序应该运行并显示空白RasterImage.若要绘制自定义圆注释,请单击窗体左上方的按钮。

最终结果

总结

本教程介绍了如何使用LEADTOOLS Annotations SDK技术创建自定义圆注释。

另请参阅

net
iOS
188金宝搏的网址客服|支持|联系我们|知识产权公告
©1991 - 2021领德科技有限公司版权所有。