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);
            }
        }
    }
}

效果如图:

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);
}

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);
}
}
}
}
}
}

效果如下所示:

C#  截图ScreenCapture,保存C#  截图ScreenCapture,保存

 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 }


原文链接: https://www.cnblogs.com/Fred1987/p/10852097.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C#  截图ScreenCapture,保存

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/402234

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:39
下一篇 2023年4月19日 上午9:40

相关推荐