C# 简单截屏软件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Threading;


namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        Graphics g;
        Bitmap bmp;
        public Form3()
        {
            InitializeComponent();
            //主显示器的边框
           Rectangle rec= Screen.PrimaryScreen.Bounds;//获取显示器的边框
           bmp = new Bitmap(rec.Width, rec.Height);//以显示器的尺寸实例化一张图片
           g = Graphics.FromImage(bmp);//以这张图片为基准创建一块画布
        }
        int i = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;//是否在任务栏显示
            this.Hide();//隐藏窗体
            Thread.Sleep(1);
            Size size=new System.Drawing.Size(bmp.Width,bmp.Height);
            g.CopyFromScreen(0, 0, 0, 0, size);//将整个屏幕截下来
            SaveFileDialog sf = new SaveFileDialog();//实例化一个对话框
            sf.Filter = "图片|*.ipg";
            sf.FileName = i.ToString() + ".jpg";
            i++;
            if (sf.ShowDialog()==DialogResult.OK)
            {
                string fileName = sf.FileName;
                bmp.Save(fileName,ImageFormat.Jpeg);
                
            }
            
            this.ShowInTaskbar = true;
            this.Show();
        }




        private void button2_Click(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;//是否在任务栏显示
            this.Hide();//隐藏窗体
            Thread.Sleep(1);
            Size size = new System.Drawing.Size(bmp.Width, bmp.Height);
            g.CopyFromScreen(0, 0, 0, 0, size);
            Form5 f5 = new Form5();//实例化窗体5
            f5.BackgroundImage = bmp;//把屏幕的截屏作为窗体的背景
          
            f5.TopMost = true;//让窗体5显示在最前端
            f5.FormBorderStyle = FormBorderStyle.None;//设置窗体没有外边框
            f5.WindowState = FormWindowState.Maximized;//让窗体最大化覆盖整个屏幕
           
            f5.Show();


        }


        private void Form3_KeyDown(object sender, KeyEventArgs e)
        {
            
        }
    }

}


//这里我们用form5来模拟整个屏幕,在form5上面做截屏操作


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        Graphics g;//画布
        Bitmap bmp;//图片
        public Form5()
        {
            InitializeComponent();
            bmp = new Bitmap(this.Width, this.Height);
            g = Graphics.FromImage(bmp);//以一个图片来创建一个画布
            //g = this.CreateGraphics();


        }
        Point pStart, pEnd;
        private void Form5_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pStart = new Point(e.X, e.Y);
            }
        }
        public Color Color = Color.Red;//画笔的颜色
        public int lineWidth = 1;//线条的宽度
        private void Form5_MouseMove(object sender, MouseEventArgs e)
        {


            if (e.Button == MouseButtons.Left)
            {
                this.Refresh();
                //在这个picture上画一个方框


                Graphics g2 = this.CreateGraphics();
                pEnd = new Point(e.X, e.Y);//获取释放的点的坐标
                Pen p = new Pen(Color, lineWidth);//实例化画具
                int x = Math.Min(pStart.X, pEnd.X);//比较两个点的x坐标,返回较小的
                int y = Math.Min(pStart.Y, pEnd.Y);//比较两个点的y坐标,返回较小的
                int width = Math.Abs(pEnd.X - pStart.X);//获取两个点x坐标的差的绝对值
                int height = Math.Abs(pEnd.Y - pStart.Y);//获取两个点y坐标的差的绝对值
                g2.DrawRectangle(p, x, y, width, height);
            }
        }


        private void Form5_MouseUp(object sender, MouseEventArgs e)
        {


            if (e.Button == MouseButtons.Left)
            {
                pEnd = new Point(e.X, e.Y);//获取释放的点的坐标
                Pen p = new Pen(Color, lineWidth);//实例化画具
                int x = Math.Min(pStart.X, pEnd.X);//比较两个点的x坐标,返回较小的
                int y = Math.Min(pStart.Y, pEnd.Y);//比较两个点的y坐标,返回较小的
                int width = Math.Abs(pEnd.X - pStart.X);//获取两个点x坐标的差的绝对值
                int height = Math.Abs(pEnd.Y - pStart.Y);//获取两个点y坐标的差的绝对值
                g.DrawRectangle(p, x, y, width, height);
                this.Refresh();
                   Bitmap bt = new Bitmap(width, height);
                SaveFileDialog sf = new SaveFileDialog();
                sf.Filter = "图片|*.jpg";
                if (sf.ShowDialog()==DialogResult.OK)
                {
                    string fileName = sf.FileName;
                    bt.Save(fileName);
                    this.Close();
                    Form3 f3 = new Form3();
                    f3.Show();
                }
            }
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值