Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#全窗口截图_C#_.net_Console Application - Fatal编程技术网

C#全窗口截图

C#全窗口截图,c#,.net,console-application,C#,.net,Console Application,我正在尝试使用.NET Framework编写一个控制台应用程序。我想截屏我的屏幕。我用过其他答案,比如: 问题是,这并不能捕获我的整个屏幕。底部和右侧大约少了1/5 如何使用C#.NET Framework捕获整个屏幕?您可以使用screen.PrimaryScreen.Bounds以获取屏幕的边界 Rectangle bounds = Screen.PrimaryScreen.Bounds; using (Bitmap bitmap = new Bitmap(bounds.Width, b

我正在尝试使用.NET Framework编写一个控制台应用程序。我想截屏我的屏幕。我用过其他答案,比如:

问题是,这并不能捕获我的整个屏幕。底部和右侧大约少了1/5


如何使用C#.NET Framework捕获整个屏幕?

您可以使用
screen.PrimaryScreen.Bounds
以获取屏幕的边界

Rectangle bounds = Screen.PrimaryScreen.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
using (Graphics g = Graphics.FromImage(bitmap))
{
    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
    bitmap.Save("C://test.jpg", ImageFormat.Jpeg);
}
要在控制台应用程序中使用此代码示例,您需要参考
System.Drawing
System.Drawing.Imaging
System.Windows.Forms

使用:


不幸的是,我没有找到从控制台应用程序截图的方法,但下面是我截图的方法,让用户使用
SaveFileDialog
选择保存图片的位置

对于以下代码,您将需要以下三个参考:

使用系统图;
使用系统、绘图、成像;
使用System.Windows.Forms;
您可以将此代码附加到按钮或事件:

Bitmap bt=新位图(Screen.PrimaryScreen.Bounds.Width,
屏幕。主屏幕。边界。高度);
Graphics g=Graphics.FromImage(bt);
g、 CopyFromScreen(0,0,0,0,bt.Size);
SaveFileDialog sfd=新建SaveFileDialog();
ShowDialog();
字符串名称=sfd.FileName+“.jpg”;
bt.Save(名称,ImageFormat.Jpeg);

如果您尝试拍摄的屏幕设置为缩放,并且您的应用程序不是DPIAware,则您将收到虚拟化的度量(缩放到预定义的DPI值)。首先,尝试将应用程序的DPI感知设置为至少系统感知。请参见此处:如何激活它。有关专用API函数的更多详细信息。
int screenLeft = SystemInformation.VirtualScreen.Left;
int screenTop = SystemInformation.VirtualScreen.Top;
int screenWidth = SystemInformation.VirtualScreen.Width;
int screenHeight = SystemInformation.VirtualScreen.Height;

// Create a bitmap of the appropriate size to receive the full-screen screenshot.
using (Bitmap bitmap = new Bitmap(screenWidth, screenHeight))
{
    // Draw the screenshot into our bitmap.
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.CopyFromScreen(screenLeft, screenTop, 0, 0, bitmap.Size);
    }

    //Save the screenshot as a Jpg image
    var uniqueFileName = "C:\\temp\\a.Jpg";
    try
    {
        bitmap.Save(uniqueFileName, ImageFormat.Jpeg);
    }
    catch (Exception ex) {
    }
 }