gpt4 book ai didi

c# - 如何在 C# 中创建带圆角的图像?

转载 作者:太空狗 更新时间:2023-10-29 18:01:19 48 4
gpt4 key购买 nike

我想用 GDI+ 创建带圆角的图像(来自另一个图像)。执行此操作的最佳方法是什么?

PS:它不适用于网络,所以我不能使用客户端 CSS

最佳答案

这个函数似乎可以做你想做的。如果需要,还可以轻松修改它以返回位图。您还需要清理不再需要的任何图像等。改编自: 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...

关于c# - 如何在 C# 中创建带圆角的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1758762/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com