C# 绘图

本文介绍了如何使用Windows GDI+库在.NET中创建一个带有圆角矩形和文字的图像,并展示了绘制上传指示和图片插入的功能。通过Graphics类操作Bitmap,演示了如何绘制图形路径和填充颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private void ImagePic()
{
    Bitmap bmp = new Bitmap(200, 25);
    Graphics g = Graphics.FromImage(bmp);

    g.DrawString("上传", Font, new SolidBrush(Color.Red), new PointF(5, 5));

    this.BackgroundImage = bmp;
    g.Dispose();
}
/// <summary>
/// 绘制圆角矩形
/// </summary>
private void MyDrawPath()
{
    Bitmap bmp = new Bitmap(200, 25);
    Graphics g = Graphics.FromImage(bmp);

    // 圆角半径
    int cRadius = 3;
    // 要实现 圆角化的 矩形
    Rectangle rect = new Rectangle(0, 0, Width - cRadius, Height - cRadius);

    // 指定图形路径, 有一系列 直线/曲线 组成
    GraphicsPath linepath = new GraphicsPath();
    linepath.StartFigure();
    linepath.AddArc(new Rectangle(new Point(rect.X, rect.Y), new Size(2 * cRadius, 2 * cRadius)), 180, 90);
    linepath.AddLine(new Point(rect.X + cRadius, rect.Y), new Point(rect.Right - cRadius, rect.Y));
    linepath.AddArc(new Rectangle(new Point(rect.Right - 2 * cRadius, rect.Y), new Size(2 * cRadius, 2 * cRadius)), 270, 90);
    linepath.AddLine(new Point(rect.Right, rect.Y + cRadius), new Point(rect.Right, rect.Bottom - cRadius));
    linepath.AddArc(new Rectangle(new Point(rect.Right - 2 * cRadius, rect.Bottom - 2 * cRadius), new Size(2 * cRadius, 2 * cRadius)), 0, 90);
    linepath.AddLine(new Point(rect.Right - cRadius, rect.Bottom), new Point(rect.X + cRadius, rect.Bottom));
    linepath.AddArc(new Rectangle(new Point(rect.X, rect.Bottom - 2 * cRadius), new Size(2 * cRadius, 2 * cRadius)), 90, 90);
    linepath.AddLine(new Point(rect.X, rect.Bottom - cRadius), new Point(rect.X, rect.Y + cRadius));
    linepath.CloseFigure();
    g.DrawPath(new Pen(Color.Red, 1), linepath);

    //填充图形
    g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(90, 0, 38, 24));
}
//绘制图片
string path=@"F:\Res\Main\";
Image img= Image.FromFile(path+"icon_cancel.png");
g.DrawImage(img, new PointF(130, 0));
img = Image.FromFile(path + "icon_cancel2.png");
g.DrawImage(img, new PointF(170, 0));

https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.graphics?view=netframework-4.0

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值