开发者

Draw image with rounded corners, border and gradient fill in C#

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!


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:

Draw image with rounded corners, border and gradient fill in C#


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜