How to align the stroke with geometry? RRS feed

  • Question

  • I am using direct2D for some vector drawings in my application. Is there a way to change the stroke alignment with my geometry edges? i.e., if I draw a rectangle with stroke width of 10.0, then the stroke may be aligned in three ways: center of rectangle edges, inside or outside. How do I control it? What is the default?
    • Edited by John Rick Tuesday, June 11, 2013 2:44 PM
    Tuesday, June 11, 2013 2:44 PM

All replies

  • Hello John Rick,

    As you can see in the documentation of DrawRectangle, the stroke will always be centered on the rectangles outline: http://msdn.microsoft.com/en-us/library/windows/desktop/dd371902(v=vs.85).aspx


    A value greater than or equal to 0.0f that specifies the width of the rectangle's stroke. The stroke is centered on the rectangle's outline.


    Knowing this, you can take it into account when determining the size of the rectangle. E.g. if you want the stroke to appear to be outside of the original rectangle, just take the width of the stroke and adjust the rectangle's size and position accordingly.


    Helge Mahrt - Microsoft Developer Support Escalation Engineer - If my reply answers your question, please mark this post as answered.

    Tuesday, June 11, 2013 3:13 PM
  • Thanks,

    For simple shapes, like rectangle, I can understand that stroke can be aligned by manually calculating the inner rectangle (Though in principle, this is a bad approach), but how to do it with arbitrary geometry comprised of beziers, lines etc. ?

    Tuesday, June 11, 2013 5:21 PM
  • Someone please help! I am stuck.
    Wednesday, June 12, 2013 5:26 AM
  • What exactly are you trying to do here and why?

    As Helge points out, the stroke is always drawn centered. If you need something else you will need to implement it yourself, whether that is involves doing the math to use a stroke and fill of different sizes or to change how you are doing the drawing will depend on your specific needs.


    Wednesday, June 12, 2013 6:53 AM
  • I have an image on DirectX swap chain panel. Now, I want user to select a region of the image using free hand vector drawing. Now, the selected regions should be marked as aligning to pixel outer edges not in center. When the user has zoomed in the image, the selection should precisely be aligned to the outer edges of pixels, otherwise it will look bad.
    Wednesday, June 12, 2013 6:57 AM
  • I too am looking for a way to render the stroke "inside" or "outside" the geometry.

    My use case is that I am generating geometries based on DirectWrite, so they are "generic" geometries, not just simple rectangles.

    I want to render the stroke either "inside" or "outside" the curves of the characters.

    If there is a way to "offset" the curve, that may be good enough for my means (offset either +/- stroke width / 2.0). Is "widen" what I'd be looking for?


    Edit: Widen is NOT what I want.
    • Edited by pulse_matt Wednesday, November 27, 2013 9:02 PM
    Wednesday, November 27, 2013 7:55 PM
  • My solution thus far:

    When I want to stroke "inside" the geometry:

    1. Push a new layer using my geometry as a clipping geometry

    2. Stroke my geometry with double the desired stroke width

    3. Pop my layer

    Essentially, my stroke is too wide and still centred, but the clipping region clips off the part of the stroke that is "outside" the shape.

    For "outside":

    1. Use Widen to create a new geometry that is double the desired stroke width

    2. Combine the widened geometry with the original

    3. Follow the same pattern as "inside", but with the combined geometry as the clipping geometry

    This does the same thing, but instead it will clip away the "inside" portion of the extra wide stroke.


    Wednesday, November 27, 2013 9:07 PM