使用 GDI / Winforms 抗锯齿贝塞尔曲线 - c# .net
我正在尝试在示例 Winforms 应用程序中绘制贝塞尔曲线。
我正在计算贝塞尔曲线点,然后使用 DrawImage 在每个点上绘制自定义图像画笔。
然而,我并没有完全得到我所希望的结果 - 结果曲线在弯曲的点处并不平滑(注意 Y 坐标以 1px 增加/减少)
: sstatic.net/P7rrU.jpg" alt="起伏的曲线">
以下是使用画笔工具在“photoshop”中快速绘制“漂亮”曲线的示例:
有谁知道如何实现这种“抗锯齿”?
我基本上是这样做的:
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();
}
}
谢谢!
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):
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会得到这个,因为您的
points
集合包含Point
类型的结构,它使用Int32
- 因此,您正在量化您的自己点。尝试使用 PointF - 这允许您在任意位置绘制图像,而不是围绕整数位置进行量化。
You're probably getting this because your
points
collection contains structs of typePoint
, which usesInt32
- 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.您实际上并没有使用 GDI 来绘制线条,因此您的 Smoothing 和 InterpolationMode 设置不起作用。您只需为点数组中的每个点绘制一个图像,因此不需要连接这些点或任何类型的抗锯齿。尝试将点集合转换为路径并使用 g.DrawPath 绘制曲线。
一个更简单但没有贝塞尔曲线的示例是使用 DrawLines 方法。类似于:
您甚至不需要 DrawLines 和 DrawPath 的循环。 DrawLines 就像穷人的 DrawPath...
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:
You don't even need a loop for DrawLines and DrawPath. DrawLines is like a poor man's DrawPath...