前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【愚公系列】2023年12月 GDI+绘图专题 颜色获取和图形绘制

【愚公系列】2023年12月 GDI+绘图专题 颜色获取和图形绘制

原创
作者头像
愚公搬代码
修改2023-12-28 08:13:13
2050
修改2023-12-28 08:13:13
举报
文章被收录于专栏:历史专栏历史专栏

🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,阿里云专家博主,腾讯云优秀博主,掘金优秀博主,51CTO博客专家等。 🏆《近期荣誉》:2022年CSDN博客之星TOP2,2022年华为云十佳博主等。 🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。 🏆🎉欢迎 👍点赞✍评论⭐收藏

🚀前言

颜色获取和图形绘制是计算机图形学中两个基本操作。

颜色获取是指从图像或者其他的颜色源中获取颜色值的过程。在计算机中,颜色值通常由RGB值来表示,即红、绿、蓝三原色的取值组合。通常可以通过鼠标选择选取颜色和颜色值,或者通过程序代码指定颜色值来获取颜色。

图形绘制是指将计算机中的图形数据以某种方式显示在屏幕或者其他输出设备上的过程。实现图形绘制通常需要使用图形库或者图形引擎,它们会提供各种绘制函数和绘制命令,比如直线、矩形、圆形、填充等绘制函数。在开发中,可以通过调用这些函数完成图形的绘制。

🚀一、颜色获取和图形绘制

🔎1.颜色获取

  1. 获取预定义颜色:

预定义颜色可以直接通过Color类的属性获取,如下所示:

代码语言:csharp
复制
Color redColor = Color.Red;
Color blueColor = Color.Blue;
Color greenColor = Color.Green;
  1. RGB颜色值:

可以通过RGB值来获取颜色,如下所示:

代码语言:csharp
复制
Color myColor = Color.FromArgb(redValue, greenValue, blueValue);
btnColor.BackColor = Color.FromArgb(255,Color.Red);//第一个参数是透明度,0都255,0表示完全透明
  1. 十六进制颜色值:

可以通过十六进制值来获取颜色,如下所示:

代码语言:csharp
复制
Color myColor = ColorTranslator.FromHtml("#FF0000");
  1. 随机颜色:

可以通过Random类来生成随机颜色,如下所示:

代码语言:csharp
复制
Random random = new Random();
Color randomColor = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
  1. 渐变色:

可以通过ColorBlend类和LinearGradientBrush类来实现渐变色,如下所示:

代码语言:csharp
复制
ColorBlend colorBlend = new ColorBlend();
colorBlend.Colors = new Color[] { Color.Red, Color.Yellow, Color.Green };
colorBlend.Positions = new float[] { 0f, 0.5f, 1f };
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(100, 100), Color.Red, Color.Green);
linearGradientBrush.InterpolationColors = colorBlend;
  1. 纹理填充:

可以通过TextureBrush类来实现纹理填充,如下所示:

代码语言:csharp
复制
Image image = Image.FromFile("texture.jpg");
TextureBrush textureBrush = new TextureBrush(image, WrapMode.Tile);
  1. 线性渐变颜色:

可以通过ColorBlend类和LinearGradientBrush类来实现线性渐变颜色,如下所示:

代码语言:csharp
复制
ColorBlend colorBlend = new ColorBlend();
colorBlend.Colors = new Color[] { Color.Red, Color.Yellow, Color.Green };
colorBlend.Positions = new float[] { 0f, 0.5f, 1f };
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(100, 100), Color.Red, Color.Green);
linearGradientBrush.InterpolationColors = colorBlend;
  1. 径向渐变颜色:

可以通过ColorBlend类和PathGradientBrush类来实现径向渐变颜色,如下所示:

代码语言:csharp
复制
ColorBlend colorBlend = new ColorBlend();
colorBlend.Colors = new Color[] { Color.Red, Color.Yellow, Color.Green };
colorBlend.Positions = new float[] { 0f, 0.5f, 1f };
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddEllipse(0, 0, 100, 100);
PathGradientBrush pathGradientBrush = new PathGradientBrush(graphicsPath);
pathGradientBrush.CenterPoint = new Point(50, 50);
pathGradientBrush.InterpolationColors = colorBlend;
  1. FromKnownColor
代码语言:csharp
复制
btnColor.BackColor = Color.FromKnownColor(KnownColor.Control );
  1. FromName
代码语言:csharp
复制
btnColor.BackColor = Color.FromName(“blue”);

🔎2.图形绘制

🦋2.1 内置图形(扇形,圆角矩形,多边形)

代码语言:csharp
复制
public partial class lblFont : Form
{
    int index = 0;
    public lblFont()
    {
        InitializeComponent();
        //SetStyle(ControlStyles.UserPaint, true);
        //SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
        //SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
        SetStyle(ControlStyles.ResizeRedraw, true);//空间大小改变时,控件会重绘
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics graphics = this.CreateGraphics();
        graphics.SmoothingMode = SmoothingMode.AntiAlias;
        int width = this.Width;
        int height = this.Height;

        Rectangle rct = new Rectangle(0, 0, width / 4, height / 4);
        Pen pen = new Pen(Color.Red);
        graphics.DrawArc(pen, rct, 0, 120);//绘制弧线,弧线是由Rectangle构成的椭圆的的弧线组成,顺时针从0到360,水平向右是0度。

        rct = new Rectangle(0, 0, width / 3, width / 3);
        Brush brush = new SolidBrush(Color.Green);
        graphics.DrawPie(pen, rct, 0, 120);//绘制扇形

        graphics.FillPie(brush, rct, 0, 120);//填充扇形;

        GraphicsPath graphicsPath = DrawAnyShapeWithPath(new Rectangle(200, 200, 100, 100), 10);
        graphics.DrawPath(pen, graphicsPath);//绘制圆角矩形

        graphics.FillPath(brush, graphicsPath);//填充圆角矩形



        graphics.DrawPolygon(pen, new Point[] { new Point(50, 50), new Point(60, 50), new Point(50, 70) });//绘制多边形



        pen.Dispose();
        graphics.Dispose();

    }

    /// <summary>
    /// 默认各个路径的首位会相连
    /// </summary>
    /// <param name="rectangle"></param>
    /// <param name="radius"></param>
    GraphicsPath DrawAnyShapeWithPath(Rectangle rectangle, int radius)
    {
        int r;
        if (4 * radius < rectangle.Width && 4 * radius < rectangle.Height)
        {
            r = radius;
        }
        else
        {
            r = rectangle.Width / 4;
        }
        GraphicsPath graphicsPath = new GraphicsPath();
        graphicsPath.AddArc(new Rectangle(rectangle.X, rectangle.Y, 2 * r, 2 * r), 180, 90);
        graphicsPath.AddArc(new Rectangle(rectangle.X + rectangle.Width - 2 * r, rectangle.Y, 2 * r, 2 * r), 270, 90);
        graphicsPath.AddArc(new Rectangle(rectangle.X + rectangle.Width - 2 * r, rectangle.Y + rectangle.Height - 2 * r, 2 * r, 2 * r), 0, 90);
        graphicsPath.AddArc(new Rectangle(rectangle.X, rectangle.Y + rectangle.Height - 2 * r, 2 * r, 2 * r), 90, 90);
        graphicsPath.CloseFigure();
        return graphicsPath;
    }

}

🦋2.2 自定义图形(圆形控件)

代码语言:csharp
复制
public partial class lblFont : Form
{
    public lblFont()
    {
        InitializeComponent();
        //SetStyle(ControlStyles.UserPaint, true);
        //SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
        //SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
        SetStyle(ControlStyles.ResizeRedraw, true);//空间大小改变时,控件会重绘
    }

    protected override void OnPaint(PaintEventArgs e)
    {

        base.OnPaint(e);

        int width = this.Width;
        int height = this.Height;
        int radius = width < height ? width : height;//以最短的边最为圆的直径
        Graphics graphics = this.CreateGraphics();
        //graphics.Clear( Color .FromArgb (255,240,240,240) );
        Rectangle rectangle = new Rectangle(0, 0, radius, radius);


        graphics.SmoothingMode = SmoothingMode.AntiAlias;//消除锯齿
        Brush brush = new SolidBrush(Color.Red);//指定画刷的颜色
        graphics.FillEllipse(brush, new Rectangle(0, 0, radius, radius));//填充一个圆


        Pen pen = new Pen(Color.Black, 2);//指定画笔的颜色和线宽
        graphics.DrawEllipse(pen, rectangle);//绘制圆的边界

        string text = this.Text;
        brush = new SolidBrush(Color.Black);//指定画刷画文本的颜色

        Font font = this.Font;

        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Center;//字符水平对齐方式
        sf.LineAlignment = StringAlignment.Center;//字符垂直对齐方式
        graphics.DrawString(text, font, brush, new RectangleF(0, 0, radius, radius), sf);


        #region  使控件的单击事件只在圆内触发,而不是在矩形控件范围内触发,消除在圆外和控件内的区域响应单机事件的bug
        System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        path.AddEllipse(0, 0, radius, radius);
        this.Region = new Region(path);
        #endregion


        //font.Dispose();//当控件的size改变时,如果调用了 font.Dispose(),那么font.Height会报异常,因为没有重新实例化font类,导致DrawString会报参数无效,所以也可以不调用font.dispose,这样就可以不用捕获异常了
        brush.Dispose();
        pen.Dispose();
        graphics.Dispose();

    }

}

我正在参与2023腾讯技术创作特训营第四期有奖征文,快来和我瓜分大奖!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 🚀前言
  • 🚀一、颜色获取和图形绘制
    • 🔎1.颜色获取
      • 🔎2.图形绘制
        • 🦋2.1 内置图形(扇形,圆角矩形,多边形)
        • 🦋2.2 自定义图形(圆形控件)
    相关产品与服务
    云开发 CloudBase
    云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档