我的位置:首页 > .Net系列>C#

C# 截图ScreenCapture,保存

时间:2019-05-12 14:09:00 来源:互联网 作者: 神秘的大神 字体:

简化版:

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 }