c#截图简单实现

两窗体
第一窗体
新建和全屏 
第二窗体   属性none 最大化
实现区域截图

新建
    private void button4_Click_1(object sender, EventArgs e)
        {
            this.Hide();
            Form3 form2 = new Form3();//打开窗体2
            form2.ShowDialog(this);
            
        }
        
//第二个窗体
 
    //截全图
     public Form3()
        {
            baseImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);//创建一个主屏幕的对象
            Graphics g = Graphics.FromImage(baseImage);//创建一个图型对象
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);//传送位图
            InitializeComponent();
        }
        //定义变量
          //定义变量
        Image baseImage;
        private double x;//x坐标
        private double y;//y坐标
        private double x1;//x坐标
        private double y1;//y坐标
        private double width;//宽度
        private double height;//高度
        Point start;//起点坐标
       // Point end;//终点坐标
        Point last_start, last_end;//上一个矩形的起点和终点
//
  private void Form3_Load(object sender, EventArgs e)
        {
            this.pic1.Image = baseImage;//设置好全图
        }
        
//截图方法
        private void CaptureScreen(double x, double y, double width, double height)
        {
            int ix = Convert.ToInt32(x);//x坐标
            int iy = Convert.ToInt32(y);//y坐标
            int iw = Convert.ToInt32(width);//宽度
            int ih = Convert.ToInt32(height);//高度

            System.Drawing.Bitmap bitmap = new Bitmap(iw, ih);//图像
            using (System.Drawing.Graphics graphics = Graphics.FromImage(bitmap))//引用
            {
                graphics.CopyFromScreen(ix, iy, 0, 0, new System.Drawing.Size(iw, ih));//创建截图
                //Clipboard.SetImage(bitmap);//把图片放到剪切板中
                string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//结果保存到桌面
                string fileName = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒") + ".jpg";//以日期命名文件名

                bitmap.Save(path + fileName, ImageFormat.Jpeg);//保存文件
                this.pic1.Image = bitmap;

                MessageBox.Show("截图成功!--文件在桌面!");


            }

        }
        
        //按下        
          private void pic1_MouseDown(object sender, MouseEventArgs e)
        {
            x = e.X;
            y = e.Y;
            start.X = e.X;
            start.Y = e.Y;
        }
        
       
       // 松开
          private void pic1_MouseUp(object sender, MouseEventArgs e)
        {
            x1 = e.X;//按下后的x坐标
            y1 = e.Y;//按下后的y坐标
            width = x1 - x;
            height = y1 - y;

            CaptureScreen(x + 1, y + 1, width, height);//截图

            this.Close();
            this.Owner.Show();//显示
            this.Hide();//隐藏
        }
        
        
        //移动时
         private void pic1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)//如果鼠标左键按下就执行
            {
                Point nop = this.PointToClient(Control.MousePosition);
                Graphics GPS = this.pic1.CreateGraphics();//CreateGraphics();
                Pen MyPen = new Pen(Color.Black, 1f);//画笔属性
                last_end.X = nop.X;
                last_end.Y = start.Y;//上位坐标
                last_start.X = start.X;
                last_start.Y = nop.Y;
                int Width1 = nop.X - start.X;
                int Height1 = nop.Y - start.Y;
                GPS.DrawLine(MyPen, start, last_end);//颜色,起点,终点
                GPS.DrawLine(MyPen, start, last_start);
                
            }
        }
        
全屏截图
 private void button5_Click_1(object sender, EventArgs e)
        {
            this.Hide();
            Image baseImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);//创建一个主屏幕的对象
            Graphics g = Graphics.FromImage(baseImage);//创建一个图型对象
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);//传送位图
            g.Dispose();//清理资源
            //桌面路径
            string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            string time = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒") + ".jpg";//以日期命名文件名

            baseImage.Save(path + time, ImageFormat.Jpeg);//保存文件
            MessageBox.Show("截图成功!桌面文件!");
            this.Show();

        }
        
附加代码
//获取整个屏幕图像,不包括任务栏
public Bitmap GetScreen()
 {
   //获取整个屏幕图像,不包括任务栏
  Rectangle ScreenArea = Screen.GetWorkingArea(this);
  Bitmap bmp = new Bitmap(ScreenArea.Width, ScreenArea.Height);
  using (Graphics g = Graphics.FromImage(bmp))
  {
  g.CopyFromScreen(0, 0, 0, 0, new Size(ScreenArea.Width,ScreenArea.Height));
  }
  return bmp;
 }
 
 
//将截图设置到剪切板
    Clipboard.SetImage(clipBmp);
    
    
//将截图以对话框的方式保存
      Image baseImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);//创建一个主屏幕的对象
            Graphics g = Graphics.FromImage(baseImage);//创建一个图型对象
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);//传送位图
            g.Dispose();//清理资源
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "jpg图片|*.JPG|gif图片|*.GIF|png图片|*.PNG|jpeg图片|*.JPEG";
            save.ShowDialog();//打开文件对话框
            if (save.FileName != string.Empty)
            {
                baseImage.Save(save.FileName, ImageFormat.Jpeg);//保存文件

            } 
            
画笔
Graphics GPS = this.CreateGraphics();
            Pen MyPen = new Pen(Color.Red, 2f);
            GPS.DrawLine(MyPen, 50, 20, 300, 200);//颜色,起点,终点
            
实验后截图
CaptureScreen(x+1,y+25,width,height);//截图

实验后矩形画笔
 Graphics GPS = this.CreateGraphics();
            Pen MyPen = new Pen(Color.Black, 1f);
            GPS.DrawRectangle(MyPen,ix+1,iy+1,iw ,ih);//起点 、宽度和高度都是into类型

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值