C# 画圆角矩形

protected void Page_Load(object sender, EventArgs e)
{
        Bitmap bm = new Bitmap(800, 600);
        Graphics g = Graphics.FromImage(bm);
        g.FillRectangle(Brushes.White,new Rectangle(0,0,800,600));
        FillRoundRectangle(g,Brushes.Plum,new Rectangle(100, 100, 100, 100), 8);
        DrawRoundRectangle(g, Pens.Yellow,new Rectangle(100, 100, 100, 100), 8);
        bm.Save(Response.OutputStream, ImageFormat.Jpeg);
        g.Dispose();
        bm.Dispose();
}
 public static void DrawRoundRectangle(Graphics g,Pen pen,Rectangle rect, int cornerRadius)
{
        using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
        {
            g.DrawPath(pen, path);
        }
}
public static void FillRoundRectangle(Graphics g, Brush brush,Rectangle rect, int cornerRadius)
{
        using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
        {
            g.FillPath(brush, path);
        }
}
internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
{
        GraphicsPath roundedRect = new GraphicsPath();
        roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
        roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
        roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
        roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
        roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
        roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
        roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
        roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
        roundedRect.CloseFigure();
        return roundedRect;
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xujh/archive/2007/04/17/1568378.aspx

圆角矩形渐变可以通过以下步骤实现: 1. 一个填充了渐变色的矩形。 2. 剪切出圆角区域,可以通过使用路径和剪切函数来实现。 3. 在剪切出的圆角区域内使用渐变色填充函数,以实现圆角矩形渐变。 以下是一个简单的圆角矩形渐变算法示例,使用C语言和SDL库: ```c #include <SDL.h> void draw_rounded_gradient_rect(SDL_Renderer *renderer, int x, int y, int w, int h, int radius, SDL_Color start_color, SDL_Color end_color) { // 一个渐变色矩形 SDL_Rect rect = { x, y, w, h }; SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawColor(renderer, start_color.r, start_color.g, start_color.b, start_color.a); SDL_RenderFillRect(renderer, &rect); SDL_SetRenderDrawColor(renderer, end_color.r, end_color.g, end_color.b, end_color.a); SDL_RenderDrawRect(renderer, &rect); // 剪切出圆角区域 SDL_Rect clip_rect = { x, y, w, h }; SDL_RenderSetClipRect(renderer, &clip_rect); SDL_RenderDrawLine(renderer, x + radius, y, x + w - radius, y); SDL_RenderDrawLine(renderer, x + radius, y + h, x + w - radius, y + h); SDL_RenderDrawLine(renderer, x, y + radius, x, y + h - radius); SDL_RenderDrawLine(renderer, x + w, y + radius, x + w, y + h - radius); const int diameter = radius * 2; int cx = x + radius; int cy = y + radius; for (int i = 0; i < 4; i++) { int ax = (i == 1 || i == 2) ? x + w - diameter : x; int ay = (i == 2 || i == 3) ? y + h - diameter : y; SDL_RenderDrawLine(renderer, cx, ay, cx, ay + diameter); SDL_RenderDrawLine(renderer, ax, cy, ax + diameter, cy); int dx = ax + ((i == 1 || i == 2) ? radius : diameter - radius); int dy = ay + ((i == 2 || i == 3) ? radius : diameter - radius); SDL_RenderDrawArc(renderer, dx, dy, radius, i * 90, (i + 1) * 90); } // 在圆角区域内使用渐变色填充函数 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawColor(renderer, start_color.r, start_color.g, start_color.b, start_color.a); for (int i = 0; i < radius; i++) { float t = (float)i / radius; SDL_Color color = { start_color.r + (end_color.r - start_color.r) * t, start_color.g + (end_color.g - start_color.g) * t, start_color.b + (end_color.b - start_color.b) * t, start_color.a + (end_color.a - start_color.a) * t }; int dx = cx - i; int dy = cy - i; int dw = i * 2; int dh = i * 2; SDL_Rect gradient_rect = { dx, dy, dw, dh }; SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); SDL_RenderFillRect(renderer, &gradient_rect); } } ``` 需要注意的是,该算法仅演示了如何实现圆角矩形渐变,如果需要更加复杂的效果,可以根据实际需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值