I want to draw one continuous line/curve with varying thickness using C#. If I set the width of the Pen as fixed, thickness will not vary. How is it possible to change the thickness of line as it is drawn? Any help is appreciated.
2 Answers
General approach: split the line into segments/points and make several draw calls with different pens (as mentioned in other posts).
Now for curves - if you do not want to reimplement their algorithms, you might wanna use GraphicsPath
to convert arbitrary paths to line segments (with sufficient resolution).
- add your path (lines, curves, beziers, ...) to a fresh
GraphicsPath
instance - call
Flatten
to letGraphicsPath
perform the magic (= conversion to lines only) - iterate over
PathPoints
to get the endpoints of corresponding line segments - subdivide them further until they are short enough for your "varying" pen strategy

- 3,192
- 13
- 28
-
I have created a graphicspath: GraphicsPath path = new GraphicsPath(); and then added other required paths to it: path.AddPath(path2, false); This is done in a loop. Now, next step to change the thickness? – AUser123 Sep 12 '13 at 11:29
-
...as I described, call Flatten, get the line segments, subdivide them as required, THEN draw each of them with a custom pen with thickness of your choice. – olydis Sep 12 '13 at 11:32
-
and you don't need a new path object if you already have a path `path2` – olydis Sep 12 '13 at 11:34
You can only vary the thickness of the line if you draw the line point by point, and then vary the thickness of the points.
To find out how to draw lines point by point, look up line drawing algorithms if you don't have them handy. One example for drawing straight lines is Bresenham's algorithm. You can find out more about that at http://en.wikipedia.org/wiki/Bresenham's_line_algorithm.
Then when you plot a dot, you can choose the thickness of the dot. So instead of just drawing a single pixel, you draw a circle with radius r, where r is the thickness of the line you want at that position in the line.

- 32,551
- 8
- 60
- 76