C#中利用鼠標的坐標進行截圖
private void Form1_Click(object sender, EventArgs e)
{
if (flag % 2 == 0)
{
Fx = Cursor.Position;//獲取在控件中的鼠標坐標 這個就是屏幕的坐標了
this.textBox1.Text = string.Format("{0},{1},{2},{3}", Fx.X, Fx.Y, Fy.X, Fy.Y);
}
else
{
// Fy = this.PointToScreen(Cursor.Position);
//MessageBox.Show(string.Format("{0},{1},{2},{3}",Fx.X,Fx.Y,Fy.X,Fy.Y));
Fy = Cursor.Position;
// this.textBox1.Text = string.Format("{0},{1},{2},{3}", Fx.X, Fx.Y, Fy.X - Fx.X, Fy.Y-Fx.Y);
this.textBox1.Text = string.Format("{0},{1},{2},{3}", Fx.X, Fx.Y, Fy.X, Fy.Y);
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight); // the final image used by memory reference
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot); //獲取繪圖繪畫
// gfx.CopyFromScreen(this.pictureBox1.PointToScreen(this.pictureBox1.Location), new Point(0, 0), this.pictureBox1.Size);
//gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));//獲取整個屏幕的類似printscreen
// Cursor.Position = new Point(Fx.X + 100, Fx.Y + 100);
gfx.CopyFromScreen(Fx.X, Fx.Y, 0, 0, new Size(Fy.X-Fx.X, Fy.Y-Fx.Y));//從這個屏幕中截取我們所需要的部分
this.imageBox1.BackgroundImage = bmpScreenShot;
bmpScreenShot.Save("shot.png");
}
// MessageBox.Show("OK");
flag++;
}