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   此时就是一个椭圆
}

我这里绘制的是panel的边框,需要减去线宽。

using (Pen pen = new Pen(Color.LightGray, 2))
{
    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线
    e.Graphics.DrawRectangle(pen, pen.Width, pen.Width, panel1.Width-2*pen.Width, panel1.Height - 2*pen.Width);
}
  • 显示结果:
    C# GDI+ 绘制线段(实线或虚线)、矩形、字符串、圆、椭圆

?

上一篇:JAVA数据库连接池C3p0 以及阿里Druid提供的连接池


下一篇:Photoshop画笔绘制绚烂的壁纸