C#GDI 繪製線段(實線或虛線)、矩形、字串、圓、橢圓
阿新 • • 發佈:2018-11-13
C#GDI 繪製線段(實線或虛線)、矩形、字串、圓、橢圓
繪製基本線條和圖形 比較簡單,直接看程式碼。
1 Graphics graphics = e.Graphics; 2 3 //繪製實線 4 using (Pen pen = new Pen(Color.Black, 2)) 5 { 6 pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; //實現 7 graphics.DrawLine(pen,0,10,100,10); 8 } 9 10 //畫出虛線 11 using (Pen pen = new Pen(Color.Black, 2)) 12 { 13 pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虛線 14 graphics.DrawLine(pen, 0, 15, 100, 15); 15 } 16 17 //畫出矩形 18 using (Pen pen = new Pen(Color.Black, 2)) 19 { 20 Rectangle rectangle = new Rectangle(2,30,100,50); 21 graphics.DrawRectangle(pen,rectangle); 22 } 23 24 //繪製字串 25 string str = "Hello 笨男孩!"; 26graphics.DrawString(str, this.Font, Brushes.Black, 2, 100); 27 28 //測量字串的高度和寬度 29 SizeF szfTitle = graphics.MeasureString(str, this.Font); 30 graphics.DrawString(str, this.Font, Brushes.Black, 2, 200-(int)szfTitle.Height); 31 graphics.DrawString("該字串高度:"+szfTitle.Height+" 字串寬度:"+szfTitle.Width, this.Font, Brushes.Black, 2, 200); 32 33 34 35 //繪製圓 36 using(Pen pen = new Pen(Color.Black,2)) 37 { 38 //圓 39 graphics.DrawEllipse(pen,2,230,100,100); //在畫板上畫橢圓,起始座標為(10,10),外接矩形的寬為100,高為100 此時就是一個圓 40 41 //橢圓 42 graphics.DrawEllipse(pen, 2, 400, 100, 50); //在畫板上畫橢圓,起始座標為(10,10),外接矩形的寬為100,高為50 此時就是一個橢圓 43 }