本教程演示如何使用LEADTOOLS SDK创建WPF c#应用程序来创建自定义注释。
概述 | |
---|---|
总结 | 本教程演示如何在c# WPF应用程序中使用LEADTOOLS自定义注释技术。 |
完成时间 | 45分钟 |
Visual Studio项目 | 下载教程项目(12kb) |
平台 | WPF c#应用程序 |
IDE | Visual Studio 2017, 2019 |
开发许可 | 下载LEADTOOLS |
用另一种语言试试 |
|
在开始之前创建一个自定义注释- WPF c#本教程中,通过回顾添加引用和设置License教程。
中创建的项目的副本开始添加引用和设置License教程。如果您没有该项目,请按照该教程中的步骤创建它。
所需要的参考资料取决于项目的目的。引用可以通过以下两种方法中的一种添加(但不能同时添加)。
如果使用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
有关特定格式所需的Codec dll的完整列表,请参阅文件格式支持.
许可证解锁项目所需的特性。它必须在调用任何工具箱函数之前设置。有关详情,包括不同平台的教程,请参阅[设置运行时License.
有两种类型的运行时许可证:
请注意
中详细介绍了添加LEADTOOLS NuGet和本地引用以及设置许可添加引用和设置License教程。
随着项目的创建、引用的添加和许可的设置,编码就可以开始了。
在解决方案资源管理器,打开MainWindow.xaml
文件。添加以下XAML代码以正确显示UI元素。
<窗口x:类= " Custom_Annotation_WPF。主窗口”
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 "
标题="MainWindow"高="1000"宽="1000"
加载= " MainWindow_Load ">
<网格名称= " imageViewerGrid ">
<网格。RowDefinitions>
<RowDefinition高度=“自动”></ RowDefinition>
<RowDefinition高度= " * "></ RowDefinition>
</网格。RowDefinitions>
</网格>
</窗口>
我们定义了2行,作为注释工具栏和Image Viewer的容器。你可以调整高度
而且宽度
如你所愿,但这并不影响本教程。
点击事件图标。属性窗口.然后,双击负载事件来创建事件处理程序。中使用语句添加以下内容使用
块的顶部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
{
初始化imageViewer对象
imageview =新imageview
{
背景= SystemColors。AppWorkspaceBrush,
UseDpi =真正的,
ViewPadding =新ControlPadding (),
ViewHorizontalAlignment = ControlAlignment。中心,
ViewVerticalAlignment = ControlAlignment。中心,
};
imageViewerGrid.Children.Add (imageview);
网格。SetRow (imageview, 1);
imageViewer.BringIntoView ();
//创建一个纯白色的背景来绘制
imageview。Image = RasterImage。创建(1000,1000,24,96,RasterColor.White);
//初始化codecs对象
编解码器=新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 begin = leadpoint . empty;
私人LeadPointD end = leadpoint . empty;
私人LeadPointD anchorThumb = leadpoint . empty;
私人LeadPointD currentThumb = leadpoint . empty;
私人LeadPointD leftThumb = leadpoint . empty;
私人LeadPointD rightThumb = leadpoint . empty;
私人LeadPointD topThumb = leadpoint . empty;
私人LeadPointD bottomThumb = leadpoint . empty;
私人LeadPointD center = leadpoint . empty;
私人intradiusModX;
私人intradiusModY;
私人双半径;
///<摘要>
///AnnCircleEditDesigner的构造函数
///> < /总结
公共AnnCircleEditDesigner(IAnnAutomationControl automationControl, AnnContainer AnnContainer, AnnRectangleObject annRectangle)
:基地(automationControl,annContainer,annRectangle) {}
///<摘要>
///重写指针向下事件
///将开始点和结束点设置为第一次单击的位置
///> < /总结
公共覆盖保龄球OnPointerDown(AnnContainer sender, AnnPointerEventArgs e)
{
circle = (AnnCircleObject)目标对象;
begin = circle.Bounds.TopLeft;
半径= circle. rectangle . width / 2;
center = circle. rotatcenter;
leftThumb = leadpoint . create(居中。X -半径,中心。y);
rightThumb = leadpoint . create(中心。X +半径,中心。y);
topThumb = leadpoint . create(居中。X,中心。Y -半径);
bottomThumb = leadpoint . create(中心。X,中心。Y +半径);
currentThumb = leadpoint . empty;
anchorThumb = leadpoint . empty;
radiusModX = 0;
radiusModY = 0;
返回基地.OnPointerDown(发送方,e);
}
///<摘要>
///重写指针移动事件
///将新的鼠标点设置为结束变量
///做一些计算来创建一个圆对象(宽度/高度保持相等)
///> < /总结
公共覆盖保龄球OnPointerMove(AnnContainer sender, AnnPointerEventArgs e)
{
circle = (AnnCircleObject)目标对象;
如果(操作== AnnEditDesignerOperation.MoveThumb)
{
end = e.位置;
//设置锚点
如果(MoveThumbIndex != -1 && currentThumb. isempty) currentThumb = GetThumbLocations()[MoveThumbIndex];
如果(currentThumb == leftThumb)
{
如果(anchorThumb. isempty)锚定拇指=右拇指;
radiusModX =(结束。X > anchorthumbx) ?1: -1;
}
如果(currentThumb == rightThumb)
{
如果(anchorThumb. isempty) anchorThumb = leftThumb;
radiusModX =(结束。X < anchorthumbx) ?-1: 1;
}
如果(currentThumb == topThumb)
{
如果(anchorThumb. isempty) anchorThumb = bottomThumb;
radiusModY =(结束。Y > anchorThumb.Y) ?1: -1;
}
如果(currentThumb == bottomThumb)
{
如果(anchorThumb. isempty) anchorThumb = topThumb;
radiusModY =(结束。Y < anchantumb.com Y) ?-1: 1;
}
//推导左上
半径= Math.Sqrt(Math.Pow(结束。X - anchorthumbb .X,2) + Math.Pow(结束。Y - anchantumb.y,2)) / 2;
中心= leadpoint . createX +(半径* radiusModX),锚定拇指。Y +(半径* radiusModY));
开始= leadpoint . create(中心。X -半径,中心。Y -半径);
//做这件事
圆。Rect = LeadRectD.Create(开始。X,开始。Y,半径* 2半径* 2);
无效(circle.Rect);
ResetRotateThumbs ();
返回真正的;
}
返回基地.OnPointerMove(发送方,e);
}
公共覆盖保龄球OnPointerUp(AnnContainer sender, AnnPointerEventArgs e)
{
返回基地.OnPointerUp(发送方,e);
}
}
}
将下面的代码添加到AnnCircleDrawDesigner
类:
使用系统;
使用Leadtools;
使用Leadtools.Annotations.Engine;
使用Leadtools.Annotations.Designers;
名称空间Custom_Annotation_WPF
{
类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.IsEmpty)
begin = e.位置;
返回基地.OnPointerDown(发送方,e);
}
///<摘要>
///重写指针移动事件
///将新的鼠标点设置为结束变量
///做一些计算来创建一个圆对象(宽度/高度保持相等)
///> < /总结
公共覆盖保龄球OnPointerMove(AnnContainer sender, AnnPointerEventArgs e)
{
如果(pointerDown)
{
end = e.位置;
AnnCircleObject circle = (AnnCircleObject)目标对象;
双x = Math.Abs(结束。X - start .X);
双y = Math.Abs(结束。Y - start .Y);
双scaleX = 1;
双scaleY = 1;
如果(x < y) scaleX = x != 0 ?Y / x: 0;
其他的scale = y != 0 ?X / y: 0;
//获取高度和宽度
双width = Math.Abs(结束。X - start .X) * Math.Abs(scaleX);
双height = Math.Abs(结束。Y - start .Y) * Math.Abs(scaleY);
//限制为容器边界
如果(开始。Y + height > sender.Bounds.Height) height = sender.Bounds.Height - begin.Y;
如果(开始。width = sender.Bounds.Width - begin.X;
//确保圆圈始终是圆
如果(width < height) height = width;
如果(height < width) width = height;
//做这件事
圆。Rect = LeadRectD.Create(开始。X,开始。Y,宽度,高度);
无效(circle.Rect);
}
返回真正的;
}
公共覆盖保龄球OnPointerUp(AnnContainer sender, AnnPointerEventArgs e)
{
begin = leadpoint . empty;
返回基地.OnPointerUp(发送方,e);
}
}
}
将下面的代码添加到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操作)
{
List
newThumbs =新列表< LeadPointD > (); 为(intI = 0;i < thumbLocations.Length;I += 2)
newThumbs.Add (thumbLocations[我]);
基地.RenderThumbs(mapper, newThumbs.ToArray(), operations);
}
}
}
右键单击MainWindow.xaml
在解决方案资源管理器并选择视图代码来显示表单后面的代码。添加一个名为InitAnnotations ()
对象中的新方法MainWindow_Load
事件处理程序如下imageViewer.BringIntoView ()
.
私人无效MainWindow_Load (对象发送者,EventArgs
{
初始化imageViewer对象
imageview =新imageview
{
背景= SystemColors。AppWorkspaceBrush,
UseDpi =真正的,
ViewPadding =新ControlPadding (),
ViewHorizontalAlignment = ControlAlignment。中心,
ViewVerticalAlignment = ControlAlignment。中心,
};
imageViewerGrid.Children.Add (imageview);
网格。SetRow (imageview, 1);
imageViewer.BringIntoView ();
InitAnnotations ();
//创建一个纯白色的背景来绘制
imageview。Image = RasterImage。创建(1000,1000,24,96,RasterColor.White);
//初始化codecs对象
编解码器=新RasterCodecs ();
}
将以下代码添加到newInitAnnotations ()
方法:
无效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)。ImageViewer = ImageViewer;
初始化交互模式并将其添加到查看器
annInteractiveMode =新AutomationInteractiveMode
{
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对象,分配设计器,设置工具栏图像,然后将其添加到管理器和渲染引擎
varexeAsm = System.Reflection.Assembly.GetExecutingAssembly();
溪流溪流=零;
foreach(字符串res在exeAsm.GetManifestResourceNames ())
如果(res.Contains (“CircleIcon”))
{
stream = 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 Annotations SDK技术创建自定义圆注释。