在 C# 中绘制带有圆角、边框和渐变填充的图像

发布于 2024-10-05 05:48:36 字数 168 浏览 6 评论 0原文

我到处都看了,用谷歌搜索了所有东西,但找不到任何好东西。 我需要的是一个能够绘制带有边框和渐变填充的圆角图像(图形)的类(每个角不同是一个加号)。

我发现的所有示例都存在一些缺陷(例如质量差、缺少功能等)。

我将使用它与 ashx 一起绘制图像,然后将其显示给用户。

谢谢!

I've looked everywhere and googled everything and couldn't find anything good.
What I need is a class that is able to draw an image (graphics) with rounded corners (different on each corner is a plus) with a border and gradient fill.

All the examples I find have some flaws (like bad quality, missing functionality etc).

I will use this with a ashx that will draw the image and then show it to the user.

Thanks!

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

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

发布评论

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

评论(2

痴意少年 2024-10-12 05:48:36

GraphicsPath 允许您绘制相对自由的形状,然后可以对其进行填充用渐变画笔。下面的示例代码将创建一个具有两个不同圆角和渐变填充的矩形。

    GraphicsPath gp = new GraphicsPath();
    gp.AddLine(new Point(10, 10), new Point(75, 10));
    gp.AddArc(50, 10, 50, 50, 270, 90);
    gp.AddLine(new Point(100, 35), new Point(100, 100));
    gp.AddArc(80, 90, 20, 20, 0, 90);
    gp.AddLine(new Point(90, 110), new Point(10, 110));
    gp.AddLine(new Point(10, 110), new Point(10, 10));
    Bitmap bm = new Bitmap(110, 120);
    LinearGradientBrush brush = new LinearGradientBrush(new Point(0, 0), new Point(100, 110), Color.Red, Color.Yellow);
    using (Graphics g = Graphics.FromImage(bm))
    {
        g.FillPath(brush, gp);
        g.DrawPath(new Pen(Color.Black, 1), gp);
        g.Save();
    }
    bm.Save(@"c:\bitmap.bmp");

结果如下图:

alt text

The GraphicsPath allows you to draw relatively free form shapes which you can then fill with a gradient brush. The below example code will create a rectangle with two differntly rounded corners and a gradient fill.

    GraphicsPath gp = new GraphicsPath();
    gp.AddLine(new Point(10, 10), new Point(75, 10));
    gp.AddArc(50, 10, 50, 50, 270, 90);
    gp.AddLine(new Point(100, 35), new Point(100, 100));
    gp.AddArc(80, 90, 20, 20, 0, 90);
    gp.AddLine(new Point(90, 110), new Point(10, 110));
    gp.AddLine(new Point(10, 110), new Point(10, 10));
    Bitmap bm = new Bitmap(110, 120);
    LinearGradientBrush brush = new LinearGradientBrush(new Point(0, 0), new Point(100, 110), Color.Red, Color.Yellow);
    using (Graphics g = Graphics.FromImage(bm))
    {
        g.FillPath(brush, gp);
        g.DrawPath(new Pen(Color.Black, 1), gp);
        g.Save();
    }
    bm.Save(@"c:\bitmap.bmp");

This result in the following image:

alt text

日记撕了你也走了 2024-10-12 05:48:36

我认为您需要创建自己的方法,使用图形对象并“手动”(阅读“使用代码”)创建图像。最简单的方法是创建一个图形对象,添加一个圆圈,然后在图像的每个象限中添加所需的额外内容,然后将对象分成四份。或者将整个内容作为一张图像返回,然后使用 CSS 精灵将图像放置在具有正确坐标的正确位置(可能是更好的解决方案,因为它减少了对图形库的调用并仅返回一个文件,因此减少了对 Web 的调用服务器)。

I think you'll need to create your own method, using a graphics object and "manually" (read "with code") create the image. Easiest way would be to create a single graphics object, add a circle, then in each quadrant of the image add the extras you need, then split the object into fourths. Or return the whole thing as one image then use CSS sprites to place the image in the right spots with the right coordinates (probably the better solution as it uses less calls to the graphics library and returns just one file, so less calls to the web server).

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