c# 截图问题

.Net技术 码拜 9年前 (2015-05-10) 884次浏览 0个评论
 

现在picturebox1.image  要从这个里面截取一部分到picturebox2中  截取30*30的
 private void pictureBox1_Click(object sender, EventArgs e)
        {
           
            Px = Cursor.Position.X;
            Py = Cursor.Position.Y;
            int x = Px – 15;
            int y = Py – 15;
            Graphics g = pictureBox2.CreateGraphics();
            g.DrawImage(pictureBox1.Image, 0, 0, new Rectangle(x, y,30,30), GraphicsUnit.Pixel);

        }
  这样写为什么不行??

代码没问题,看看X,Y取值对不对
40分
你获得的坐标是鼠标相对于屏幕的坐标,而不是相对于pictureBox1的坐标,通过下面的函数转换一下,就可以了

                Point p = this.pictureBox1.PointToClient(Control.MousePosition);
                int Px = p.X;
                int Py = p.Y;               
                Graphics g = pictureBox2.CreateGraphics();
                g.DrawImage(pictureBox1.Image, 0, 0, new Rectangle(Px, Py, 30, 30), GraphicsUnit.Pixel);

极有可能是你得到的x,y值超出了你的控件范围,所以你看不到。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c# 截图问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!