创建一个自定义注释——WPF c#

本教程演示如何创建一个WPF c#应用程序创建一个定制的注释使用LEADTOOLS SDK。

概述
总结 本教程演示了如何使用c# LEADTOOLS定制注释技术WPF应用程序。
完成时间 45分钟
Visual Studio项目 下载教程项目(12 KB)
平台 WPF c#应用程序
IDE Visual Studio 2017中,2019年
开发许可 下载LEADTOOLS
试试用另一种语言

所需的知识

在工作之前创建一个自定义注释——WPF c#教程中,熟悉的基本步骤,创建一个项目通过审查添加引用和设置一个许可证教程。

创建项目并添加LEADTOOLS引用

从项目中创建的副本添加引用和设置一个许可证教程。如果你没有这个项目,按照这个教程中的步骤创建它。

的引用需要取决于项目的目的。引用可以通过添加一个或另一个下面的两种方法(但不是全部)。

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

如果使用本地DLL的引用,下面的DLL是必要的。dll位于< INSTALL_DIR > \ LEADTOOLS22 \ Bin \ Dotnet4 \ x64:

对于一个完整的列表的编解码器dll需要特定的格式,请参考文件格式的支持

设置许可文件

许可解锁项目需要的特性。它必须设置工具箱函数被调用之前。详情,包括教程为不同的平台,请参考[设置一个运行时许可

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

请注意

添加LEADTOOLS NuGet和本地引用设置许可中会详细介绍添加引用和设置一个许可证教程。

设置主窗口XAML

与项目创建、添加引用,许可,可以开始编码。

解决方案资源管理器,打开MainWindow.xaml文件。添加以下XAML代码正确显示UI元素。

<窗口x:类= " Custom_Annotation_WPF.MainWindow "xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x = " http://schemas.microsoft.com/winfx/2006/xaml "xmlns: d = " http://schemas.microsoft.com/expression/blend/2008 "xmlns: mc = " http://schemas.openxmlformats.org/markup-compatibility/2006 "xmlns:当地= " clr-namespace: Custom_Annotation_WPF”主持人:可忽略的= " d "Title =“主窗口”= =“1000”“1000”宽度高度加载= " MainWindow_Load "><网格名称= " imageViewerGrid "><Grid.RowDefinitions><RowDefinition高度=“自动”></ RowDefinition><RowDefinition高度= " * "></ RowDefinition></ Grid.RowDefinitions></网格></窗口>

我们定义2行,作为容器的注释工具栏和图片查看器。你可以调整高度宽度如图所示,如果你的欲望,但本教程并不重要。

初始化图像查看器

点击事件图标属性窗口。然后,双击负载创建一个事件处理程序。使用语句中添加以下使用块的顶部MainWindow.xaml.cs

c#
/ /使用块使用系统;使用先;使用System.Windows;使用System.Windows.Controls;使用System.Windows.Media.Imaging;使用Leadtools;使用Leadtools.Codecs;使用Leadtools.Controls;使用Leadtools.Annotations.Engine;使用Leadtools.Annotations.Automation;使用Leadtools.Annotations.Designers;使用Leadtools.Annotations.Rendering;使用Leadtools.Annotations.Wpf;

添加下面的全局变量:

c#
/ /声明这些全球成员私人imageview imageview;私人RasterCodecs编解码器;私人AutomationInteractiveMode annInteractiveMode;私人IAnnAutomationControl automationControl;私人AnnAutomationManager annManager;私人AnnAutomation AnnAutomation;

将下面的代码添加到MainWindow_Load事件处理程序初始化imageview并创建一个新的空白RasterImage

c#
私人无效MainWindow_Load (对象发送方的EventArgs e){/ /初始化imageview对象imageview =imageview{背景= SystemColors.AppWorkspaceBrush,UseDpi =真正的,ViewPadding =ControlPadding (),ViewHorizontalAlignment = ControlAlignment.Center,ViewVerticalAlignment = ControlAlignment.Center,};imageViewerGrid.Children.Add (imageview);网格。SetRow (imageview, 1);imageViewer.BringIntoView ();/ /创建一个纯白色背景画imageview。形象= RasterImage。96年创建(1000、1000、24日,RasterColor.White);/ /初始化对象编解码器编解码器=RasterCodecs ();}

添加工具栏图标资源

解决方案资源管理器,下拉属性选项卡,双击Resources.resx

打开资源文件

点击添加资源然后点击添加现有文件…

添加现有的项目

下载下面的圆圈图标PNG图像,然后浏览到的位置,然后单击开放

圆形图标

资源已经导入后,将创建一个项目资源文件夹中。

这张图片需要嵌入到该项目。为此,掉下来资源文件夹中解决方案资源管理器,然后单击Create-a-Custom-Annotation-Circle-Icon.png。在属性改变建立行动嵌入的资源。

嵌入自定义注释资源

创建自定义注释对象类

解决方案资源管理器,右键单击项目,突出添加,然后单击新文件夹。重命名文件夹AnnCircleObject

创建循环对象文件夹

解决方案资源管理器,右键单击AnnCircleObject文件夹,突出添加,然后单击类……

添加类

添加四个新类:

请注意

一定要改变这些类的名称空间为每个匹配的名称空间MainWindow.xaml.cs

将以下代码添加到AnnCircleEditDesigner类:

c#
使用系统;使用Leadtools;使用Leadtools.Annotations.Engine;使用Leadtools.Annotations.Designers;名称空间Custom_Annotation_WPF{AnnCircleEditDesigner: AnnRectangleEditDesigner{私人AnnCircleObject圈子;私人LeadPointD开始= LeadPointD.Empty;私人LeadPointD结束= LeadPointD.Empty;私人LeadPointD anchorThumb = LeadPointD.Empty;私人LeadPointD currentThumb = LeadPointD.Empty;私人LeadPointD leftThumb = LeadPointD.Empty;私人LeadPointD rightThumb = LeadPointD.Empty;私人LeadPointD topThumb = LeadPointD.Empty;私人LeadPointD bottomThumb = LeadPointD.Empty;私人LeadPointD中心= LeadPointD.Empty;私人intradiusModX;私人intradiusModY;私人半径;/ / /<摘要>/ / /构造函数的AnnCircleEditDesigner/ / /> < /总结公共AnnCircleEditDesigner (IAnnAutomationControl automationControl, AnnContainer AnnContainer, AnnRectangleObject annRectangle):基地(annContainer automationControl annRectangle) {}/ / /<摘要>/ / /覆盖的指针事件/ / /设置开始和结束点,第一次点击的位置/ / /> < /总结公共覆盖boolOnPointerDown (AnnContainer发送方,AnnPointerEventArgs e){圆= (AnnCircleObject) TargetObject;开始= circle.Bounds.TopLeft;= circle.Rect半径。宽/ 2;中心= circle.RotateCenter;leftThumb = LeadPointD.Create(中心。X -半径,center.Y);rightThumb = LeadPointD.Create(中心。X +半径,center.Y);topThumb = LeadPointD.Create(中心。X,中心。Y -半径);bottomThumb = LeadPointD.Create(中心。X,中心。Y +半径);currentThumb = LeadPointD.Empty;anchorThumb = LeadPointD.Empty;radiusModX = 0;radiusModY = 0;返回基地.OnPointerDown(发送方,e);}/ / /<摘要>/ / /覆盖的指针移动事件/ / /设置新的鼠标点到变量/ / /做一些数学对象创建一个圆(宽/高保持相等)/ / /> < /总结公共覆盖boolOnPointerMove (AnnContainer发送方,AnnPointerEventArgs e){圆= (AnnCircleObject) TargetObject;如果(= = AnnEditDesignerOperation.MoveThumb操作){结束= e.Location;/ /设置锚点如果(MoveThumbIndex ! = 1 & & currentThumb.IsEmpty) currentThumb = GetThumbLocations () (MoveThumbIndex);如果(currentThumb = = leftThumb){如果(anchorThumb.IsEmpty) anchorThumb = rightThumb;radiusModX =(结束。X > anchorThumb.X) ?1:1;}如果(currentThumb = = rightThumb){如果(anchorThumb.IsEmpty) anchorThumb = leftThumb;radiusModX =(结束。X < anchorThumb.X) ?1:1;}如果(currentThumb = = topThumb){如果(anchorThumb.IsEmpty) anchorThumb = bottomThumb;radiusModY =(结束。Y > anchorThumb.Y) ?1:1;}如果(currentThumb = = bottomThumb){如果(anchorThumb.IsEmpty) anchorThumb = topThumb;radiusModY =(结束。Y < anchorThumb.Y) ?1:1;}/ /获取左上角半径= Math.Sqrt (Math.Pow(结束。X - anchorThumb.X 2) + Math.Pow(结束。Y - anchorThumb.Y, 2)) / 2;= LeadPointD.Create (anchorThumb中心。anchorThumb X +(半径* radiusModX)。Y +(半径* radiusModY));= LeadPointD.Create(中心开始。X -半径,中心。Y -半径);/ /做的事情圆。矩形= LeadRectD.Create(开始。X,开始。Y, * 2半径,半径* 2);无效(circle.Rect);ResetRotateThumbs ();返回真正的;}返回基地.OnPointerMove(发送方,e);}公共覆盖boolOnPointerUp (AnnContainer发送方,AnnPointerEventArgs e){返回基地.OnPointerUp(发送方,e);}}}

将以下代码添加到AnnCircleDrawDesigner类:

c#
使用系统;使用Leadtools;使用Leadtools.Annotations.Engine;使用Leadtools.Annotations.Designers;名称空间Custom_Annotation_WPF{AnnCircleDrawDesigner: AnnRectangleDrawDesigner{/ /我们需要2点,一个开始和一个结束点私人LeadPointD开始= LeadPointD.Empty;私人LeadPointD结束= LeadPointD.Empty;/ / /<摘要>/ / /构造函数的AnnCircleDrawDesigner/ / /> < /总结公共AnnCircleDrawDesigner (IAnnAutomationControl automationControl AnnContainer容器,AnnCircleObject annObject):基地(annObject automationControl、集装箱){}/ / /<摘要>/ / /覆盖的指针事件/ / /设置开始和结束点,第一次点击的位置/ / /> < /总结公共覆盖boolOnPointerDown (AnnContainer发送方,AnnPointerEventArgs e){如果(begin.IsEmpty)开始= e.Location;返回基地.OnPointerDown(发送方,e);}/ / /<摘要>/ / /覆盖的指针移动事件/ / /设置新的鼠标点到变量/ / /做一些数学对象创建一个圆(宽/高保持相等)/ / /> < /总结公共覆盖boolOnPointerMove (AnnContainer发送方,AnnPointerEventArgs e){如果(pointerDown){结束= e.Location;AnnCircleObject圆= (AnnCircleObject) TargetObject;x = Math.Abs(结束。X - begin.X);y = Math.Abs(结束。Y - begin.Y);scaleX = 1;写入scaleY = 1;如果(x < y) scaleX = x ! = 0 ?y / x: 0;其他的写入scaleY = y ! = 0 ?x / y: 0;/ /得到高度和宽度宽度= Math.Abs(结束。X - begin.X) * Math.Abs (scaleX);身高= Math.Abs(结束。Y - begin.Y) * Math.Abs(写入scaleY);/ /容器边界限制如果(开始。Y +高度> sender.Bounds.Height)高度= sender.Bounds。高度- begin.Y;如果(开始。X +宽度> sender.Bounds.Width)宽度= sender.Bounds。宽度——begin.X;/ /确保圆仍然是一个圆如果(宽度<高度)高度=宽度;如果(高度<宽度)宽度=高度;/ /做的事情圆。矩形= LeadRectD.Create(开始。X,开始。Y,宽度、高度);无效(circle.Rect);}返回真正的;}公共覆盖boolOnPointerUp (AnnContainer发送方,AnnPointerEventArgs e){开始= LeadPointD.Empty;返回基地.OnPointerUp(发送方,e);}}}

将以下代码添加到AnnCircleObject类:

c#
使用Leadtools.Annotations.Engine;名称空间Create_a_Custom_Annotation{AnnCircleObject: AnnEllipseObject{/ /设置UserObjectID id公共常量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_Annotation{AnnCircleObjectRenderer: AnnEllipseObjectRenderer{/ / /<摘要>/ / /渲染器的构造函数/ / /得到了椭圆对象渲染器和循环使用相同的风格/ / /> < /总结公共AnnCircleObjectRenderer (AnnAutomationManager经理):基地(){IAnnObjectRenderer annEllipseObjRenderer = manager.RenderingEngine.Renderers [AnnObject.EllipseObjectId];LabelRenderer = annEllipseObjRenderer.LabelRenderer;LocationsThumbStyle = annEllipseObjRenderer.LocationsThumbStyle;RotateCenterThumbStyle = annEllipseObjRenderer.RotateCenterThumbStyle;RotateGripperThumbStyle = annEllipseObjRenderer.RotateGripperThumbStyle;/ /下面的代码片段改变注释的缩略图的大小/ / LeadSizeD newThumbSize = LeadSizeD。创建(300、300);/ /新的缩略图的大小,改变是必要的/ / LocationsThumbStyle。大小= newThumbSize;/ / RotateCenterThumbStyle。大小= newThumbSize;/ / RotateGripperThumbStyle。大小= newThumbSize;}/ / /<摘要>/ / /覆盖RenderThumbs方法/ / /通过拇指和删除顶部底部左和右拇指所以不能改变从一个圆/ / /> < /总结公共覆盖无效RenderThumbs (AnnContainerMapper mapper LeadPointD [] thumbLocations, AnnFixedStateOperations操作){列出< LeadPointD > newThumbs =列表< LeadPointD > ();(int我= 0;我< thumbLocations.Length;我+ = 2)newThumbs.Add (thumbLocations[我]);基地.RenderThumbs (mapper newThumbs.ToArray()操作);}}}

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

右键单击MainWindow.xaml解决方案资源管理器并选择视图代码以弹出形式背后的代码。添加一个新方法InitAnnotations (),电话里的新方法MainWindow_Load下面的事件处理程序imageViewer.BringIntoView ()

c#
私人无效MainWindow_Load (对象发送方的EventArgs e){/ /初始化imageview对象imageview =imageview{背景= SystemColors.AppWorkspaceBrush,UseDpi =真正的,ViewPadding =ControlPadding (),ViewHorizontalAlignment = ControlAlignment.Center,ViewVerticalAlignment = ControlAlignment.Center,};imageViewerGrid.Children.Add (imageview);网格。SetRow (imageview, 1);imageViewer.BringIntoView ();InitAnnotations ();/ /创建一个纯白色背景画imageview。形象= RasterImage。96年创建(1000、1000、24日,RasterColor.White);/ /初始化对象编解码器编解码器=RasterCodecs ();}

在新添加以下代码InitAnnotations ()方法:

c#
无效InitAnnotations (){/ / AnnManager初始化annManager =AnnAutomationManager{RestrictDesigners =真正的,RenderingEngine =AnnWPFRenderingEngine ()};/ /创建我的自定义对象CreateCustomObject (annManager);/ /创建助手和工具栏,然后将它们添加到表单AutomationManagerHelper annHelper =AutomationManagerHelper (annManager);annHelper.CreateToolBar ();imageViewerGrid.Children.Add (annHelper.ToolBar);Grid.SetRow (annHelper。工具栏,0);/ /创建自动化控制和附加观众automationControl =ImageViewerAutomationControl ();((ImageViewerAutomationControl) automationControl)。imageview = imageview;/ /初始化交互模式,并将其添加到查看器annInteractiveMode =AutomationInteractiveMode{AutomationControl = AutomationControl};imageViewer.InteractiveModes.BeginUpdate ();imageViewer.InteractiveModes.Add (annInteractiveMode);imageViewer.InteractiveModes.EndUpdate ();/ /初始化自动化annAutomation =AnnAutomation (annManager automationControl);/ /当加载一个新的形象,设置新的大小的容器中imageview。ItemChanged + = ImageViewer_ItemChanged;annAutomation。活跃的=真正的;}私人无效ImageViewer_ItemChanged (对象发送方,ImageViewerItemChangedEventArgs e){如果(e。原因= = ImageViewerItemChangedReason.Image)annAutomation.Container。大小= annAutomation.Container.Mapper.SizeToContainerCoordinates (imageViewer.ImageSize.ToLeadSizeD ());}

在添加一个新的方法Form1类命名CreateCustomObject (AnnAutomationManager annManager)。在调用此方法InitAnnotations ()方法如上所示。将以下代码添加到新方法:

c#
私人无效CreateCustomObject (AnnAutomationManager annManager){/ /创建圆形对象,分配设计师,设置工具栏图像,然后将其添加到管理器和渲染引擎varexeAsm = System.Reflection.Assembly.GetExecutingAssembly ();流流=;foreach(字符串resexeAsm.GetManifestResourceNames ())如果(res.Contains (“CircleIcon”)){流= exeAsm.GetManifestResourceStream (res);打破;}BitmapImage BitmapImage =BitmapImage ();bitmapImage.BeginInit ();bitmapImage。StreamSource =流;bitmapImage.EndInit ();AnnAutomationObject circleAutomationObject =AnnAutomationObject{Id = AnnCircleObject.CircleObjectId,Name =“圆”,ToolBarToolTipText =名字,DrawDesignerType =typeof(AnnCircleDrawDesigner),EditDesignerType =typeof(AnnCircleEditDesigner),RunDesignerType =typeof(AnnRunDesigner),ObjectTemplate =AnnCircleObject (),ToolBarImage = bitmapImage,};annManager.Objects.Add (circleAutomationObject);annManager.RenderingEngine.Renderers.Add (AnnCircleObject.CircleObjectIdAnnCircleObjectRenderer (annManager));}

运行该项目

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

如果是正确的步骤,应用程序应该运行并显示一个空白RasterImage。画的圆注释,单击左上角的按钮的形式。

最终结果

总结

本教程介绍了如何创建一个定制的循环使用LEADTOOLS注释SDK技术注释。

另请参阅

iOS
188金宝搏的网址客服|支持|联系我们|知识产权的通知
©1991 - 2023领先的技术公司。保留所有权利。