Graphics 类的CopyFromScreen方法可以拿到整个屏幕的截图,屏幕截图的实现主要就是用到了他
思路是首先将整个屏幕的图像截取下来,然后作为一个窗体的背景显示给用户,由用户选择要截取的区域
后,保存图片就可以了

 Code
Code public partial class Form2 : Form
public partial class Form2 : Form
 
     {
{ private bool down = false;
        private bool down = false; private Point firstPoint;
        private Point firstPoint; private Pen p = new Pen(Color.Red);
        private Pen p = new Pen(Color.Red); private Graphics gra;
        private Graphics gra; private Rectangle rectangle;//存储用户截取的矩形
        private Rectangle rectangle;//存储用户截取的矩形 public Form2()
        public Form2()
 
         {
{ InitializeComponent();
            InitializeComponent(); }
        }
 private void Form2_Load(object sender, EventArgs e)
        private void Form2_Load(object sender, EventArgs e)
 
         {
{ Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);//创建一个和屏幕同样大小的图像
            Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);//创建一个和屏幕同样大小的图像 Graphics g = Graphics.FromImage(img);//绘制这个图像
            Graphics g = Graphics.FromImage(img);//绘制这个图像 //将屏幕绘制到此图像,第一,二个Point是屏幕要截取的左上角的坐标和绘制到图像的左上角的坐标(哪个是哪个就忘了)
//将屏幕绘制到此图像,第一,二个Point是屏幕要截取的左上角的坐标和绘制到图像的左上角的坐标(哪个是哪个就忘了) g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size); this.BackgroundImage = img;
            this.BackgroundImage = img; this.FormBorderStyle = FormBorderStyle.None;
            this.FormBorderStyle = FormBorderStyle.None; this.Bounds = Screen.PrimaryScreen.Bounds;
            this.Bounds = Screen.PrimaryScreen.Bounds; gra = this.CreateGraphics();//主要为了用户截取方便
            gra = this.CreateGraphics();//主要为了用户截取方便 }
        }
 private void Form2_MouseDown(object sender, MouseEventArgs e)
        private void Form2_MouseDown(object sender, MouseEventArgs e)
 
         {
{ if (e.Button == MouseButtons.Left)
            if (e.Button == MouseButtons.Left)
 
             {
{ down = true;
                down = true; firstPoint = e.Location;
                firstPoint = e.Location; }
            } }
        }
 private void Form2_MouseUp(object sender, MouseEventArgs e)
        private void Form2_MouseUp(object sender, MouseEventArgs e)
 
         {
{ if (e.Button == MouseButtons.Left)
            if (e.Button == MouseButtons.Left)
 
             {
{ down = false;
                down = false; }
            } if (e.Button == MouseButtons.Middle)
            if (e.Button == MouseButtons.Middle)
 
             {
{ Application.Exit();
                Application.Exit(); }
            } }
        }
 private void Form2_MouseMove(object sender, MouseEventArgs e)
        private void Form2_MouseMove(object sender, MouseEventArgs e)
 
         {
{ if (down)
            if (down)
 
             {
{ gra.DrawImage(this.BackgroundImage, 0, 0);
                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));
                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);
                gra.DrawRectangle(p, rectangle); }
            } }
        }
 private void Form2_DoubleClick(object sender, EventArgs e)
        private void Form2_DoubleClick(object sender, EventArgs e)
 
         {
{ if (rectangle.Width != 0 && rectangle.Height != 0)
            if (rectangle.Width != 0 && rectangle.Height != 0)
 
             {
{ gra.DrawImage(this.BackgroundImage, 0, 0);
                gra.DrawImage(this.BackgroundImage, 0, 0); Image im = new Bitmap(rectangle.Width, rectangle.Height);
                Image im = new Bitmap(rectangle.Width, rectangle.Height); Graphics g = Graphics.FromImage(im);
                Graphics g = Graphics.FromImage(im); g.CopyFromScreen(rectangle.Location, new Point(0, 0), rectangle.Size);
                g.CopyFromScreen(rectangle.Location, new Point(0, 0), rectangle.Size); if (((MouseEventArgs)e).Button == MouseButtons.Left)
                if (((MouseEventArgs)e).Button == MouseButtons.Left)
 
                 {
{ Clipboard.SetImage(im);
                    Clipboard.SetImage(im); MessageBox.Show("图像已经复制到剪切板!","复制成功");
                    MessageBox.Show("图像已经复制到剪切板!","复制成功"); Application.Exit();
                    Application.Exit(); }
                } if (((MouseEventArgs)e).Button == MouseButtons.Right)
                if (((MouseEventArgs)e).Button == MouseButtons.Right)
 
                 {
{ if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
 
                     {
{ im.Save(saveFileDialog1.FileName);
                        im.Save(saveFileDialog1.FileName); MessageBox.Show("图像已经保存到本地!","保存成功");
                        MessageBox.Show("图像已经保存到本地!","保存成功"); Application.Exit();
                        Application.Exit(); }
                    } }
                } }
            } }
        } }
    }

 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号