C#绘制圆角矩形
来源:互联网 发布:互联网职位 知乎 编辑:程序博客网 时间:2024/06/10 17:32
C#使用图形组合的方法绘制圆角矩形
在C#中的System.Drawing.Graphics类里为我们提供了诸多绘图方法,但圆角矩形这个形状却没有提供方法去绘制,但是我们可以通过图形组合的方法来绘制圆角矩形,如图。
代码:
using System.Drawing;using System.Drawing.Drawing2D;/// <summary>/// 填充一个圆角矩形/// </summary>/// <param name="b">源图片</param>/// <param name="brush">笔刷</param>/// <param name="x">X坐标</param>/// <param name="y">Y坐标</param>/// <param name="width">宽度</param>/// <param name="height">高度</param>/// <param name="radius">半径</param>private void FillRoundRect(Bitmap b, Brush brush, int x, int y, int width, int height, int radius){ //防止半径过大 if (radius * 2 > width) radius = width / 2; if (radius * 2 > height) radius = height / 2; Graphics g = Graphics.FromImage(b); //防止锯齿 g.SmoothingMode = SmoothingMode.HighQuality; //绘制四个圆角 g.FillEllipse(brush, x, y, radius * 2, radius * 2); g.FillEllipse(brush, x + (width - radius * 2), y, radius * 2, radius * 2); g.FillEllipse(brush, x, y + (height - radius * 2), radius * 2, radius * 2); g.FillEllipse(brush, x + (width - radius * 2), y + (height - radius * 2), radius * 2, radius * 2); //绘制横竖两个矩形 g.FillRectangle(brush, x + radius, y, width - radius * 2, height); g.FillRectangle(brush, x, y + radius, width, height - radius * 2);}
调用也很简单:
Bitmap b = new Bitmap(150, 150);FillRoundRect(b, new SolidBrush(), 0, 0, 150, 150, 30);pic_Image.Image = b;
效果图:
这样一来,填充圆角矩形是实现了,但绘制边框就要用到另一种方法了:绘制四个角的圆弧,再绘制四条边。
代码:
using System.Drawing;using System.Drawing.Drawing2D;/// <summary>/// 绘制一个圆角矩形/// </summary>/// <param name="b">源图片</param>/// <param name="pen">笔</param>/// <param name="x">X坐标</param>/// <param name="y">Y坐标</param>/// <param name="width">宽度</param>/// <param name="height">高度</param>/// <param name="radius">半径</param>private void DrawRoundRect(Bitmap b, Pen pen, int x, int y, int width, int height, int radius){ //防止半径过大 if (radius * 2 > width) radius = width / 2; if (radius * 2 > height) radius = height / 2; Graphics g = Graphics.FromImage(b); //防止锯齿 g.SmoothingMode = SmoothingMode.HighQuality; //绘制四个圆弧 g.DrawArc(pen, x, y, radius * 2, radius * 2, 270, -90); g.DrawArc(pen, x + (width - radius * 2), y, radius * 2, radius * 2, -90, 90); g.DrawArc(pen, x, y + (height - radius * 2), radius * 2, radius * 2, 90, 90); g.DrawArc(pen, x + (width - radius * 2), y + (height - radius * 2), radius * 2, radius * 2, 0, 90); //四条直线边 g.DrawLine(pen, x, y + radius, x, y + height - radius); g.DrawLine(pen, x + width, y + radius, x + width, y + height - radius); g.DrawLine(pen, x + radius, y, x + width - radius, y); g.DrawLine(pen, x + radius, y + height, x + width - radius, y + height);}
调用同上,效果图:
当然,你也可以用最普遍的GraphicsPath的方法来绘制圆角矩形。
阅读全文
1 0
- C#绘制圆角矩形
- C# GDI+ 绘制圆角实心矩形
- C#绘制浮点矩形
- Ps绘制圆角矩形
- GDI+绘制圆角矩形
- MFC绘制圆角矩形
- Canvas绘制圆角矩形
- GDI+绘制圆角矩形
- android绘制圆角矩形
- ios-矩形-圆角矩形-椭圆-圆形的绘制
- 渐变,圆角矩形体的绘制
- android view绘制圆角矩形(转)
- 绘制圆角矩形(方法 )
- drawRoundRect方法:绘制圆角矩形
- drawRoundRect方法:绘制圆角矩形
- 绘制圆角矩形和文字
- canvas.drawRoundRect方法,绘制圆角矩形
- 在Canvas中绘制圆角矩形
- 深度学习网络卷积在GPU上的优化
- 数据结构思维 第十五章 爬取维基百科
- Eclipse Neon
- 理解正向代理和反向代理
- ubuntu kde Plasma5 图标无法自定义问题
- C#绘制圆角矩形
- springboot vue 安装 每天进步百分之一
- cannot be read or is not a valid ZIP file
- Linux学习——Linux常用命令-文件处理命令
- XSHELL工具上传文件到Linux以及下载文件到本地(Windows)
- spring boot 部署
- LintCode刷题——最长上升连续子序列
- code profiling
- poj_2752_kmp_nxt_application_求s 所有子串是s的前缀又是后缀的长度