我在窗体上放了个pictureBox1控件我想要根据pictureBox1绘制出对角线来,下面的代码绘制的不是对角线不知道为什么
   private void test_Load(object sender, EventArgs e)
        {
            Bitmap mymap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
           
            Pen mypen = new Pen(Color.Red,3 );
            Brush mybrush=new SolidBrush(Color.Blue);
            Point mypoint1 = new Point(pictureBox1.Location.X, pictureBox1.Location.Y);            Point mypoint2 = new Point(pictureBox1.Location.X + pictureBox1.Width, pictureBox1.Location.Y + pictureBox1.Height);            Point myPoint3 = new Point(pictureBox1.Location.X,pictureBox1.Location.Y+pictureBox1.Height);            Point mypoint4 = new Point(pictureBox1.Location.X+pictureBox1.Width,pictureBox1.Location.Y);            Graphics g = Graphics.FromImage(mymap);
            g.FillRectangle(mybrush, pictureBox1.Location.X, pictureBox1.Location.Y, pictureBox1.Width, pictureBox1.Height);
           
           g.DrawLine(mypen, mypoint1, mypoint2);
            g.DrawLine(mypen,myPoint3,mypoint4);
            pictureBox1.Image = mymap;
        }

解决方案 »

  1.   


    Bitmap mymap = new Bitmap(pictureBox1.Width, pictureBox1.Height); Pen mypen = new Pen(Color.Red, 3);
    Brush mybrush = new SolidBrush(Color.Blue);
    Point mypoint1 = Point.Empty; Point mypoint2 = new Point(pictureBox1.Width, pictureBox1.Height); Point myPoint3 = new Point(0, pictureBox1.Height); Point mypoint4 = new Point(pictureBox1.Width, 0); Graphics g = Graphics.FromImage(mymap);
    g.FillRectangle(mybrush, 0,0, pictureBox1.Width, pictureBox1.Height); g.DrawLine(mypen, mypoint1, mypoint2);
    g.DrawLine(mypen, myPoint3, mypoint4);
    pictureBox1.Image = mymap;
      

  2.   

    楼主对位图的坐标有误解。
    mypoint1是Bitmap的左上角,所以是0,0。绘制图时坐标是相对于位图的左上角。 
      

  3.   

    不完全明白能说的具体点儿吗?
    pictureBox1.Location.X是实际的坐标
    mypoint是bitmap的相对坐标?
    Bitmap mymap = new Bitmap(pictureBox1.Width, pictureBox1.Height)不是指定位图的大小吗
    那位图坐标怎样和pictureBox1的坐标联系起来的
      

  4.   

    Bitmap mymap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    这句是创建一个和pictureBox1大小一样的位图,但绘制时是在mymap上操作的。
    绘制mymap与任何控件都没有位置关系。
    pictureBox1.Image = mymap;
    这句设置图片和你在设计视图中设置Image属性一样,给pictureBox1设置一个图像,
    设置的图像和控件位置没有关系的。
    你可以mymap.Save(@"c:\a.jpg");存到磁盘,看看绘制了什么。