.NET中使用Graphics绘图
 
InBlock.gif using System;
InBlock.gif using System.Drawing;
InBlock.gif using System.Collections;
InBlock.gif using System.ComponentModel;
InBlock.gif using System.Windows.Forms;
InBlock.gif using System.Data;
InBlock.gif using System.Drawing.Drawing2D;
InBlock.gif
InBlock.gif namespace Graph
InBlock.gif...{
InBlock.gif        /** <summary>
InBlock.gif         /// Form1 的摘要说明。
InBlock.gif         /// </summary>
InBlock.gif         public class Form1 : System.Windows.Forms.Form
InBlock.gif        ...{
InBlock.gif                 private System.Windows.Forms.PictureBox pictureBox1;
InBlock.gif                 private System.Windows.Forms.Button button1;
InBlock.gif                 private System.Windows.Forms.Button button2;
InBlock.gif                /** <summary>
InBlock.gif                 /// 必需的设计器变量。
InBlock.gif                 /// </summary>
InBlock.gif                 private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif                 public Form1()
InBlock.gif                ...{
InBlock.gif                         //
InBlock.gif                         // Windows 窗体设计器支持所必需的
InBlock.gif                         //
InBlock.gif                        InitializeComponent();
InBlock.gif
InBlock.gif                         //
InBlock.gif                         // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif                         //
InBlock.gif                }
InBlock.gif
InBlock.gif                /** <summary>
InBlock.gif                 /// 清理所有正在使用的资源。
InBlock.gif                 /// </summary>
InBlock.gif                 protected override void Dispose( bool disposing )
InBlock.gif                ...{
InBlock.gif                         if( disposing )
InBlock.gif                        ...{
InBlock.gif                                 if (components != null)    
InBlock.gif                                ...{
InBlock.gif                                        components.Dispose();
InBlock.gif                                }
InBlock.gif                        }
InBlock.gif                         base.Dispose( disposing );
InBlock.gif                }
InBlock.gif
InBlock.gifWindows Form Designer generated code #region Windows Form Designer generated code
InBlock.gif                /** <summary>
InBlock.gif                 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif                 /// 此方法的内容。
InBlock.gif                 /// </summary>
InBlock.gif                 private void InitializeComponent()
InBlock.gif                ...{
InBlock.gif                         this.pictureBox1 = new System.Windows.Forms.PictureBox();
InBlock.gif                         this.button1 = new System.Windows.Forms.Button();
InBlock.gif                         this.button2 = new System.Windows.Forms.Button();
InBlock.gif                         this.SuspendLayout();
InBlock.gif                         //    
InBlock.gif                         // pictureBox1
InBlock.gif                         //    
InBlock.gif                         this.pictureBox1.BackColor = System.Drawing.Color.White;
InBlock.gif                         this.pictureBox1.Location = new System.Drawing.Point(184, 24);
InBlock.gif                         this.pictureBox1.Name = "pictureBox1";
InBlock.gif                         this.pictureBox1.Size = new System.Drawing.Size(200, 216);
InBlock.gif                         this.pictureBox1.TabIndex = 0;
InBlock.gif                         this.pictureBox1.TabStop = false;
InBlock.gif                         //    
InBlock.gif                         // button1
InBlock.gif                         //    
InBlock.gif                         this.button1.Location = new System.Drawing.Point(48, 272);
InBlock.gif                         this.button1.Name = "button1";
InBlock.gif                         this.button1.Size = new System.Drawing.Size(120, 48);
InBlock.gif                         this.button1.TabIndex = 1;
InBlock.gif                         this.button1.Text = "在窗体上绘图";
InBlock.gif                         this.button1.Click += new System.EventHandler( this.button1_Click);
InBlock.gif                         //    
InBlock.gif                         // button2
InBlock.gif                         //    
InBlock.gif                         this.button2.Location = new System.Drawing.Point(232, 272);
InBlock.gif                         this.button2.Name = "button2";
InBlock.gif                         this.button2.Size = new System.Drawing.Size(120, 48);
InBlock.gif                         this.button2.TabIndex = 2;
InBlock.gif                         this.button2.Text = "在图片框上绘图";
InBlock.gif                         this.button2.Click += new System.EventHandler( this.button2_Click_1);
InBlock.gif                         //    
InBlock.gif                         // Form1
InBlock.gif                         //    
InBlock.gif                         this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
InBlock.gif                         this.ClientSize = new System.Drawing.Size(408, 342);
InBlock.gif                         this.Controls.AddRange( new System.Windows.Forms.Control[] ...{
InBlock.gif                                                                                                                                                     this.button2,
InBlock.gif                                                                                                                                                     this.button1,
InBlock.gif                                                                                                                                                     this.pictureBox1});
InBlock.gif                         this.Name = "Form1";
InBlock.gif                         this.Text = "Graphics";
InBlock.gif                         this.ResumeLayout( false);
InBlock.gif
InBlock.gif                }
InBlock.gif                #endregion
InBlock.gif
InBlock.gif                /** <summary>
InBlock.gif                 /// 应用程序的主入口点。
InBlock.gif                 /// </summary>
InBlock.gif                [STAThread]
InBlock.gif                 static void Main()    
InBlock.gif                ...{
InBlock.gif                        Application.Run( new Form1());
InBlock.gif                }
InBlock.gif
InBlock.gif                 protected override void OnPaint(PaintEventArgs e)
InBlock.gif                ...{
InBlock.gif                        Graphics g = e.Graphics;
InBlock.gif                         float x, y,w, h;
InBlock.gif                        x = pictureBox1.Left - 2;
InBlock.gif                        y = pictureBox1.Top - 2;
InBlock.gif                        w = pictureBox1.Width + 4;
InBlock.gif                        h = pictureBox1.Height + 4;
InBlock.gif                        Pen pen= new Pen(Color.Red, 2);
InBlock.gif                        g.DrawRectangle(pen, x, y, w, h);
InBlock.gif
InBlock.gif                        GraphicsPath gp= new GraphicsPath();
InBlock.gif                        gp.AddRectangle( new Rectangle(10, 10, 100, 100));
InBlock.gif                }
InBlock.gif
InBlock.gif                 private void button1_Click( object sender, EventArgs e)
InBlock.gif                ...{
InBlock.gif                        Graphics g= this.CreateGraphics();
InBlock.gif                        g.FillEllipse(Brushes.Blue, 10, 20, 50, 100);
InBlock.gif                }
InBlock.gif
InBlock.gif                 private void button2_Click_1( object sender, EventArgs e)
InBlock.gif                ...{
InBlock.gif
InBlock.gif                        Graphics g= pictureBox1.CreateGraphics();
InBlock.gif                        g.FillEllipse(Brushes.Blue, 10, 20, 50, 100);
InBlock.gif                }
InBlock.gif
InBlock.gif        }
InBlock.gif}