C#GDI+ 绘制线段(实线或虚线)、矩形、字符串、圆、椭圆

C#GDI+ 绘制线段(实线或虚线)、矩形、字符串、圆、椭圆

绘制基本线条和图形 比较简单,直接看代码。

             Graphics graphics = e.Graphics;

             //绘制实线
using (Pen pen = new Pen(Color.Black, ))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; //实线
graphics.DrawLine(pen,,,,);
} //画出虚线
using (Pen pen = new Pen(Color.Black, ))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线
graphics.DrawLine(pen, , , , );
} //画出矩形
using (Pen pen = new Pen(Color.Black, ))
{
Rectangle rectangle = new Rectangle(,,,);
graphics.DrawRectangle(pen,rectangle);
} //绘制字符串
string str = "Hello 笨男孩!";
graphics.DrawString(str, this.Font, Brushes.Black, , ); //测量字符串的高度和宽度
SizeF szfTitle = graphics.MeasureString(str, this.Font);
graphics.DrawString(str, this.Font, Brushes.Black, , -(int)szfTitle.Height);
graphics.DrawString("该字符串高度:"+szfTitle.Height+" 字符串宽度:"+szfTitle.Width, this.Font, Brushes.Black, , ); //绘制圆
using(Pen pen = new Pen(Color.Black,))
{
//圆
graphics.DrawEllipse(pen,,,,); //在画板上画椭圆,起始坐标为(10,10),外接矩形的宽为100,高为100 此时就是一个圆 //椭圆
graphics.DrawEllipse(pen, , , , ); //在画板上画椭圆,起始坐标为(10,10),外接矩形的宽为100,高为50 此时就是一个椭圆
}

绘制效果

C#GDI+ 绘制线段(实线或虚线)、矩形、字符串、圆、椭圆

源代码工程文件下载

上一篇:openssl 对称加密算法enc命令详解


下一篇:Yii Framework2.0开发教程(5)数据库mysql性能