Graphics 类的CopyFromScreen方法可以拿到整个屏幕的截图,屏幕截图的实现主要就是用到了他
思路是首先将整个屏幕的图像截取下来,然后作为一个窗体的背景显示给用户,由用户选择要截取的区域
后,保存图片就可以了
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public partial class Form2 : Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private bool down = false;
private Point firstPoint;
private Pen p = new Pen(Color.Red);
private Graphics gra;
private Rectangle rectangle;//存储用户截取的矩形
public Form2()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Form2_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);//创建一个和屏幕同样大小的图像
Graphics g = Graphics.FromImage(img);//绘制这个图像
//将屏幕绘制到此图像,第一,二个Point是屏幕要截取的左上角的坐标和绘制到图像的左上角的坐标(哪个是哪个就忘了)
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
this.BackgroundImage = img;
this.FormBorderStyle = FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
gra = this.CreateGraphics();//主要为了用户截取方便
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Form2_MouseDown(object sender, MouseEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (e.Button == MouseButtons.Left)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
down = true;
firstPoint = e.Location;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Form2_MouseUp(object sender, MouseEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (e.Button == MouseButtons.Left)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
down = false;
}
if (e.Button == MouseButtons.Middle)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Exit();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Form2_MouseMove(object sender, MouseEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (down)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
gra.DrawImage(this.BackgroundImage, 0, 0);
rectangle = new Rectangle(Math.Min(firstPoint.X, e.X), Math.Min(e.Y, firstPoint.Y), Math.Abs(e.X - firstPoint.X), Math.Abs(e.Y - firstPoint.Y));
gra.DrawRectangle(p, rectangle);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void Form2_DoubleClick(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (rectangle.Width != 0 && rectangle.Height != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
gra.DrawImage(this.BackgroundImage, 0, 0);
Image im = new Bitmap(rectangle.Width, rectangle.Height);
Graphics g = Graphics.FromImage(im);
g.CopyFromScreen(rectangle.Location, new Point(0, 0), rectangle.Size);
if (((MouseEventArgs)e).Button == MouseButtons.Left)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Clipboard.SetImage(im);
MessageBox.Show("图像已经复制到剪切板!","复制成功");
Application.Exit();
}
if (((MouseEventArgs)e).Button == MouseButtons.Right)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
im.Save(saveFileDialog1.FileName);
MessageBox.Show("图像已经保存到本地!","保存成功");
Application.Exit();
}
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)