国家  china

如何使用LEADTOOLS图像标注控件在代码中对图像进行标注

leadtools对图像和文档提供了多种类型的标注,允许用户通过图形界面或者后台代码进行插入。本教程将按照步骤分享如何在代码中对图像进行标注。

上一篇博客介绍了如何给界面添加标注工具栏,以实现从界面上添加标注。链接如下http://blog.gcpowertools.com.cn/post/annotation_startup.aspx

本篇博客通过对文本标注的添加以及定位。讲解如果通过代码添加AnnObject来实现标注。

 

1.创建VS项目

在VS中创建winform项目,创建完成后需要确认使用的.NET版本以及编译的目标平台。本例采用.NET4.0和X86平台进行编译。

 

2.引用接口dll

需要引用的dll列表如下,可以在C:\LEADTOOLS 19\Bin\Dotnet4\Win32找到。

image_thumb2

主界面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();


                // setup the automation (will create the container as well) 
                automation = new AnnAutomation(annger, viewer);

                // add an event handler for changes to the current designer 

                // setup this automation as the active one 
                automation.Active = true;
            }

        }

 

4.2添加加载图像方法

private void loadImage(string filename)
        {
            if(automation!=null)
                automation.Container.Objects.Clear();
            img = new RasterCodecs().Load(filename);
            viewer.Image = img;
        }

 

4.3添加新增文本对象的方法

private void addAnnText(int sign,string text) {
            AnnTextObject textObj = new AnnTextObject();
            textObj.Bounds = new AnnRectangle(getLocation(sign),new AnnSize(100,30));
            textObj.Font = new AnnFont("Arial", new AnnLength(30), FontStyle.Bold);
            textObj.TextColor = Color.Blue;
            textObj.Text = text;
            // Add the object to the automation container 
            //automation.Container.Children.Add(textObj);
            automation.Container.Objects.Add(textObj);
            viewer.Refresh();

        }
        //获取定位
        private AnnPoint getLocation(int sign) {
            int annwidth = 60;
            int annheight = 30;
            AnnPoint ap=new AnnPoint();
            switch (sign)
            {
                //1 2 3
                //4   5
                //6 7 8
                case 1:
                    ap = new AnnPoint(0, 0);
                    break;
                case 2:
                    ap = new AnnPoint((img.Width - annwidth) / 2, 0);
                    break;
                case 3:
                    ap = new AnnPoint(img.Width - annwidth, 0);
                    break;
                case 4:
                    ap = new AnnPoint(0, (img.Height - annheight) / 2);
                    break;
                case 5:
                    ap = new AnnPoint(img.Width - annwidth, (img.Height - annheight) / 2);
                    break;
                case 6:
                    ap = new AnnPoint(0, img.Height - annheight);
                    break;
                case 7:
                    ap = new AnnPoint((img.Width - annwidth) / 2, img.Height - annheight);
                    break;
                case 8:
                    ap = new AnnPoint(img.Width - annwidth, img.Height - annheight);
                    break;
            }
            return ap;
        }

 

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添加打开文件按钮事件方法

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

 

4.6添加下拉按钮相关方法

private void 左上ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(1,(sender as ToolStripMenuItem).Text);
        }

        private void 上方ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(2, (sender as ToolStripMenuItem).Text);
        }

        private void 右上ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(3, (sender as ToolStripMenuItem).Text);
        }

        private void 左边ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(4, (sender as ToolStripMenuItem).Text);
        }

        private void 右边ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(5, (sender as ToolStripMenuItem).Text);
        }

        private void 坐下ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(6, (sender as ToolStripMenuItem).Text);
        }

        private void 下方ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(7, (sender as ToolStripMenuItem).Text);
        }

        private void 右下ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(8, (sender as ToolStripMenuItem).Text);
        }

 

 

5.编译运行

演示程序界面如下图所示

image

 

下载示例代码

转载声明:
欢迎将本站文章进行转载、演绎或用于商业目的,转载时请注明以下信息
文章转自:葡萄城控件产品博客,http://blog.gcpowertools.com.cn
原文地址:http://blog.gcpowertools.com.cn/post/leadtools_annotation_code.aspx

不允许评论