C# 屏幕截图

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

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

复制代码

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

    private void Form2_Load(object sender, EventArgs e)
    {
        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();//主要为了用户截取方便
}

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

    private void Form2_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            down = false;
        }
        if (e.Button == MouseButtons.Middle)
        {
            Application.Exit();
        }
    }

    private void Form2_MouseMove(object sender, MouseEventArgs e)
    {
        if (down)
        {
            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);
        }
    }

    private void Form2_DoubleClick(object sender, EventArgs e)
    {
        if (rectangle.Width != 0 && rectangle.Height != 0)
        {
            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)
            {
                Clipboard.SetImage(im);
                MessageBox.Show("图像已经复制到剪切板!","复制成功");
                Application.Exit();
            }
            if (((MouseEventArgs)e).Button == MouseButtons.Right)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    im.Save(saveFileDialog1.FileName);
                    MessageBox.Show("图像已经保存到本地!","保存成功");
                    Application.Exit();
                }
            }
        }
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DK业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值