Search code examples
.netgdiantialiasingbeziercurve

Antialiased bezier curve with GDI / Winforms - c# .net


I'm trying to paint a bezier curve in a sample Winforms application.

I'm calculating the bezier points, and then painting using DrawImage to draw a custom image brush on each point.

However I'm not exactly getting the result I was hoping for - the resulting curve is not smooth at the points that it bends (note the Y coordinates are increased / decreased with 1px):

Choppy curve

Here is an example of a "nice" curve quickly painted in "photoshop" with the brush tool:

Nice curve

Does anyone know how to achieve this kind of "antialiasing"?

I'm basically doing this:

        using(var g = Graphics.FromImage(bitmap))
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //points - an array with calculated beziere curve points
            //image - the "image brush" that is painted at each curve point
            foreach (var p in points)
            {
                g.DrawImage(image, p);
                g.Flush();
            }
        }

Thank you!


Solution

  • You're probably getting this because your points collection contains structs of type Point, which uses Int32 - as a result, you're quantizing your points yourself.

    Try using PointF instead - this allows you to draw images at any arbitrary location, instead of being quantized around integer locations.