使用 .NET 创建手绘效果

发布于 2024-08-03 15:03:18 字数 352 浏览 2 评论 0原文

我想编写一段代码,创建与 'Balsamiq Mockups' 或 'yUML' 使用 .NET 框架。

谁能告诉我如何使用GDI+实现手绘铅笔效果?

显然可以使用正确的字体来完成文本 - 我的问题是如何渲染线条、方框和圆圈。

谢谢!

I'd like to write a piece of code which creates images similar in style to those rendered by 'Balsamiq Mockups' or 'yUML' using the .NET framework.

Can anybody tell me how to achieve the hand-drawn pencil effect using GDI+?

The text can obviously be done using the right font - my question is how to render the lines, boxes and circles.

Thanks!

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

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

发布评论

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

评论(1

夜深人未静 2024-08-10 15:03:18

GDI+ 最适合绘制直角型图形,但您可以使用它来产生如下效果:

Drawn Square

...通过使用 Graphics 对象的 DrawBezier 方法。下面是渲染上图的代码片段:

Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
    g.FillRectangle(new SolidBrush(Color.White),
        new Rectangle(0, 0, bmp.Width, bmp.Height));
    Pen pen = new Pen(Color.Gray, 7.0F);
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

    g.DrawBezier(pen,
        new Point(10, 10),
        new Point(11, 41),
        new Point(7, 147),
        new Point(13, 199));

    g.DrawBezier(pen,
        new Point(7, 10),
        new Point(87, 13),
        new Point(213, 17),
        new Point(319, 6));

    g.DrawBezier(pen,
        new Point(319, 4),
        new Point(305, 53),
        new Point(299, 107),
        new Point(319, 203));

    g.DrawBezier(pen,
        new Point(13, 199),
        new Point(33, 195),
        new Point(150, 207),
        new Point(319, 203));

}
pictureBox1.Image = bmp;

这种效果的关键是使用大宽度的 Pen(本例中为 7.0F),并将 Graphics 对象的 SmoothingMode 设置为 HighQuality(因为这看起来像默认的 ass)平滑模式)。

编写自定义方法相对容易,您可以在其中指定要以正常 GDI+ 方式绘制的形状(矩形、坐标和半径等),然后您的代码将这些元素的线条转换为贝塞尔坐标,您可以在其中稍微改变物体在每个方向上随机放置几个像素。

GDI+ is best suited for drawing right-angle-type graphics, but you can use it to produce an effect like this:

Drawn Square

... by using the DrawBezier method of the Graphics object. Here's the code snippet that renders the above image:

Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
    g.FillRectangle(new SolidBrush(Color.White),
        new Rectangle(0, 0, bmp.Width, bmp.Height));
    Pen pen = new Pen(Color.Gray, 7.0F);
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

    g.DrawBezier(pen,
        new Point(10, 10),
        new Point(11, 41),
        new Point(7, 147),
        new Point(13, 199));

    g.DrawBezier(pen,
        new Point(7, 10),
        new Point(87, 13),
        new Point(213, 17),
        new Point(319, 6));

    g.DrawBezier(pen,
        new Point(319, 4),
        new Point(305, 53),
        new Point(299, 107),
        new Point(319, 203));

    g.DrawBezier(pen,
        new Point(13, 199),
        new Point(33, 195),
        new Point(150, 207),
        new Point(319, 203));

}
pictureBox1.Image = bmp;

The keys to this effect are using a Pen with a large width (7.0F in this example), and setting the SmoothingMode of your Graphics object to HighQuality (since this looks like ass with the default SmoothingMode).

It would be relatively easy to write custom methods where you specify shapes to be drawn in the normal GDI+ way (rectangles and coordinates and radii and so forth), and then your code translates the lines of these elements into Bezier coordinates where you slightly alter the positions of things randomly by a few pixels in each direction.

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