華文網

如何使用LEADTOOLS圖像標注控制項在介面上對圖像進行標注

leadtools對圖像和文檔提供了多種類型的標注,允許使用者通過圖形介面或者後臺代碼進行插入。本教程將按照步驟分享如何使用圖像標注控制項在介面上對圖像進行標注。

Leadtools 19總套包下載>>>

1.創建VS項目

在VS中創建winform專案,創建完成後需要確認使用的.NET版本以及編譯的目標平臺。本例採用.NET4.0和X86平臺進行編譯。

2.引用介面dll

需要引用的dll清單如下,可以在C:LEADTOOLS 19BinDotnet4Win32找到。

主介面Form1代碼中添加引用

using System.Windows.Forms;using Leadtools;using Leadtools.Codecs;using Leadtools.WinForms;using Leadtools.Annotations;3.主介面添加相關控制項

在設計介面添加一個MenuStrip功能表

添加按鈕用以打開檔;

添加下拉清單,包括2個選項:標注模式和流覽模式

4.後臺代碼編寫

4.1控制項初始化相關代碼

添加相關物件

RasterImageViewer viewer = new RasterImageViewer; RasterImage img; AnnAutomationManager annger; AnnAutomation automation;

添加initControl方法,

用於初始化圖片流覽控制項以及標注相關控制項,在Form1構造函數中調用該方法

private void initControl { Support.SetLicense; viewer.Dock = DockStyle.Fill; panel1.Controls.Add(viewer); viewer.HorizontalAlignMode = RasterPaintAlignMode.Center; viewer.VerticalAlignMode = RasterPaintAlignMode.Center; loadImage("qwe.jpg"); if (viewer.Image != null) { // create and setup the automation manager annger = new AnnAutomationManager; // Instruct the manager to create the default (all) automation objects. annger.CreateDefaultObjects; // create the toolbar and add it to the form annger.CreateToolBar; Controls.Add(annger.ToolBar); // setup the automation (will create the container as well) automation = new AnnAutomation(annger, viewer); // add an event handler for changes to the current designer automation.CurrentDesignerChanged += new EventHandler(automation_CurrentDesignerChanged); // setup this automation as the active one automation.Active = true; } toolStripComboBox1.SelectedIndex = 0; }

4.2添加載入圖像方法

private void loadImage(string filename) { img = new RasterCodecs.Load(filename); viewer.Image = img; }

4.3添加automation_CurrentDesignerChanged方法

private void automation_CurrentDesignerChanged(object sender, EventArgs e) { AnnAutomation automation = sender as AnnAutomation; AnnButtonRunDesigner buttonRunDesigner = automation.CurrentDesigner as AnnButtonRunDesigner; if (buttonRunDesigner != null) buttonRunDesigner.Run += new EventHandler

4.4添加buttonRunDesigner_Run方法

private void buttonRunDesigner_Run(object sender, AnnRunDesignerEventArgs e) { if (e.OperationStatus == AnnDesignerOperationStatus.End) { AnnButtonObject btn = e.Object as AnnButtonObject; MessageBox.Show(string.Format("Button with text = {0} was clicked!", btn.Text)); } }

4.5添加打開檔按鈕事件方法

4.6添加模式下拉清單選項改變事件方法

private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) { annger.UserMode = (toolStripComboBox1.SelectedIndex == 0) ? AnnUserMode.Design : AnnUserMode.Run; }5.編譯運行

演示程式介面如下圖所示