如何创建图像在C#中圆角? [英] How to create image with rounded corners in C#?

查看:380
本文介绍了如何创建图像在C#中圆角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建映像(从另一个)与圆角与GDI +。什么是做到这一点的最好方法是什么?

I'd like to create image (from another one) with rounded corners with GDI+. What's the best way to do this?

PS:这是不是网络,所以无法利用客户端的CSS

PS: it's not for web, so I cannot make use of client CSS

推荐答案

这个功能似乎做你想要的。它也可以很容易地进行修改,如果需要返回一个位图。您还需要清理摘自不再需要任何图像,等等。: <一href="http://www.jigar.net/howdoi/viewhtmlcontent98.aspx">http://www.jigar.net/howdoi/viewhtmlcontent98.aspx

This function seems to do what you want. It can also easily be modified to return a Bitmap if needed. You'll also need to clean up any images you no longer want, etc.. Adapted from: http://www.jigar.net/howdoi/viewhtmlcontent98.aspx

using System.Drawing;
using System.Drawing.Drawing2D;

public Image RoundCorners(Image StartImage, int CornerRadius, Color BackgroundColor)
{
    CornerRadius *= 2;
    Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height);
    Graphics g = Graphics.FromImage(RoundedImage);
    g.Clear(BackgroundColor);
    g.SmoothingMode = SmoothingMode.AntiAlias;
    Brush brush = new TextureBrush(StartImage);
    GraphicsPath gp = new GraphicsPath();
    gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90);
    gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90);
    gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
    gp.AddArc(0, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
    g.FillPath(brush, gp);
    return RoundedImage;
}

Image StartImage = Image.FromFile("YourImageFile.jpg");
Image RoundedImage = this.RoundCorners(StartImage, 25, Color.White);
//Use RoundedImage...

这篇关于如何创建图像在C#中圆角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆