Day17_C#_GDI


01. 主体使用对象

  >> Graphics 

02. 各种绘制

 1      void DrawLine()
 2         {
 3             Graphics gp = pnl_1.CreateGraphics();
 4             Pen pen = new Pen(Color.FromName("blue"));
 5             gp.DrawLine(pen, 100, 100, 200, 200);
 6         }
 7 
 8         void DrawRect()
 9         {
10             Graphics gp = pnl_1.CreateGraphics();
11             Pen pen = new Pen(Color.FromName("blue"));
12             pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
13             gp.DrawRectangle(pen, 100, 100, 150, 150);
14         }
15 
16         void DrawCircle()
17         {
18             Graphics gra = pnl_1.CreateGraphics();
19             gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
20             Pen pen = new Pen(Color.Green);//画笔颜色
21             pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
22             gra.DrawEllipse(pen, 300, 10, 100, 100);//画椭圆的方法,x坐标、y坐标、宽、高,如果是100,则半径为50
23         }
24 
25         void DrawCircleFilled()
26         {
27             Graphics gra = pnl_1.CreateGraphics();
28             gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
29             Brush bush = new SolidBrush(Color.Green);//填充的颜色
30             gra.FillEllipse(bush, 10, 10, 100, 100);//画填充椭圆的方法,x坐标、y坐标、宽、高,如果是100,则半径为50
31         }
32 
33         void DrawTest()
34         {
35             Graphics gra = pnl_1.CreateGraphics();
36             Font myFont = new Font("宋体", 24, FontStyle.Bold);
37             Brush bush = new SolidBrush(Color.Red);//填充的颜色
38             gra.DrawString("测试文本!", myFont, bush, 130, 50);
39         }
40 
41 
42         /// 
43         /// 生成二维码
44         /// 
45         /// 信息
46         /// 版本 1 ~ 40
47         /// 像素点大小
48         /// 图标路径
49         /// 图标尺寸
50         /// 图标边框厚度
51         /// 二维码白边
52         /// 位图
53         ///   imagecode("123456", 2, 5, "", 0, 0, false);//生成二维码
54         public static Bitmap imagecode(string msg, int version, int pixel, string icon_path, int icon_size, int icon_border, bool white_edge)
55         {
56             QRCoder.QRCodeGenerator code_generator = new QRCoder.QRCodeGenerator();
57 
58             QRCoder.QRCodeData code_data = code_generator.CreateQrCode(msg, QRCoder.QRCodeGenerator.ECCLevel.M/* 这里设置容错率的一个级别 */, true, true, QRCoder.QRCodeGenerator.EciMode.Utf8, version);
59 
60             QRCoder.QRCode code = new QRCoder.QRCode(code_data);
61 
62             //Bitmap icon = new Bitmap(icon_path);
63 
64             // Bitmap bmp = code.GetGraphic(pixel, Color.Black, Color.White, icon, icon_size, icon_border, white_edge);
65             //Bitmap bmp = code.GetGraphic(pixel, Color.Black, Color.White, white_edge);
66             Bitmap bmp = code.GetGraphic(pixel, Color.Green, Color.White, white_edge);
67 
68             return bmp;
69 
70         }
71 
72         void DrawCode()
73         {
74             //using System.Drawing;
75             //先创建一个画布,来绘制一个宽长为:300,300的画布。
76             var imagedata = new Bitmap(300, 300);
77             var sourcegra = Graphics.FromImage(imagedata);//存入画布
78             sourcegra.DrawString("品牌", new Font("宋体", 20, FontStyle.Regular), new SolidBrush(Color.Red), 20, 100);//在画布绘制字体
79             var qrImage = imagecode("123456", 2, 5, "", 0, 0, false);//生成二维码
80             sourcegra.DrawImage(qrImage, 100, 100, 100, 100);//绘制二维码到画布
81             //imagedata.Save("D://1.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);//保存图片到D盘
82             //将图片赋值给PictureBox
83             pic_code.Image = imagedata  ;
84 
85             //直接画图片
86             Graphics gra = pnl_1.CreateGraphics();
87             gra.DrawImage(qrImage, 10, 300, 150, 150);
88         }