coc*_*est 8 .net c# image rounded-corners
我想用GDI +创建带圆角的图像(来自另一个).最好的方法是什么?
PS:它不适用于网络,因此我无法使用客户端CSS
Gar*_*hby 22
这个功能似乎做你想要的.如果需要,还可以轻松修改它以返回位图.你还需要清理你不再需要的任何图像等.改编自: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);
using(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...
Run Code Online (Sandbox Code Playgroud)
使用Graphics.SetClip()方法是最好的方法.例如:
public static Image OvalImage(Image img) {
Bitmap bmp = new Bitmap(img.Width, img.Height);
using (GraphicsPath gp = new GraphicsPath()) {
gp.AddEllipse(0, 0, img.Width, img.Height);
using (Graphics gr = Graphics.FromImage(bmp)) {
gr.SetClip(gp);
gr.DrawImage(img, Point.Empty);
}
}
return bmp;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我最终结合了/sf/answers/123135141/和/sf/answers/123145781/来获得我的圆形图像,因为我希望背景是透明的。以为我会分享它:
private Image RoundCorners(Image image, int cornerRadius)
{
cornerRadius *= 2;
Bitmap roundedImage = new Bitmap(image.Width, image.Height);
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);
using (Graphics g = Graphics.FromImage(roundedImage))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.SetClip(gp);
g.DrawImage(image, Point.Empty);
}
return roundedImage;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17887 次 |
| 最近记录: |