如何用C#截取整个屏幕

   2024-09-30 3040
核心提示:要使用C#截取整个屏幕,你可以使用System.Drawing和System.Windows.Forms命名空间中的类using System;using System.Drawing;usin

要使用C#截取整个屏幕,你可以使用System.DrawingSystem.Windows.Forms命名空间中的类

using System;using System.Drawing;using System.Windows.Forms;namespace ScreenCapture{    class Program    {        static void Main(string[] args)        {            // 获取屏幕分辨率            int screenWidth = Screen.PrimaryScreen.Bounds.Width;            int screenHeight = Screen.PrimaryScreen.Bounds.Height;            // 创建Bitmap对象来保存屏幕截图            using (Bitmap screenshot = new Bitmap(screenWidth, screenHeight))            {                // 创建Graphics对象来绘制屏幕截图                using (Graphics graphics = Graphics.FromImage(screenshot))                {                    // 将屏幕内容复制到Graphics对象中                    graphics.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));                }                // 保存屏幕截图为文件                string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\screenshot.png";                screenshot.Save(filePath);                Console.WriteLine("屏幕截图已保存到:" + filePath);            }        }    }}

这段代码首先获取屏幕的分辨率,然后创建一个Bitmap对象来保存屏幕截图。接下来,它创建一个Graphics对象并使用CopyFromScreen方法将屏幕内容复制到Graphics对象中。最后,它将屏幕截图保存为一个PNG文件,并在控制台输出文件路径。

 
举报打赏
 
更多>同类网点查询
推荐图文
推荐网点查询
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号