C#获取全屏截图的方法
1.代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace draftpaper_CSharp
{
class Program
{
static void Main(string[] args)
{
ScreenShot.GetFullScreenShot();
}
}
class ScreenShot
{
/*private enum processDpiAwareness
{
ProcessDPIUnaware = 0,
ProcessSystemDPIAware = 1,
ProcessPerMonitorDPIAware = 2
};
[DllImport("shcore.dll")]
private static extern int SetProcessDpiAwareness(processDpiAwareness value);*/
public static void GetFullScreenShot()
{
/*SetProcessDpiAwareness(processDpiAwareness.ProcessPerMonitorDPIAware);*/
Screen sc = Screen.PrimaryScreen; //获取主屏
Bitmap i = new Bitmap(sc.Bounds.Width, sc.Bounds.Height);
Graphics g = Graphics.FromImage(i);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), sc.Bounds.Size);
i.Save(".//SavedImage.png",ImageFormat.Png);
g.Dispose();
}
}
}
2.说明
如果截出来的图像大小和屏幕分辨率不符合,去掉注释部分的代码即可。