使用 GDI / Winforms 抗锯齿贝塞尔曲线 - c# .net

发布于 2024-12-06 09:04:18 字数 909 浏览 1 评论 0原文

我正在尝试在示例 Winforms 应用程序中绘制贝塞尔曲线。

我正在计算贝塞尔曲线点,然后使用 DrawImage 在每个点上绘制自定义图像画笔。

然而,我并没有完全得到我所希望的结果 - 结果曲线在弯曲的点处并不平滑(注意 Y 坐标以 1px 增加/减少)

: sstatic.net/P7rrU.jpg" alt="起伏的曲线">

以下是使用画笔工具在“photoshop”中快速绘制“漂亮”曲线的示例:

Nice curve

有谁知道如何实现这种“抗锯齿”?

我基本上是这样做的:

        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):

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼角的笑意。 2024-12-13 09:04:18

您可能会得到这个,因为您的 points 集合包含 Point 类型的结构,它使用 Int32 - 因此,您正在量化您的自己点。

尝试使用 PointF - 这允许您在任意位置绘制图像,而不是围绕整数位置进行量化。

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.

不疑不惑不回忆 2024-12-13 09:04:18

您实际上并没有使用 GDI 来绘制线条,因此您的 Smoothing 和 InterpolationMode 设置不起作用。您只需为点数组中的每个点绘制一个图像,因此不需要连接这些点或任何类型的抗锯齿。尝试将点集合转换为路径并使用 g.DrawPath 绘制曲线。

一个更简单但没有贝塞尔曲线的示例是使用 DrawLines 方法。类似于:

g.DrawLines(Pens.Blue, points.ToArray());

您甚至不需要 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:

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文