Home : Annotations : Custom : Override Line Cap Rendering
Q10282 - HOWTO: Override Line Cap Rendering

The default behavior for annotation line cap rendering is to use the same pen as used for the annotation outline.  Normally this isn't an issue, but if you only want a rounded end, rather than an actual end cap, you will need to handle the line cap rendering manually.

This article will create a custom renderer for FreehandAnnotation that will draw a rounded end. Below are the steps we will take.

  1. Create a class that derives from FreehandRenderingEngine.
  2. Override the RenderAnnotation method.
    • Call base.RenderAnnotation to let the default rendering happen with no line cap.
    • Create a 1-pixel AnnotationPen that will be used to draw the line cap.
    • Create the AnnotationLineCap we will draw.
    • Call base.SetGraphicsTransform so any offset, zoom or rotation will be applied to the line cap.
    • Call base.RenderLineCap to draw both line caps.
    • Call base.RestoreGraphicsTransform to restore the Graphics object.
  3. Replace the existing freehand renderer with our own.

Here is the source code for this custom renderer:

[C#]
public class CustomFreehandRenderer : FreehandRenderingEngine
{
    public CustomFreehandRenderer()
    {
    }

    public override void RenderAnnotation(Atalasoft.Annotate.AnnotationData annotation, RenderEnvironment e)
    {
        // Let the default rendering happen.
        // Make sure your FreehandAnnotation has a style of None for its line caps.
        base.RenderAnnotation(annotation, e);

        // Grab the points so we can tell the line cap where to draw.
        FreehandData data = annotation as FreehandData;
        PointF[] pts = data.Points.ToArray();
        int plen = pts.Length;

        // Create our own 1 pixel pen to use.
        AnnotationPen pen = data.Outline.Clone();
        pen.Width = 1;

        // Create the real line cap with a slightly smaller size.
        AnnotationLineCap cap = new AnnotationLineCap(AnnotationLineCapStyle.FilledEllipse, new SizeF(data.Outline.Width - 1, data.Outline.Width - 1));

        // Set the render transform to handle any offset, zoom and rotation,
        // then draw the line caps and restore the Graphics object.
        base.SetGraphicsTransform(annotation, e);
        base.RenderLineCap(pts[1], pts[0], cap, pen, e);
        base.RenderLineCap(pts[plen - 2], pts[plen - 1], cap, pen, e);
        base.RestoreGraphicsTransform(e);
    }
}
[VB.NET]
Public Class CustomFreehandRenderer
   Inherits FreehandRenderingEngine

   Public Sub New()
   End Sub

   Public Overrides Sub RenderAnnotation(ByVal annotation As Atalasoft.Annotate.AnnotationData, ByVal e As RenderEnvironment)
      ' Let the default rendering happen.
      ' Make sure your FreehandAnnotation has a style of None for its line caps.
      MyBase.RenderAnnotation(annotation, e)

      ' Grab the points so we can tell the line cap where to draw.
      Dim data As FreehandData = TryCast(annotation, FreehandData)
      Dim pts() As PointF = data.Points.ToArray()
      Dim plen As Integer = pts.Length

      ' Create our own 1 pixel pen to use.
      Dim pen As AnnotationPen = data.Outline.Clone()
      pen.Width = 1

      ' Create the real line cap with a slightly smaller size.
      Dim cap As New AnnotationLineCap(AnnotationLineCapStyle.FilledEllipse, New SizeF(data.Outline.Width - 1, data.Outline.Width - 1))
' Set the render transform to handle any offset, zoom and rotation, ' then draw the line caps and restore the Graphics object. MyBase.SetGraphicsTransform(annotation, e) MyBase.RenderLineCap(pts(1), pts(0), cap, pen, e) MyBase.RenderLineCap(pts(plen - 2), pts(plen - 1), cap, pen, e) MyBase.RestoreGraphicsTransform(e) End Sub End Class

Now that we have our renderer we have to replace the existing freehand renderer with ours.  This is done by simply adding our renderer to the AnnotationRenderers collection in the constructor of our application.

[C#]
Atalasoft.Annotate.Renderer.AnnotationRenderers.Add(typeof(FreehandData), new CustomFreehandRenderer());

[VB.NET]
Atalasoft.Annotate.Renderer.AnnotationRenderers.Add(GetType(FreehandData), New CustomFreehandRenderer())

Now when you draw a FreehandAnnotation it will automatically have rounded ends. This same technique can be used for other line based annotations.

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2017. InstantASP Ltd. All Rights Reserved