Property | Value |
---|---|
Dock | Fill |
[Visual Basic]
Imports System.Drawing.Drawing2D Imports Leadtools Imports Leadtools.Drawing Imports Leadtools.Codecs Imports Leadtools.WinForms
[C#]
using System.Drawing.Drawing2D; using Leadtools; using Leadtools.Drawing; using Leadtools.Codecs; using Leadtools.WinForms;
[Visual Basic]
' what shape we are drawing Private Enum ShapeType Line Rectangle Ellipse End Enum ' shape we are currently drawing Private shape As ShapeType ' start and end point for the line Private startCoords As Point Private endCoords As Point ' coordinate for the rectangle and the ellipse Private rectangleCoords As Rectangle ' are we currently drawing or not Private drawing As Boolean Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Initialize the private varibles shape = ShapeType.Line drawing = False ' Load an image into the viewer Dim codecs As New RasterCodecs() rasterImageViewer1.Image = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Image1.cmp") End Sub
[C#]
// what shape we are drawing private enum ShapeType { Line, Rectangle, Ellipse } // shape we are currently drawing ShapeType shape; // start and end point for the line private Point startCoords; private Point endCoords; // coordinate for the rectangle and the ellipse private Rectangle rectangleCoords; // are we currently drawing or not private bool drawing; private void Form1_Load(object sender, System.EventArgs e) { // Initialize the private varibles shape = ShapeType.Line; drawing = false; // Load an image into the viewer RasterCodecs codecs = new RasterCodecs(); rasterImageViewer1.Image = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\Image1.cmp"); }
[Visual Basic]
Private Sub RasterImageViewer1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RasterImageViewer1.MouseDown ' check if we are already drawing, if so cancel it If (drawing) Then ' erase the shape DrawShape() drawing = False Else ' based on shape, initialize the coordinates Select Case (shape) Case ShapeType.Line ' drawing a line startCoords = New Point(e.X, e.Y) endCoords = startCoords Case ShapeType.Rectangle Case ShapeType.Ellipse ' drawing a rectangle or an ellipse rectangleCoords = New Rectangle(e.X, e.Y, 0, 0) End Select ' draw the shape drawing = True DrawShape() End If End Sub
[C#]
private void rasterImageViewer1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { // check if we are already drawing, if so cancel it if(drawing) { // erase the shape DrawShape(); drawing = false; } else { // based on shape, initialize the coordinates switch(shape) { case ShapeType.Line: // drawing a line startCoords = new Point(e.X, e.Y); endCoords = startCoords; break; case ShapeType.Rectangle: case ShapeType.Ellipse: // drawing a rectangle or an ellipse rectangleCoords = new Rectangle(e.X, e.Y, 0, 0); break; } // draw the shape drawing = true; DrawShape(); } }
[Visual Basic]
Private Sub RasterImageViewer1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RasterImageViewer1.MouseMove If (drawing) Then ' first, erase the shape DrawShape() ' get new coords for the shape Select Case (shape) Case ShapeType.Line endCoords = New Point(e.X, e.Y) Case ShapeType.Rectangle, ShapeType.Ellipse rectangleCoords = Rectangle.FromLTRB(rectangleCoords.Left, rectangleCoords.Top, e.X, e.Y) End Select ' draw the shape again DrawShape() End If End Sub
[C#]
private void rasterImageViewer1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if(drawing) { // first, erase the shape DrawShape(); // get new coords for the shape switch(shape) { case ShapeType.Line: endCoords = new Point(e.X, e.Y); break; case ShapeType.Rectangle: case ShapeType.Ellipse: rectangleCoords = Rectangle.FromLTRB(rectangleCoords.Left, rectangleCoords.Top, e.X, e.Y); break; } // draw the shape again DrawShape(); } }
[Visual Basic]
Private Sub rasterImageViewer1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) If drawing Then ' erase the shape DrawShape() drawing = False ' draw the shape on the image Select Case shape Case ShapeType.Line If startCoords.X <> endCoords.X OrElse startCoords.Y <> endCoords.Y Then ' convert the points to image coordinates Dim t As Transformer = New Transformer(rasterImageViewer1.Transform) startCoords = Point.Round(t.PointToLogical(startCoords)) endCoords = Point.Round(t.PointToLogical(endCoords)) Dim startPoint As LeadPoint = New LeadPoint(startCoords.X, startCoords.Y) Dim endPoint As LeadPoint = New LeadPoint(endCoords.X, endCoords.Y) startPoint = rasterImageViewer1.Image.PointToImage(RasterViewPerspective.TopLeft, startPoint) endPoint = rasterImageViewer1.Image.PointToImage(RasterViewPerspective.TopLeft, endPoint) startCoords = New Point(startPoint.X, startPoint.Y) endCoords = New Point(endPoint.X, endPoint.Y) ' create the graphics container for the image and draw the line Using container As RasterImageGdiPlusGraphicsContainer = New RasterImageGdiPlusGraphicsContainer(rasterImageViewer1.Image) Using pen As Pen = New Pen(Color.Yellow, 2) ' use anti-alias lines container.Graphics.SmoothingMode = SmoothingMode.AntiAlias container.Graphics.DrawLine(pen, startCoords, endCoords) ' draw a rectangle next shape = ShapeType.Rectangle End Using End Using rasterImageViewer1.Invalidate() End If Case ShapeType.Rectangle, ShapeType.Ellipse ' "fix" the rectangle so it does not have a negative width or height rectangleCoords = FixRectangle(rectangleCoords) If (Not rectangleCoords.IsEmpty) Then ' convert the rectangle to image coordinates Dim t As Transformer = New Transformer(rasterImageViewer1.Transform) rectangleCoords = Rectangle.Round(t.RectangleToLogical(rectangleCoords)) Dim rectCoords As LeadRect = New LeadRect(rectangleCoords.Left, rectangleCoords.Top, rectangleCoords.Width, rectangleCoords.Height) rectCoords = rasterImageViewer1.Image.RectangleToImage(RasterViewPerspective.TopLeft, rectCoords) rectangleCoords = New Rectangle(rectCoords.Left, rectCoords.Top, rectCoords.Width, rectCoords.Height) ' create the graphics container for the image and draw the rectangle or ellipse Using container As RasterImageGdiPlusGraphicsContainer = New RasterImageGdiPlusGraphicsContainer(rasterImageViewer1.Image) Using pen As Pen = New Pen(Color.Yellow, 2) ' use anti-alias lines container.Graphics.SmoothingMode = SmoothingMode.AntiAlias If shape = ShapeType.Rectangle Then container.Graphics.DrawRectangle(pen, rectangleCoords) ' draw an ellipse next shape = ShapeType.Ellipse Else container.Graphics.DrawEllipse(pen, rectangleCoords) ' draw a line next shape = ShapeType.Line End If End Using End Using rasterImageViewer1.Invalidate() End If End Select End If End Sub
[C#]
private void rasterImageViewer1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if (drawing) { // erase the shape DrawShape(); drawing = false; // draw the shape on the image switch (shape) { case ShapeType.Line: if (startCoords.X != endCoords.X || startCoords.Y != endCoords.Y) { // convert the points to image coordinates Transformer t = new Transformer(rasterImageViewer1.Transform); startCoords = Point.Round(t.PointToLogical(startCoords)); endCoords = Point.Round(t.PointToLogical(endCoords)); LeadPoint startPoint = new LeadPoint(startCoords.X, startCoords.Y); LeadPoint endPoint = new LeadPoint(endCoords.X, endCoords.Y); startPoint = rasterImageViewer1.Image.PointToImage(RasterViewPerspective.TopLeft, startPoint); endPoint = rasterImageViewer1.Image.PointToImage(RasterViewPerspective.TopLeft, endPoint); startCoords = new Point(startPoint.X, startPoint.Y); endCoords = new Point(endPoint.X, endPoint.Y); // create the graphics container for the image and draw the line using (RasterImageGdiPlusGraphicsContainer container = new RasterImageGdiPlusGraphicsContainer(rasterImageViewer1.Image)) { using (Pen pen = new Pen(Color.Yellow, 2)) { // use anti-alias lines container.Graphics.SmoothingMode = SmoothingMode.AntiAlias; container.Graphics.DrawLine(pen, startCoords, endCoords); // draw a rectangle next shape = ShapeType.Rectangle; } } rasterImageViewer1.Invalidate(); } break; case ShapeType.Rectangle: case ShapeType.Ellipse: // "fix" the rectangle so it does not have a negative width or height rectangleCoords = FixRectangle(rectangleCoords); if (!rectangleCoords.IsEmpty) { // convert the rectangle to image coordinates Transformer t = new Transformer(rasterImageViewer1.Transform); rectangleCoords = Rectangle.Round(t.RectangleToLogical(rectangleCoords)); LeadRect rectCoords = new LeadRect(rectangleCoords.Left, rectangleCoords.Top, rectangleCoords.Width, rectangleCoords.Height); rectCoords = rasterImageViewer1.Image.RectangleToImage(RasterViewPerspective.TopLeft, rectCoords); rectangleCoords = new Rectangle(rectCoords.Left, rectCoords.Top, rectCoords.Width, rectCoords.Height); // create the graphics container for the image and draw the rectangle or ellipse using (RasterImageGdiPlusGraphicsContainer container = new RasterImageGdiPlusGraphicsContainer(rasterImageViewer1.Image)) { using (Pen pen = new Pen(Color.Yellow, 2)) { // use anti-alias lines container.Graphics.SmoothingMode = SmoothingMode.AntiAlias; if (shape == ShapeType.Rectangle) { container.Graphics.DrawRectangle(pen, rectangleCoords); // draw an ellipse next shape = ShapeType.Ellipse; } else { container.Graphics.DrawEllipse(pen, rectangleCoords); // draw a line next shape = ShapeType.Line; } } } rasterImageViewer1.Invalidate(); } break; } } }
[Visual Basic]
Private Sub DrawShape() ' draw the shape on screen (reversible) Select Case (shape) Case ShapeType.Line ControlPaint.DrawReversibleLine(RasterImageViewer1.PointToScreen(startCoords), RasterImageViewer1.PointToScreen(endCoords), Color.Transparent) Case ShapeType.Rectangle, ShapeType.Ellipse ' "fix" the rectangle so it does not have a negative width or height Dim rc As Rectangle = FixRectangle(rectangleCoords) ControlPaint.DrawReversibleFrame(RasterImageViewer1.RectangleToScreen(rc), Color.Transparent, FrameStyle.Thick) End Select End Sub Shared Function FixRectangle(ByVal rc As Rectangle) As Rectangle If (rc.Left > rc.Right OrElse rc.Top > rc.Bottom) Then Return Rectangle.FromLTRB( _ Math.Min(rc.Left, rc.Right), _ Math.Min(rc.Top, rc.Bottom), _ Math.Max(rc.Left, rc.Right), _ Math.Max(rc.Top, rc.Bottom)) Else Return rc End If End Function
[C#]
private void DrawShape() { // draw the shape on screen (reversible) switch(shape) { case ShapeType.Line: ControlPaint.DrawReversibleLine(rasterImageViewer1.PointToScreen(startCoords), rasterImageViewer1.PointToScreen(endCoords), Color.Transparent); break; case ShapeType.Rectangle: case ShapeType.Ellipse: // "fix" the rectangle so it does not have a negative width or height Rectangle rc = FixRectangle(rectangleCoords); ControlPaint.DrawReversibleFrame(rasterImageViewer1.RectangleToScreen(rc), Color.Transparent, FrameStyle.Thick); break; } } static Rectangle FixRectangle(Rectangle rc) { if(rc.Left > rc.Right || rc.Top > rc.Bottom) return Rectangle.FromLTRB( Math.Min(rc.Left, rc.Right), Math.Min(rc.Top, rc.Bottom), Math.Max(rc.Left, rc.Right), Math.Max(rc.Top, rc.Bottom)); else return rc; }
NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.