1. 程式人生 > 其它 >C# GDI+ 繪製線段(實線或虛線)、矩形、字串、圓、橢圓

C# GDI+ 繪製線段(實線或虛線)、矩形、字串、圓、橢圓

Graphics graphics = e.Graphics;
//繪製實線
using (Pen pen = new Pen(Color.Black, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; //實線
graphics.DrawLine(pen,0,10,100,10);
}
//畫出虛線
using (Pen pen = new Pen(Color.Black, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虛線
graphics.DrawLine(pen, 0, 15, 100, 15);
}
//畫出矩形
using (Pen pen = new Pen(Color.Black, 2))
{
Rectangle rectangle = new Rectangle(2,30,100,50);
graphics.DrawRectangle(pen,rectangle);
}
//繪製字串
string str = "http://61office.cn/";
graphics.DrawString(str, this.Font, Brushes.Black, 2, 100);

//測量字串的高度和寬度
SizeF szfTitle = graphics.MeasureString(str, this.Font);
graphics.DrawString(str, this.Font, Brushes.Black, 2, 200-(int)szfTitle.Height);
graphics.DrawString("該字串高度:"+szfTitle.Height+" 字串寬度:"+szfTitle.Width, this.Font, Brushes.Black, 2, 200);

//繪製圓
using(Pen pen = new Pen(Color.Black,2))
{
//圓
graphics.DrawEllipse(pen,2,230,100,100); //在畫板上畫橢圓,起始座標為(10,10),外接矩形的寬為100,高為100 此時就是一個圓
//橢圓
graphics.DrawEllipse(pen, 2, 400, 100, 50); //在畫板上畫橢圓,起始座標為(10,10),外接矩形的寬為100,高為50 此時就是一個橢圓
}
-----------------------------------
©著作權歸作者所有:來自51CTO部落格作者usdoc的原創作品,請聯絡作者獲取轉載授權,否則將追究法律責任
C# GDI+ 繪製線段(實線或虛線)、矩形、字串、圓、橢圓
https://blog.51cto.com/usdoc/3244135