本教程演示如何创建一个WPF c#应用程序创建一个定制的注释使用LEADTOOLS SDK。
概述 | |
---|---|
总结 | 本教程演示了如何使用c# LEADTOOLS定制注释技术WPF应用程序。 |
完成时间 | 45分钟 |
Visual Studio项目 | 下载教程项目(12 KB) |
平台 | WPF c#应用程序 |
IDE | Visual Studio 2017中,2019年 |
开发许可 | 下载LEADTOOLS |
试试用另一种语言 |
|
在工作之前创建一个自定义注释——WPF c#教程中,熟悉的基本步骤,创建一个项目通过审查添加引用和设置一个许可证教程。
从项目中创建的副本添加引用和设置一个许可证教程。如果你没有这个项目,按照这个教程中的步骤创建它。
的引用需要取决于项目的目的。引用可以通过添加一个或另一个下面的两种方法(但不是全部)。
如果使用NuGet引用,本教程将需要以下NuGet包:
Leadtools.Annotations.Wpf
Leadtools.Viewer.Controls.Wpf
如果使用本地DLL的引用,下面的DLL是必要的。dll位于< INSTALL_DIR > \ LEADTOOLS22 \ Bin \ Dotnet4 \ x64
:
Leadtools.dll
Leadtools.Annotations.Automation.dll
Leadtools.Annotations.Designers.dll
Leadtools.Annotations.Engine.dll
Leadtools.Annotations.Rendering.Wpf.dll
Leadtools.Annotations.Wpf.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Controls.Wpf.dll
Leadtools.Drawing.dll
对于一个完整的列表的编解码器dll需要特定的格式,请参考文件格式的支持。
许可解锁项目需要的特性。它必须设置工具箱函数被调用之前。详情,包括教程为不同的平台,请参考[设置一个运行时许可。
有两种类型的运行时许可证:
请注意
添加LEADTOOLS NuGet和本地引用设置许可中会详细介绍添加引用和设置一个许可证教程。
与项目创建、添加引用,许可,可以开始编码。
在解决方案资源管理器,打开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
。
/ /使用块
使用系统;
使用先;
使用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;
添加下面的全局变量:
/ /声明这些全球成员
私人imageview imageview;
私人RasterCodecs编解码器;
私人AutomationInteractiveMode annInteractiveMode;
私人IAnnAutomationControl automationControl;
私人AnnAutomationManager annManager;
私人AnnAutomation AnnAutomation;
将下面的代码添加到MainWindow_Load
事件处理程序初始化imageview并创建一个新的空白RasterImage。
私人无效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
文件夹,突出添加,然后单击类……
添加四个新类:
AnnCircleEditDeisgner.cs
AnnCircleDrawDesigner.cs
AnnCircleObject.cs
AnnCircleObjectRenderer.cs
请注意
一定要改变这些类的名称空间为每个匹配的名称空间
MainWindow.xaml.cs
。
将以下代码添加到AnnCircleEditDesigner
类:
使用系统;
使用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
类:
使用系统;
使用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
类:
使用Leadtools.Annotations.Engine;
名称空间Create_a_Custom_Annotation
{
类AnnCircleObject: AnnEllipseObject
{
/ /设置UserObjectID id
公共常量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;
/ /下面的代码片段改变注释的缩略图的大小
/ / 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 ()
。
私人无效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 ()
方法:
无效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 ()
方法如上所示。将以下代码添加到新方法:
私人无效CreateCustomObject (AnnAutomationManager annManager)
{
/ /创建圆形对象,分配设计师,设置工具栏图像,然后将其添加到管理器和渲染引擎
varexeAsm = System.Reflection.Assembly.GetExecutingAssembly ();
流流=零;
foreach(字符串res在exeAsm.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.CircleObjectId新AnnCircleObjectRenderer (annManager));
}
按运行项目F5,或通过选择调试- >开始调试。
如果是正确的步骤,应用程序应该运行并显示一个空白RasterImage。画的圆注释,单击左上角的按钮的形式。
本教程介绍了如何创建一个定制的循环使用LEADTOOLS注释SDK技术注释。