C#画图GDI Grphic
作者:Lynn 日期:2009-03-12
GDI是Graphics Device Interface的缩写,含义是图形设备接口,它的主要任务是负责系统与绘图程序之间的信息交换,处理所有Windows程序的图形输出。
在Windows操作系统下,绝大多数具备图形界面的应用程序都离不开GDI,我们利用GDI所提供的众多函数就可以方便的在屏幕、打印机及其它输出设备上输出图形,文本等操作。GDI的出现使程序员无需要关心硬件设备及设备驱动,就可以将应用程序的输出转化为硬件设备上的输出,实现了程序开发者与硬件设备的隔离,大大方便了开发工作。
GDI是如何实现输出的?
要想在屏幕或者其它输出设备上输出图形或者文字,那么我们就必须先获得一个称为设备描述表( DC:Device Context)的对象的句柄,以它为参数,调用各种GDI函数实现各种文字或图形的输出。设备描述表是GDI内部保存数据的一种数据结构,此结构中的属性内容与特定的输出设备(显示器,打印机等)相关,属性定义了GDI函数的工作细节,在这里属性确定了文字的颜色,x坐标和y坐标映射到窗口显示区域的方式等。
设备描述表句柄一旦获得,那么系统将使用默认的属性值填充设备描述表结构。
如果有必要,我们可以使用一些GDI函数获取和改变设备描述表中的属性值。
1.位图上绘制点和线
System.Drawing.Image MyImage=new System.Drawing.Bitmap (w,h);//申请位图对象
System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(MyImage); //申请画图对象
//用白色C清出除
Color C=Color.FromArgb(255,255,255);
g.Clear(C);
//画线
g.DrawLine(Pens.Black,x1,y1,x2,y2);
//画一个象素的点
//MyBitmap.SetPixel(x, y, Color.White);
g.DrawImage(MyImage,0,0,h,w); //pictureBox1在(0,0)到(h,w)范围画点
pictureBox1.Image=MyImage; //这个图片贴到pictureBox1控件上
2.在窗体上绘制图形
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);//画笔
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);//画刷
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(0,0,100,200));//画实心椭圆
formGraphics.DrawEllipse(myPen, new Rectangle(0,0,100,200));//空心圆
formGraphics.FillRectangle(myBrush, new Rectangle(0,0,100,200));//画实心方
formGraphics.DrawRectangle(myPen, new Rectangle(0,0,100,200));//空心矩形
formGraphics.DrawLine(myPen, 0, 0, 200, 200);//画线
formGraphics.DrawPie(myPen,90,80,140,40,120,100); //画馅饼图形
//画多边形
formGraphics.DrawPolygon(myPen,new Point[]{ new Point(30,140), new Point(270,250), new Point(110,240), new Point
(200,170), new Point(70,350), new Point(50,200)});
//清理使用的资源
myPen.Dispose();
myBrush.Dispose();
formGraphics.Dispose();
3.在窗体上绘制文本
//在窗体上绘制竖向文本
System.Drawing.Graphics formGraphics = this.CreateGraphics();
string drawString = "Text";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = 150.0f;
float y = 50.0f;
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);//文本
垂直
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);//绘制文本
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);//绘制垂直文本
//清理使用的资源
drawFont.Dispose();
drawBrush.Dispose();
formGraphics.Dispose();
4.其他画笔
//该画刷的HatchStyle有DiagonalCross、 ForwardDiagonal、Horizontal、 Vertical、 Solid等不同风格
HatchStyle hs = HatchStyle.Cross; //十字
HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red);
g.FillRectangle(sb,50,50,150,150);
Rectangle r = new Rectangle(500, 300, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lb, r);
//PathGradientBrush路径渐变,LinearGradientBrush线性渐变
Image bgimage = new Bitmap("E:2065215396.jpg");
brush = new TextureBrush(bgimage); //易一幅图作椭圆的背景
g.FillEllipse(brush,50,50,500,300);
5.其他技巧
//申请画图区域
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
//创建笔
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
myPen.DashStyle=DashStyle.Dash //DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等风格
//创建单色画刷
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
//画图函数
protected override void OnPaint ( System.Windows.Forms.PaintEventArgs e )
{
}
//颜色对话框
ColorDialog c = new ColorDialog();
c.ShowDialog();
textBox1.BackColor = c.Color;
//字体对话框
FontDialog f = new FontDialog();
f.ShowDialog();
textBox1.Font = flg.Font;
//RGB的使用
int Red=(int)SelfPlaceKey.GetValue("Red");
int Green=(int)SelfPlaceKey.GetValue("Green");
int Blue=(int)SelfPlaceKey.GetValue("Blue");
BackColor=Color.FromArgb(Red,Green,Blue);
//字体
Font aFont=new Font("Arial",16,FontStyle.Bold|FontStyle.Italic);
rect=new Rectangle(0,y,400,aFont.Height);
g.DrawRectangle(Pens.Blue,rect);
StringFormat sf=new StringFormat();
sf.Alignment=StringAlignment.Far;
g.DrawString("This text is right justified.",aFont,Brushes.Blue,rect,sf);
y+=aFont.Height+20;
aFont.Dispose();
在Windows操作系统下,绝大多数具备图形界面的应用程序都离不开GDI,我们利用GDI所提供的众多函数就可以方便的在屏幕、打印机及其它输出设备上输出图形,文本等操作。GDI的出现使程序员无需要关心硬件设备及设备驱动,就可以将应用程序的输出转化为硬件设备上的输出,实现了程序开发者与硬件设备的隔离,大大方便了开发工作。
GDI是如何实现输出的?
要想在屏幕或者其它输出设备上输出图形或者文字,那么我们就必须先获得一个称为设备描述表( DC:Device Context)的对象的句柄,以它为参数,调用各种GDI函数实现各种文字或图形的输出。设备描述表是GDI内部保存数据的一种数据结构,此结构中的属性内容与特定的输出设备(显示器,打印机等)相关,属性定义了GDI函数的工作细节,在这里属性确定了文字的颜色,x坐标和y坐标映射到窗口显示区域的方式等。
设备描述表句柄一旦获得,那么系统将使用默认的属性值填充设备描述表结构。
如果有必要,我们可以使用一些GDI函数获取和改变设备描述表中的属性值。
1.位图上绘制点和线
System.Drawing.Image MyImage=new System.Drawing.Bitmap (w,h);//申请位图对象
System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(MyImage); //申请画图对象
//用白色C清出除
Color C=Color.FromArgb(255,255,255);
g.Clear(C);
//画线
g.DrawLine(Pens.Black,x1,y1,x2,y2);
//画一个象素的点
//MyBitmap.SetPixel(x, y, Color.White);
g.DrawImage(MyImage,0,0,h,w); //pictureBox1在(0,0)到(h,w)范围画点
pictureBox1.Image=MyImage; //这个图片贴到pictureBox1控件上
2.在窗体上绘制图形
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);//画笔
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);//画刷
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(0,0,100,200));//画实心椭圆
formGraphics.DrawEllipse(myPen, new Rectangle(0,0,100,200));//空心圆
formGraphics.FillRectangle(myBrush, new Rectangle(0,0,100,200));//画实心方
formGraphics.DrawRectangle(myPen, new Rectangle(0,0,100,200));//空心矩形
formGraphics.DrawLine(myPen, 0, 0, 200, 200);//画线
formGraphics.DrawPie(myPen,90,80,140,40,120,100); //画馅饼图形
//画多边形
formGraphics.DrawPolygon(myPen,new Point[]{ new Point(30,140), new Point(270,250), new Point(110,240), new Point
(200,170), new Point(70,350), new Point(50,200)});
//清理使用的资源
myPen.Dispose();
myBrush.Dispose();
formGraphics.Dispose();
3.在窗体上绘制文本
//在窗体上绘制竖向文本
System.Drawing.Graphics formGraphics = this.CreateGraphics();
string drawString = "Text";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = 150.0f;
float y = 50.0f;
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);//文本
垂直
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);//绘制文本
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);//绘制垂直文本
//清理使用的资源
drawFont.Dispose();
drawBrush.Dispose();
formGraphics.Dispose();
4.其他画笔
//该画刷的HatchStyle有DiagonalCross、 ForwardDiagonal、Horizontal、 Vertical、 Solid等不同风格
HatchStyle hs = HatchStyle.Cross; //十字
HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red);
g.FillRectangle(sb,50,50,150,150);
Rectangle r = new Rectangle(500, 300, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lb, r);
//PathGradientBrush路径渐变,LinearGradientBrush线性渐变
Image bgimage = new Bitmap("E:2065215396.jpg");
brush = new TextureBrush(bgimage); //易一幅图作椭圆的背景
g.FillEllipse(brush,50,50,500,300);
5.其他技巧
//申请画图区域
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
//创建笔
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
myPen.DashStyle=DashStyle.Dash //DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等风格
//创建单色画刷
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
//画图函数
protected override void OnPaint ( System.Windows.Forms.PaintEventArgs e )
{
}
//颜色对话框
ColorDialog c = new ColorDialog();
c.ShowDialog();
textBox1.BackColor = c.Color;
//字体对话框
FontDialog f = new FontDialog();
f.ShowDialog();
textBox1.Font = flg.Font;
//RGB的使用
int Red=(int)SelfPlaceKey.GetValue("Red");
int Green=(int)SelfPlaceKey.GetValue("Green");
int Blue=(int)SelfPlaceKey.GetValue("Blue");
BackColor=Color.FromArgb(Red,Green,Blue);
//字体
Font aFont=new Font("Arial",16,FontStyle.Bold|FontStyle.Italic);
rect=new Rectangle(0,y,400,aFont.Height);
g.DrawRectangle(Pens.Blue,rect);
StringFormat sf=new StringFormat();
sf.Alignment=StringAlignment.Far;
g.DrawString("This text is right justified.",aFont,Brushes.Blue,rect,sf);
y+=aFont.Height+20;
aFont.Dispose();
评论: 0 | 引用: 0 | 查看次数: -
发表评论
上一篇
下一篇

文章来自:
Tags: