首页
学习
活动
专区
圈层
工具
发布
50 篇文章
1
【愚公系列】2023年11月 WPF控件专题 Line控件详解
2
【愚公系列】2023年11月 WPF控件专题 2023秋招WPF高频面试题
3
【愚公系列】2023年11月 Winform控件专题 Label控件详解
4
【愚公系列】2023年11月 Winform控件专题 Button控件详解
5
【愚公系列】2023年11月 Winform控件专题 Form控件详解
6
【愚公系列】2023年11月 Winform控件专题 TextBox控件详解
7
【愚公系列】2023年11月 Winform控件专题 CheckBox控件详解
8
【愚公系列】2023年11月 Winform控件专题 RadioButton控件详解
9
【愚公系列】2023年11月 Winform控件专题 ComboBox控件详解
10
【愚公系列】2023年11月 Winform控件专题 RichTextBox控件详解
11
【愚公系列】2023年11月 Winform控件专题 ListBox控件详解
12
【愚公系列】2023年11月 Winform控件专题 CheckedListBox控件详解
13
【愚公系列】2023年11月 Winform控件专题 numericUpDown控件详解
14
【愚公系列】2023年11月 Winform控件专题 LinkLabel控件详解
15
【愚公系列】2023年11月 Winform控件专题 DateTimePicker控件详解
16
【愚公系列】2023年11月 Winform控件专题 NotifyIcon控件详解
17
【愚公系列】2023年11月 Winform控件专题 ContextMenuStrip控件详解
18
【愚公系列】2023年11月 Winform控件专题 Timer控件详解
19
【愚公系列】2023年11月 Winform控件专题 PictureBox控件详解
20
【愚公系列】2023年11月 Winform控件专题 Chart控件详解
21
【愚公系列】2023年11月 Winform控件专题 DataGridView控件详解
22
【愚公系列】2023年11月 Winform控件专题 TableLayoutPanel控件详解
23
【愚公系列】2023年11月 Winform控件专题 ProgressBar控件详解
24
【愚公系列】2023年11月 Winform控件专题 Panel控件详解
25
【愚公系列】2023年11月 Winform控件专题 FlowLayoutPanel控件详解
26
【愚公系列】2023年11月 Winform控件专题 GroupBox控件详解
27
【愚公系列】2023年11月 Winform控件专题 SplitContainer控件详解
28
【愚公系列】2023年11月 Winform控件专题 TabControl控件详解
29
【愚公系列】2023年11月 Winform控件专题 ToolTip控件详解
30
【愚公系列】2023年11月 Winform控件专题 MaskedTextBox控件详解
31
【愚公系列】2023年11月 Winform控件专题 MonthCalendar控件详解
32
【愚公系列】2023年11月 Winform控件专题 TreeView控件详解
33
【愚公系列】2023年11月 Winform控件专题 MenuStrip控件详解
34
【愚公系列】2023年11月 Winform控件专题 OpenFileDialog控件详解
35
【愚公系列】2023年11月 Winform控件专题 SaveFileDialog控件详解
36
【愚公系列】2023年12月 Winform控件专题 FontDialog控件详解
37
【愚公系列】2023年12月 Winform控件专题 ColorDialog控件详解
38
【愚公系列】2023年12月 Winform控件专题 FolderBrowserDialog控件详解
39
【愚公系列】2023年12月 Winform控件专题 StatusStrip控件详解
40
【愚公系列】2023年12月 Winform控件专题 ToolStrip控件详解
41
【愚公系列】2023年12月 Winform控件专题 HelpProvider控件详解
42
【愚公系列】2023年12月 Winform控件专题 ToolStripContainer控件详解
43
【愚公系列】2023年12月 Winform控件专题 BackgroundWorker控件详解
44
【愚公系列】2023年12月 GDI+绘图专题 图形图像编程基础
45
【愚公系列】2023年12月 GDI+绘图专题 图形图像的重绘
46
【愚公系列】2023年12月 GDI+绘图专题 颜色获取和图形绘制
47
【愚公系列】2023年12月 GDI+绘图专题 Point
48
【愚公系列】2023年12月 GDI+绘图专题 Rectangle
49
【愚公系列】2023年12月 GDI+绘图专题 Matrix
50
【愚公系列】2023年12月 GDI+绘图专题 Pen

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

🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,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腾讯技术创作特训营第四期有奖征文,快来和我瓜分大奖!

下一篇
举报
领券