• C# Winform编程(8)GDI+绘图


    简介

    GDI(Graphics Device Interface)图像设备接口,属于绘图方面的API。System.Drawing命名空间提供了对GDI+基本图形功能的访问。

    System.Drawing命名空间

    System.Drawing命名空间中的常用类:

    说明
    Bitmap封装GDI+位图,此位图由图形图像及其属性的像素数据组成。
    Brush画刷,定义用于填充图形形状(如:矩形、椭圆、饼图、多边形和封闭路径)的内部对象
    Font字体,定义特定的文本格式,包括:字体、字号和字形属性。无法继承此类
    Graphics封装一个GDI+绘图图面。无法继承此类
    Pen画笔,定义用于绘制直线和曲线的对象。无法继承此类
    Region指示由矩形和路径构成的图形形状的内部。无法继承此类

    System.Drawing命名空间中的常用结构:

    结构说明
    Color表示RGB颜色
    Point表示二维平面中定义的点、整数X和Y坐标的有序对
    Rectangle存储一组整数,共4个,表示一个矩形的位置和大小
    Size存储一个有序整数对,通常为矩形的宽度和高度

    Graphics 类

    Graphics类封装了一个GDI+绘图界面,提供将对象绘制到显示设备的方法。
    Graphics类的常用方法:

    名称说明
    Dispose释放由Graphics使用的所有资源
    DrawEllipse绘制一个由边框(该边框由一对坐标、高度和宽度指定)定义的椭圆
    DrawArc绘制弧形
    DrawLine绘制直线
    DrawPolygon绘制由一组Point结构定义的多边形
    DrawRectangle绘制由坐标对、宽度和高度指定的矩形
    DrawPie绘制一个扇形,该形状由一个坐标对、宽度、高度以及两天射线所指定的椭圆定义
    DrawCurse绘制曲线,由参数Point数组指定
    FillEllips填充边框所定义的椭圆的内部,该边框由一对坐标、一个宽度和一个高度指定
    FillRegion填充Region的内部
    ScaleTransform将指定的缩放操作应用与此Graphics
    TanslateTransform平移坐标系原点

    Pen类

    Pen类可以设置画笔的颜色,线条的粗细和线条的样式(实现、虚线等)。画笔是绘图的工具,Graphics对象是绘画的场所。

    Brush类

    Brush类(画刷),用于填充图形。该类是一个抽象基类,不能直接实例化。
    Brush类的派生类:

    名称说明
    ImageBrush图像绘制区域
    LinearGradientBrush线性渐变绘制区域
    RadialGradientBrush径向渐变绘制区域,焦点定义渐变的开始,椭圆定义渐变的重点
    SolidColorBrush单色绘制区域
    VideoBrush视频内容绘制区域

    Font类

    Font类,绘制文本时,可以设置字体的样式、大小、以及字体的种类。

    using System.Drawing.Drawing2D;
    using System.Numerics;
    using System.Security.Cryptography.Xml;
    using System.Threading;
    
    
    namespace GDIDemo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            /// 
            /// 绘制直线
            /// 
            /// 
            /// 
            private void button1_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen p = new Pen(Color.Black);
                g.DrawLine(p, 0, this.Height / 2, this.Width, this.Height / 2);
                p.Dispose();
                g.Dispose();
            }
    
    
            /// 
            /// 绘制矩形
            /// 
            /// 
            /// 
            private void button2_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen p = new Pen(Color.Red);
                g.DrawRectangle(p, 50, 50, 200, 200);
                p.Dispose();
                g.Dispose();
            }
    
            /// 
            /// 绘制圆形
            /// 
            /// 
            /// 
            private void button3_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen p = new Pen(Color.Blue);
                g.DrawEllipse(p, 50, 50, 100, 100);
                p.Dispose();
                g.Dispose();
            }
    
            /// 
            /// 绘制圆柱
            /// 
            /// 
            /// 
            private void button4_Click(object sender, EventArgs e)
            {
                int height = this.ClientSize.Height - 100;
                int width = this.ClientSize.Width - 50;
                int vHeight = 200;
                int vWidth = 100;
    
                Graphics g = this.CreateGraphics();
                g.Clear(Color.White);
                Pen pen = new Pen(Color.Gray);
                SolidBrush brush = new SolidBrush(Color.Gainsboro);
                for (int i = height / 2; i > 0; i--)
                {
                    g.DrawEllipse(pen, width / 2, i, vHeight, vWidth);
                }
                g.FillEllipse(brush, width / 2, 0, vHeight, vWidth);
            }
    
            /// 
            /// 绘制填充矩形
            /// 
            /// 
            /// 
            private void button5_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen pen = new Pen(Color.Blue);
                Brush brush = pen.Brush;
                Rectangle rect = new Rectangle(50, 50, 100, 100);
                g.FillRectangle(brush, rect);
                pen.Dispose();
                g.Dispose();
            }
    
            /// 
            /// 绘制渐变圆形
            /// 
            /// 
            /// 
            private void button6_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Rectangle rect = new Rectangle(150, 50, 200, 200);
                LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Orange, Color.Purple, 90);
                g.FillEllipse(brush, rect);
                brush.Dispose();
                g.Dispose();
            }
    
            /// 
            /// 绘制文字
            /// 
            /// 
            /// 
            private void button7_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen pen = new Pen(Color.Blue);
                Font f = new Font("隶书", 24, FontStyle.Italic);
                g.DrawString("Windows 应用程序设计", f, pen.Brush, 50, 50);
                pen.Dispose();
                g.Dispose();
            }
    
    
            private void button9_Click(object sender, EventArgs e)
            {
                using (Graphics g = this.CreateGraphics())
                {
                    g.Clear(Color.White);
                }
            }
            private void Form1_Paint(object sender, PaintEventArgs e)
            {
    #if false
                Graphics g = this.CreateGraphics();
                g.Clear(Color.White);
                Pen myPen = new Pen(Color.Red, 3);
                g.DrawRectangle(myPen, 0, 0, 200, 100);
                g.DrawEllipse(myPen, 0, 0, 200, 100);
                g.Dispose();
                myPen.Dispose();
    #endif
            }
    
            /// 
            /// 坐标平移
            /// 
            /// 
            /// 
            private void button8_Click(object sender, EventArgs e)
            {
    
                Graphics g = this.CreateGraphics();
                g.Clear(Color.White);
                Pen myPen = new Pen(Color.Red, 3);
    
                g.DrawRectangle(myPen, 0, 0, 200, 100);
                g.DrawEllipse(myPen, 0, 0, 200, 100);
                Thread.Sleep(1000);
    
                g.Clear(Color.White);
                g.TranslateTransform(30, 30);
                g.DrawRectangle(myPen, 0, 0, 200, 100);
                g.DrawEllipse(myPen, 0, 0, 200, 100);
                g.Dispose();
                myPen.Dispose();
            }
    
            /// 
            /// 坐标缩放
            /// 
            /// 
            /// 
            private void button10_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                g.Clear(Color.White);
                Pen myPen = new Pen(Color.Red, 3);
                g.DrawRectangle(myPen, 0, 0, 200, 100);
                g.DrawEllipse(myPen, 0, 0, 200, 100);
                Thread.Sleep(1000);
    
                g.Clear(Color.White);
                g.ScaleTransform(1.5f, 2.0f); // 横轴放到1.5倍,纵轴放大2倍
                g.DrawRectangle(myPen, 0, 0, 200, 100);
                g.DrawEllipse(myPen, 0, 0, 200, 100);
                g.Dispose();
                myPen.Dispose();
    
    
            }
    
            /// 
            /// 绘制波形
            /// 
            /// 
            /// 
    
            private void button11_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                g.Clear(Color.White);
                Pen pen = new Pen(Color.Blue, 3);
                Point p1 = new Point(30, this.ClientSize.Height - 200);
                Point p2 = new Point(this.ClientSize.Width - 100, this.ClientSize.Height - 200);
                Point p3 = new Point(30, 30);
                g.DrawLine(pen, p1, p2);
                g.DrawLine(pen, p1, p3);
                Font f = new Font("宋体", 12, FontStyle.Bold);
                g.DrawString("x", f, pen.Brush, p2);
                g.DrawString("y", f, pen.Brush, 10, 10);
    
    
                double x1, x2, y1, y2, a;
                x1 = x2 = y1 = y2 = 0;
                y2 = this.ClientSize.Height - 200;
                for (x2 = 0; x2 < this.ClientSize.Width; x2++)
                {
                    a = 2 * Math.PI * x2 / this.ClientSize.Width;
                    y2 = Math.Sin(a);
                    y2 = (1 - y2) * (this.ClientSize.Height - 200) / 2;
                    g.DrawLine(pen, (int)(x1 + 30), (int)y1, (int)(x2 + 30), (int)y2);
                    x1 = x2;
                    y1 = y2;
                }
    
    
            }
    
            /// 
            /// 绘制饼状图
            /// 
            /// 
            /// 
            private void button12_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Rectangle rect = new Rectangle(50, 50, 200, 100);
                Brush brush = new SolidBrush(Color.Blue);
                g.FillPie(brush, rect, 0, 60);
                g.FillPie(brush, rect, 60, 150);
                brush = new SolidBrush(Color.Yellow);
                g.FillPie(brush, rect, 210, 150);
                brush.Dispose();
                g.Dispose();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252

    在这里插入图片描述

    PictureBox图像控件

    PictureBox控件,图片框是操作图形图像的基本控件,主要用于显示,保存图形图像信息,其主要属性方法:

    • 属性:
      • Image:设置或获取与该控件显示的图像
      • SizeMode:指示如何显示图像
    • 方法:
      • Load:显示图像

    Bitmap类

    Bitmap类,封装GDI+位图,此位图由图形图像及其属性的像素数据组成。Bitmap是用于处理由像素数据定义的图像的对象,其常用属性及方法:

    名称说明
    Size获取图像的以像素为单位的宽度和高度
    Width获取图像的宽度
    Height获取图像的高度
    FromFile()从指定的文件创建图像
    FromStream()从指定的数据流创建图像
    GetPixel获取此Bitmap中指定像素的颜色
    MakeTransparent使默认的透明颜色对此Bitmap透明
    Save将此图像以指定的格式保存到指定的流中
    RotateFlip旋转、镜像或同时旋转和镜像图像
    
            /// 
            /// 创建位图并保存为图片
            /// 
            /// 
            /// 
            private void button13_Click(object sender, EventArgs e)
            {
                Bitmap bm = new Bitmap(this.ClientSize.Width, this.ClientSize.Height,
                    System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                Graphics g = Graphics.FromImage(bm);
    
                Font f = new Font("隶书", 24, FontStyle.Italic);
                Pen pen = new Pen(Color.OrangeRed);
                g.DrawString("创建位图测试保存图像", f, pen.Brush, 0, 0);
                pictureBox1.Image = bm;
                pictureBox1.Show();
    
                bm.Save("c:\\temp\\1.bmp");
                pen.Dispose();
                g.Dispose();
            }
    
            /// 
            /// 加载图片并另存图片
            /// 
            /// 
            /// 
            private void button14_Click(object sender, EventArgs e)
            {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.Filter = "图像文件*.bmp|*.bmp|*.jpg|*.png";
                if (openfile.ShowDialog() == DialogResult.OK)
                { 
                    pictureBox1.Image = Image.FromFile(openfile.FileName);
                    pictureBox1.Image.Save("c:\\temp\\2.bmp");
                }
            }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    Pytorch面试题面经
    CMake基础
    用VS Code搞Qt6:编译源代码与基本配置
    Fortran语言程序设计01 函数与子例行程序
    云原生之深入解析Kubernetes集群内的服务通信机制
    C++11 条件变量
    你了解TCP协议吗(二)?
    代码随想录算法训练营第五十八天 | 583. 两个字符串的删除操作 & 72. 编辑距离
    一篇了解Java Web中的数据库开发的JDBC基础
    java版Spring Cloud+Mybatis+Oauth2+分布式+微服务+实现工程管理系统
  • 原文地址:https://blog.csdn.net/u013420428/article/details/134011502