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 开发者_JAVA技巧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):
Here is an example of a "nice" curve quickly painted in "photoshop" with the brush tool:
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!
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.
You're not actually using GDI to draw lines and so your Smoothing and InterpolationMode settings have no effect. You're simply drawing an image per every point in a point array and so there's no connecting those points or any kind of antialiasing. Try converting your points collection into a path and using g.DrawPath to draw your curve.
A simpler, though Bezier-less, example of this would be to use the DrawLines method. Something like:
g.DrawLines(Pens.Blue, points.ToArray());
You don't even need a loop for DrawLines and DrawPath. DrawLines is like a poor man's DrawPath...
精彩评论