C# 提供两种切割圆形图片的方式
效果:
private void Form1_Load(object sender, EventArgs e) { string file =System.IO.Path.Combine(Environment.CurrentDirectory, @"11.jpg"); try { Image i = new Bitmap(file); pbO.Image = i; pbD1.Image = WayOne(file); pbD2.Image = WayTwo(file); pbS1.Image = WaySOne(file); pbS2.Image = WaySTwo(file); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private Bitmap WayOne(string file) { using (Image i = new Bitmap(file)) { Bitmap b = new Bitmap(i.Width, i.Height); using (Graphics g = Graphics.FromImage(b)) { g.FillEllipse(new TextureBrush(i), 0, 0, i.Width, i.Height); } return b; } } private Bitmap WayTwo(string file) { using (Image i = new Bitmap(file)) { Bitmap b = new Bitmap(i.Width, i.Height); using (Graphics g = Graphics.FromImage(b)) { g.DrawImage(i, 0, 0, b.Width, b.Height); int r = Math.Min(b.Width, b.Height) / 2; PointF c = new PointF(b.Width / 2.0F, b.Height / 2.0F); for (int h = 0; h < b.Height; h++) for (int w = 0; w < b.Width; w++) if ((int)Math.Pow(r, 2) < ((int)Math.Pow(w * 1.0 - c.X, 2) + (int)Math.Pow(h * 1.0 - c.Y, 2))) { b.SetPixel(w, h, Color.Transparent); } } return b; } } private Bitmap WaySOne(string file) { using (Image i = new Bitmap(file)) { Bitmap b = new Bitmap(i.Width, i.Height); using (Graphics g = Graphics.FromImage(b)) { g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; using (System.Drawing.Drawing2D.GraphicsPath p = new System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Alternate)) { p.AddEllipse(0, 0, i.Width, i.Height); g.FillPath(new TextureBrush(i), p); } //g.FillEllipse(new TextureBrush(i), 0, 0, i.Width, i.Height); } return b; } } private Bitmap WaySTwo(string file) { using (Image i = new Bitmap(file)) { Bitmap b = new Bitmap(i.Width, i.Height); using (Graphics g = Graphics.FromImage(b)) { g.DrawImage(i, 0, 0, b.Width, b.Height); int r = Math.Min(b.Width, b.Height) / 2; PointF c = new PointF(b.Width / 2.0F, b.Height / 2.0F); for (int h = 0; h < b.Height; h++) for (int w = 0; w < b.Width; w++) if ((int)Math.Pow(r, 2) < ((int)Math.Pow(w * 1.0 - c.X, 2) + (int)Math.Pow(h * 1.0 - c.Y, 2))) { b.SetPixel(w, h, Color.Transparent); } //画背景色圆 using (Pen p = new Pen(System.Drawing.SystemColors.Control)) g.DrawEllipse(p, 0, 0, b.Width, b.Height); } return b; } }

分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
2013-09-26 'extern'