As part of the LEAD Technologies 25th anniversary, we are creating 25 projects in 25 days to celebrate LEAD’s depth of features and ease of use. Today’s project comes from Hadi.
What it Does
This C# application will create several custom annotations using LEADTOOLS Version 19.
Development Progress Journal
Hello, my name is Hadi and I am writing a sample application that will demonstrate the various capabilities of the LEADTOOLS Annotation SDK, more specifically, how to create Custom Annotations and Designers.
My plan is to start off by writing a simple project that uses the default Annotations and
ImageViewerso that I have a base to start with. The WinForms Annotations demo located in the \Examples\DotNet\CS\AnnotationsDemo directory will work perfectly.
Now that I have the sample project created, I will start with my first custom object, the
AnnCircleObject. I want this custom object to be very similar to the
AnnEllipseObjectbut instead of an oval, it will be a perfect circle.
To get started, I created new directories to keep things organized and will start by creating the new object which is based on the Ellipse.
Next, I want to control how the object is drawn, so I will need to create my own custom Draw Designer based on the
AnnEllipseDrawDesigner. I will override the
OnPointerMoveevents so that I can control the ellipse and force it into a perfect circle.
OnPointerDownevent I set the begin and end points, and in the
OnPointerMoveevent I do some math to get the width and height of the drawing rectangle so I can force the circle to be the same width and height, therefore creating a perfect circle.
I also don’t want the new
AnnCircleObjectto be modified and changed to an oval or ellipse, so I have created a custom
ObjectRendererbased on the
AnnEllipseObjectRendererand overrode the
RenderThumbsevent to not render the top, left, right and bottom resize thumbs so the user can’t modify the height or width individually.
Next I will create a text-based custom object, the
AnnNumberObject. This object will be a custom text-based Annotation object that will, when drawn, count and display the number of other
AnnNumberObjectsin the same
I based the
AnnTextObjectand its Draw Designer on the
AnnRectangleDrawDesigner. In the Draw Designer I overrode the
OnPointerDownevent so that it will cycle through each object in the
AnnContainerand increment a counter by 1 for each object it finds, setting the
Textproperty of the
NumberObjectto that value.
NumberObject, I also had to create a custom Edit Designer which I based on the
AnnTextEditDesignerso that I can have the font size change as the size of the object changes. To do this, I overrode the
OnPointerDownevent to capture the starting values for the
NumberObject‘s bounds and font size, then I overrode the
OnEditevent to do some math to get a ratio to see how the width was being edited and forces the font size to change with it.
The third object I created was the
AnnStarObjectbased on the
AnnPolylineObject. This object will let the user click and drag their mouse to set the center and radius of a perfect gold star. To achieve this, I created a custom Draw Designer based on the
AnnPolylineDrawDesigner. I overrode the
OnPointerDownto set the center of the star then I overrode the
OnPointerMoveevent and did the math necessary to create the perfect star. I calculated the distance between the center point and the current location of the mouse to generate the radius, then used a formula to find the 10 points of the star and add them to the object.
The final object added was the
AnnTinyStampObjectwhich is based on the
AnnStampObject. This object is very similar to the
StampObjectexcept I have it draw the stamp on a single click. To achieve this, I created a custom Draw Designer which isn’t based on another object’s draw designer. I overrode the
OnPointerDownevent and calculated the 4 points of the rectangle based on the mouse location and added those to the tiny stamp object and ended the designer in the
After all my objects were created, I had to add them to my
AnnAutomationManager.AutomationObjectlist so that the user can access and use them. I created a method to handle adding the objects, as well as setting the
ToolbarImageto make it easy for the user to find the object in the toolbar.
Overall this project took me less than a day to write since I had the
AnnotationDemoto get started with and a helpful tutorial within the LEADTOOLS documentation about implementing custom annotations located here:
Download the Project
The source code for this sample project can be downloaded from here. To run the project, extract it to the C:\LEADTOOLS 19\Examples\DotNet\CS directory.