本帖最后由 x1212chf 于 2019-4-22 14:27 编辑
Leadtool 19
在AnnAutomation中添加AnnPolylineObject,移动AnnPolylineObject,获取AnnPolylineObject的位置信息,并安装其位置信息分割图片,现在的问题是如何坐标转换,获取到AnnPolylineObject的位置,非常感谢!
已查到处理方法,感谢!
主要代码如下:
private ImageViewer viewer;
private AnnAutomation automation;
private LeadRect currentDrawRectangle = LeadRect.Empty;
private void AnnotationsDemo_Load(object sender, EventArgs e)
{
viewer = new ImageViewer();
viewer.Dock = DockStyle.Fill;
ImageViewerAutomationControl automationControl = new ImageViewerAutomationControl();
automationControl.ImageViewer = viewer;
// initialize a new RasterCodecs object
RasterCodecs codecs = new RasterCodecs();
// load the main image into the viewer
viewer.Image = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\Sample1.cmp");
// initialize the interactive mode for the viewer
AutomationInteractiveMode automationInteractiveMode = new AutomationInteractiveMode();
automationInteractiveMode.AutomationControl = automationControl;
// add the interactive mode to the viewer
viewer.InteractiveModes.BeginUpdate();
viewer.InteractiveModes.Add(automationInteractiveMode);
viewer.InteractiveModes.EndUpdate();
viewer.PostRender += Viewer_PostRender;
if (viewer.Image != null)
{
// create and set up the automation manager
AnnAutomationManager annAutomationManager = new AnnAutomationManager();
annAutomationManager.RestrictDesigners = true;
// Instruct the manager to create all of the default automation objects.
annAutomationManager.CreateDefaultObjects();
// initialize the manager helper and create the toolbar and add it then the viewer to the controls
AutomationManagerHelper managerHelper = new AutomationManagerHelper(annAutomationManager);
managerHelper.CreateToolBar();
ToolBar toolBar = managerHelper.ToolBar;
Controls.Add(toolBar);
Controls.Add(viewer);
toolBar.Visible = false;
automation = new AnnAutomation(annAutomationManager, automationControl);
automation.Active = true;
automation.Container.Size = automation.Container.Mapper.SizeToContainerCoordinates(LeadSizeD.Create(viewer.Image.ImageWidth, viewer.Image.ImageHeight));
CreateMyAutomationObjects();
annAutomationManager.UserMode = AnnUserMode.Design;
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
//AnnAutomationObject obj = automation.Manager.FindObjectById(AnnObject.LineObjectId);
//if(obj!=null)
//{
//}
if (this.automation != null && automation.Container.Children.Count > 0 && automation.Container.Children[0] is AnnPolylineObject)
{
try
{
if ((automation.Container.Children[0] as AnnPolylineObject).Points.Count >= 2)
{
LeadPointD p1 = (automation.Container.Children[0] as AnnPolylineObject).Points[0];
LeadPointD p2 = (automation.Container.Children[0] as AnnPolylineObject).Points[1];
/////--------------如何转换坐标??????????????????????????????
//p1 = viewer.ConvertPoint(viewer.ActiveItem, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, p1);
//p2 = viewer.ConvertPoint(viewer.ActiveItem, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, p2);
p1 = automation.Container.Mapper.PointFromContainerCoordinates(p1, AnnFixedStateOperations.None);
p2 = automation.Container.Mapper.PointFromContainerCoordinates(p2, AnnFixedStateOperations.None);
leadRect1 = new LeadRect(0, 0, this.imageViewer.Image.Width, (int)p1.Y);
currentDrawRectangle = leadRect1;
this.viewer.Invalidate();
}
}
catch (Exception ex)
{
ShowError("出错", ex);
}
}
}
private void CreateMyAutomationObjects()
{
AnnPolylineObject polyLineObj2 = new AnnPolylineObject();
polyLineObj2.Points.Add(new LeadPointD(20, 600));
polyLineObj2.Points.Add(new LeadPointD(1000, 600));
polyLineObj2.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("Red"), LeadLengthD.Create(3));
polyLineObj2.IsClosed = true;
automation.Container.Children.Add(polyLineObj2);
}
private void ImageViewer_PostRender(object sender, ImageViewerRenderEventArgs e)
{
if (this.currentDrawRectangle != LeadRect.Empty)
{
Graphics graphics = e.PaintEventArgs.Graphics;
using (Brush brush = new SolidBrush(Color.FromArgb(50, Color.Yellow)))
{
LeadRect leadRect = this.imageViewer.ConvertRect(null, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control,
new LeadRect(currentDrawRectangle.X, currentDrawRectangle.Y, currentDrawRectangle.Width, currentDrawRectangle.Height));
//LeadRect leadRect = currentDrawRectangle;
Rectangle rectangle = new Rectangle(leadRect.X, leadRect.Y, leadRect.Width, leadRect.Height);
graphics.FillRectangle(brush, rectangle);
graphics.DrawRectangle(Pens.Yellow, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
}
}
}
|
|