c#实现屏幕截图

Graphics 类的CopyFromScreen方法可以拿到整个屏幕的截图,屏幕截图的实现主要就是用到了他

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

 

ContractedBlock.gif ExpandedBlockStart.gif Code
public partial class Form2 : Form
ExpandedBlockStart.gifContractedBlock.gif    
{
        
private bool down = false;
        
private Point firstPoint;
        
private Pen p = new Pen(Color.Red);
        
private Graphics gra;
        
private Rectangle rectangle;//存储用户截取的矩形
        public Form2()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
        }


        
private void Form2_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.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(00), new Point(00), Screen.AllScreens[0].Bounds.Size);
            
this.BackgroundImage = img;
            
this.FormBorderStyle = FormBorderStyle.None;
            
this.Bounds = Screen.PrimaryScreen.Bounds;
            gra 
= this.CreateGraphics();//主要为了用户截取方便
        }


        
private void Form2_MouseDown(object sender, MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (e.Button == MouseButtons.Left)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                down 
= true;
                firstPoint 
= e.Location;
            }

        }


        
private void Form2_MouseUp(object sender, MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (e.Button == MouseButtons.Left)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                down 
= false;
            }

            
if (e.Button == MouseButtons.Middle)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Application.Exit();
            }

        }


        
private void Form2_MouseMove(object sender, MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (down)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                gra.DrawImage(
this.BackgroundImage, 00);
                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);
            }

        }


        
private void Form2_DoubleClick(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (rectangle.Width != 0 && rectangle.Height != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                gra.DrawImage(
this.BackgroundImage, 00);
                Image im 
= new Bitmap(rectangle.Width, rectangle.Height);
                Graphics g 
= Graphics.FromImage(im);
                g.CopyFromScreen(rectangle.Location, 
new Point(00), rectangle.Size);
                
if (((MouseEventArgs)e).Button == MouseButtons.Left)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    Clipboard.SetImage(im);
                    MessageBox.Show(
"图像已经复制到剪切板!","复制成功");
                    Application.Exit();
                }

                
if (((MouseEventArgs)e).Button == MouseButtons.Right)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        im.Save(saveFileDialog1.FileName);
                        MessageBox.Show(
"图像已经保存到本地!","保存成功");
                        Application.Exit();
                    }

                }

            }

        }

    }


转载于:https://www.cnblogs.com/howardwyy/archive/2008/10/14/1310444.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值