• C# 截图ScreenCapture,保存


    简化版:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO;
    using System.Drawing.Imaging;
    
    namespace ConsoleApp346
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                ScreenCapture();
    
            }
    
            static void ScreenCapture()
            {
                Rectangle bounds = Screen.GetBounds(System.Drawing.Point.Empty);
                using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
                {
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, bounds.Size);
                    }
                    string fullName = Directory.GetCurrentDirectory() + "\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
                    bitmap.Save(fullName, ImageFormat.Jpeg);
                }
            }
        }
    }

    效果如图:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO;
    using System.Drawing.Imaging;

    namespace ConsoleApp346
    {
    class Program
    {
    [STAThread]
    static void Main(string[] args)
    {
    ScreenCapture();

    }

    static void ScreenCapture()
    {
    Rectangle bounds = Screen.GetBounds(System.Drawing.Point.Empty);
    using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    {
    using (Graphics g = Graphics.FromImage(bitmap))
    {
    g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, bounds.Size);
    }

    using (SaveFileDialog sfd = new SaveFileDialog())
    {
    sfd.Title = "Save Pictures";
    sfd.InitialDirectory = Directory.GetCurrentDirectory();
    sfd.RestoreDirectory = true;
    sfd.Filter = "bmp files(*.bmp)|*.bmp|All Files(*.*)|*.*";
    sfd.FileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
    if (sfd.ShowDialog() == DialogResult.OK)
    {
    bitmap.Save(sfd.FileName, ImageFormat.Jpeg);
    }
    }
    }
    }
    }
    }

    效果如下所示:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Drawing;
     7 using System.Windows.Forms;
     8 using System.IO;
     9 using System.Drawing.Imaging;
    10 
    11 namespace ConsoleApp346
    12 {
    13     class Program
    14     {
    15         [STAThread]
    16         static void Main(string[] args)
    17         {
    18             ScreenCapture();
    19 
    20         }
    21 
    22         static void ScreenCapture()
    23         {
    24             Rectangle bounds = Screen.GetBounds(System.Drawing.Point.Empty);
    25             using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    26             {
    27                 using (Graphics g = Graphics.FromImage(bitmap))
    28                 {
    29                     g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, bounds.Size);
    30                 }
    31 
    32                 using (SaveFileDialog sfd = new SaveFileDialog())
    33                 {
    34                     sfd.Title = "Save Pictures";
    35                     sfd.InitialDirectory = Directory.GetCurrentDirectory();
    36                     sfd.RestoreDirectory = true;
    37                     sfd.Filter = "bmp files(*.bmp)|*.bmp|All Files(*.*)|*.*";
    38                     sfd.FileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
    39                     if (sfd.ShowDialog() == DialogResult.OK)
    40                     {                         
    41                         bitmap.Save(sfd.FileName, ImageFormat.Jpeg);
    42                     }
    43                 }
    44             }
    45         }
    46     }
    47 }


  • 相关阅读:
    Insert into select语句把生产服务器炸了!
    人人都能看懂的 6 种限流实现方案
    Idea 快捷生成类注释与方法注释
    拦截器
    java 泛型
    SQL语句总结
    深入浅出Git教程(转载)
    (转载)CSS3与页面布局学习总结(三)——BFC、定位、浮动、7种垂直居中方法
    css中常见margin塌陷问题之解决办法
    css中固定宽高div与不固定宽高div垂直居中的处理办法
  • 原文地址:https://www.cnblogs.com/Fred1987/p/10852097.html
Copyright © 2020-2023  润新知