c#图形编程入门

一。如何:创建钢笔

 

此示例创建一个 Pen 对象。

System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Tomato);

可靠编程

当使用完需要消耗系统资源的对象(如 Pen 对象)后,应对其调用 Dispose

请参见

参考

概念

其他资源

 
 
 
二。如何:设置钢笔颜色

 

此示例更改预先存在的 Pen 对象的颜色。

myPen.Color = System.Drawing.Color.PeachPuff;

编译代码

此示例要求:

  • 名为 myPenPen 对象。

可靠编程

应在使用完需要消耗系统资源的对象(例如 Pen 对象)后,对其调用 Dispose

请参见

 
三。如何:创建实心画笔

 

此示例创建一个 SolidBrush 对象,Graphics 对象可以用它来填充形状。


System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
 

可靠编程

当使用完那些使用系统资源的对象(如 Brush 对象)后,应该对这些对象调用 Dispose

请参见

 
 
 
 
四。如何:在 Windows 窗体上绘制线条

 

此示例在窗体上绘制线条。

 

System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
formGraphics.Dispose();
 

编译代码

不能在 Load 事件处理程序中调用此方法。如果已调整该窗体的大小或者其他窗体遮蔽了该窗体,则不会重绘所绘制的内容。若要自动重绘内容,应该重写 OnPaint 方法。

可靠编程

应该始终对使用系统资源的任何对象(如 PenGraphics 对象)调用 Dispose

请参见

参考

其他资源

 
 
 
五。如何:绘制空心形状

 

此示例在窗体上绘制空心椭圆和空心矩形。

 

private void DrawEllipse()
{
    System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
    System.Drawing.Graphics formGraphics;
    formGraphics = this.CreateGraphics();
    formGraphics.DrawEllipse(myPen, new Rectangle(0, 0, 200, 300));
    myPen.Dispose();
    formGraphics.Dispose();
}

private void DrawRectangle()
{
    System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
    System.Drawing.Graphics formGraphics;
    formGraphics = this.CreateGraphics();
    formGraphics.DrawRectangle(myPen, new Rectangle(0, 0, 200, 300));
    myPen.Dispose();
    formGraphics.Dispose();
}
 

编译代码

不能在 Load 事件处理程序中调用此方法。如果已调整该窗体的大小或者其他窗体遮蔽了该窗体,则不会重绘所绘制的内容。若要自动重绘内容,应该重写 OnPaint 方法。

可靠编程

应该始终对使用系统资源的任何对象(如 PenGraphics 对象)调用 Dispose

请参见

 
六。如何:在 Windows 窗体上绘制实心矩形

 

此示例在窗体上绘制实心矩形。

 

System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
 

编译代码

不能在 Load 事件处理程序中调用此方法。如果已调整该窗体的大小或者其他窗体遮蔽了该窗体,则不会重绘所绘制的内容。若要自动重绘内容,应该重写 OnPaint 方法。

可靠编程

对任何消耗系统资源的对象(如 BrushGraphics 对象)都应调用 Dispose

请参见

 七。如何:在 Windows 窗体上绘制实心椭圆

 

此示例在窗体上绘制实心椭圆。

 

System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(0, 0, 200, 300));
myBrush.Dispose();
 
 

编译代码

不能在 Load 事件处理程序中调用此方法。如果已调整该窗体的大小或者其他窗体遮蔽了该窗体,则不会重绘所绘制的内容。若要自动重绘内容,应该重写 OnPaint 方法。

可靠编程

对任何消耗系统资源的对象(如 BrushGraphics 对象)都应调用 Dispose

请参见

其他资源


 
 
 
八。如何:在 Windows 窗体上绘制文本

 

下面的代码示例演示如何使用 GraphicsDrawString 方法在窗体上绘制文本。或者,可以使用 TextRenderer 在窗体上绘制文本。有关更多信息,请参见如何:用 GDI 绘制文本

 

public void DrawString()
{
    System.Drawing.Graphics formGraphics = this.CreateGraphics();
    string drawString = "Sample 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();
    formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
    drawFont.Dispose();
    drawBrush.Dispose();
    formGraphics.Dispose();
}
 

编译代码

不能在 Load 事件处理程序中调用 DrawString 方法。如果已调整该窗体的大小或者其他窗体遮蔽了该窗体,则不会重绘所绘制的内容。若要自动重绘内容,应该重写 OnPaint 方法。

可靠编程

以下情况可能会导致异常:

  • 未安装 Arial 字体。

请参见

任务

参考

其他资源

 
 
九。如何:在 Windows 窗体上绘制垂直文本

 

下面的代码示例演示如何使用 GraphicsDrawString 方法在窗体上绘制竖排文字。

public void DrawVerticalString()
{
    System.Drawing.Graphics formGraphics = this.CreateGraphics();
    string drawString = "Sample 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();
    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
    formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
    drawFont.Dispose();
    drawBrush.Dispose();
    formGraphics.Dispose();
}

 

编译代码

不能在 Load 事件处理程序中调用此方法。如果已调整该窗体的大小或者其他窗体遮蔽了该窗体,则不会重绘所绘制的内容。若要自动重绘内容,应该重写 OnPaint 方法。

可靠编程

以下情况可能会导致异常:

  • 未安装 Arial 字体。

请参见

参考

其他资源

 
 
十。如何:使用 GDI+ 呈现图像

 

可以在应用程序中使用 GDI+ 呈现以文件形式存在的图像。可通过以下方式做到这一点:创建一个 Image 类(如 Bitmap)的新对象,创建一个 Graphics 对象来引用要使用的绘图图面,然后调用 Graphics 对象的 DrawImage 方法。将在图形类所表示的绘图表面上绘制图像。可以在设计时使用图像编辑器创建和编辑图像文件,而在运行时使用 GDI+ 呈现图像。有关更多信息,请参见图像编辑器



 
 

用 GDI+ 呈现图像

  1. 创建一个对象,该对象表示要显示的图像。此对象必须是从 Image 继承的类的成员,如 BitmapMetafile。下面显示了一个示例:

 
// Uses the System.Environment.GetFolderPath to get the path to the 
// current user's MyPictures folder.
Bitmap myBitmap = new Bitmap
   (System.Environment.GetFolderPath
      (System.Environment.SpecialFolder.MyPictures));
 
 
 
 2. 创建一个 Graphics 对象来表示要使用的绘图图面。有关更多信息,请参见如何:创建用于绘制的 Graphics 对象
 
 
// Creates a Graphics object that represents the drawing surface of 
// Button1.
Graphics g = Button1.CreateGraphics();
 
 
 3. 调用图形对象的 DrawImage 以呈现图像。必须同时指定要绘制的图像以及将绘制它的位置的坐标。 
g.DrawImage(myBitmap, 1, 1);
 
 参见

任务

参考

概念

其他资源

 
 
十一。如何:创建特定形状的 Windows 窗体

 

此示例向窗体提供随该窗体一起调整大小的椭圆形状。

 

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath();
    shape.AddEllipse(0, 0, this.Width, this.Height);
    this.Region = new System.Drawing.Region(shape);
}


 
 
 

编译代码

此示例需要:

该示例重写 OnPaint 方法以更改窗体的形状。若要使用此代码,请将方法声明以及绘图代码复制到该方法中。

请参见

参考

其他资源

 

 
十二。如何:在 Windows 窗体中复制像素以减少闪烁 
 

在使一个简单图形产生动画效果时,用户有时可能会遇到闪烁或其他不需要的视觉效果。限制出现此问题的一种方法是对该图形使用“bitblt”进程。Bitblt 是颜色数据从像素原始矩形到像素目标矩形的“位块转换”。

在 Windows 窗体中,使用 Graphics 类的 CopyFromScreen 方法可以实现 bitblt。您可以在该方法的参数中指定源和目标(根据点)、要复制的区域大小和用于绘制新形状的图形对象。

在下面的示例中,使用 Paint 事件处理程序在窗体上绘制了一个形状。然后,使用 CopyFromScreen 方法复制了该形状。

Note注意

将窗体的 DoubleBuffered 属性设置为 true 将使 Paint 事件中基于图形的代码被双缓冲。但在使用下面的代码时不会有任何明显的性能提升,这一点在使用更加复杂的图形操作代码时必须记住。

 
 
 
 
private void Form1_Paint(System.Object sender,
    System.Windows.Forms.PaintEventArgs e)
        {
        e.Graphics.FillEllipse(Brushes.DarkBlue, new
            Rectangle(10,10,60,60));
        e.Graphics.FillRectangle(Brushes.Khaki, new
            Rectangle(20,30,60,10));
        e.Graphics.CopyFromScreen(new Point(10, 10), new Point(100, 100), 
            new Size(70, 70));
}
 
 

编译代码

上面的代码在窗体的 Paint 事件处理程序中运行,从而在重新绘制窗体时仍然会保持图形。同样地,不要在 Load 事件处理程序中调用图形相关的方法,因为如果窗体大小经过调整或被其他窗体遮盖后,已绘制的内容将不会重新绘制。

请参见

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 
 
 
posted @ 2009-09-29 10:57  张兴业  阅读(686)  评论(0编辑  收藏  举报