quark

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

[C#技术] 【转】C#画点方法总结

数字人 发表于:2010-8-22 16:44:30
1、直线
public  void  DrawLineFloat(PaintEventArgs  e) 

//  Create  pen. 
Pen  blackPen  =  new  Pen(Color.Black,  3); 
//  Create  coordinates  of  points  that  define  line. 
float  x1  =  100.0F; 
float  y1  =  100.0F; 
float  x2  =  500.0F; 
float  y2  =  100.0F; 
//  Draw  line  to  screen. 
e.Graphics.DrawLine(blackPen,  x1,  y1,  x2,  y2); 
}
2、正方形
g.DrawRectangle(thepen, ps.X, ps.Y, 0.5F, 0.5F);
3、指定的图形
Bitmap  bm=new  Bitmap(2,2);    //这里调整点的大小 
bm.SetPixel(0,  0,  color);      //设置点的颜色 
bm.SetPixel(0,  1,  color); 
bm.SetPixel(1,  0,  color); 
bm.SetPixel(1,  1,  color); 
Graphics  g  =  Graphics.FromHwnd(this.panel1.Handle);    //画在哪里   
g.DrawImageUnscaled(bm,  e.X,  e.Y);      //具体坐标


关于最后一种方法,还有一个详细的例子,如下:
用Bitmap的SetPixel()函数来实现。其实现是通过设置像素点生成一张图片,然后显示图片。
//定义图片
        Bitmap bmp;
        private void Form1_Load(object sender, EventArgs e)
        {
            bmp = new Bitmap(this.ClientRectangle.Width,this.ClientRectangle.Height);

            Graphics graphics = Graphics.FromImage(bmp);

            int i,j;
            int w= bmp.Width;
            int h = bmp.Height;

            int interval = 5;
            //每隔5个像素点画设置一个黑颜色点,生成图片。
            for (i = 0; i < w; i += interval)
            {
                for (j = 0; j < h; j += interval)
                {
                    //使用SetPixel()来设置像素点。
                    bmp.SetPixel(i,j,Color.Black);
                }
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            //显示图片
            graphics.DrawImage(bmp,new Rectangle(0,0,this.ClientRectangle.Width,this.ClientRectangle.Height));
        }

[url=http://byfiles.storage.live.com/y1p0u3wW6izvRjf35JA4oGWEWTbzS8yERmX-_p9n0IwhyS1JqmcUUQw0tDZGXVFKXE4noJs--0m8V4][/url]
posted on 2012-03-17 19:28  QuarkZ  阅读(4264)  评论(0)    收藏  举报