【问题标题】:C# Screenshot Full WindowC#截图全窗口
【发布时间】:2019-01-13 23:05:48
【问题描述】:

我正在尝试使用 .NET Framework 编写控制台应用程序。我想截屏我的屏幕。我在 SO 上使用了其他答案,如下所示:

https://stackoverflow.com/a/24879511/9457997

问题是这并没有捕获我的整个屏幕。它在底部和右侧缺失了大约 1/5。

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

【问题讨论】:

标签: c# .net console-application


【解决方案1】:

使用VirtualScreen:

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

【讨论】:

    【解决方案2】:

    您可以使用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.DrawingSystem.Drawing.ImagingSystem.Windows.Forms 才能使此代码示例在控制台应用程序中运行。

    【讨论】:

      【解决方案3】:

      很遗憾,我还没有找到从控制台应用程序截屏的方法,但这是我截屏的方法,让用户使用SaveFileDialog 选择保存图片的位置。

      对于以下代码,您将需要这三个引用:

      using System.Drawing;
      using System.Drawing.Imaging;
      using System.Windows.Forms;
      

      您可以将此代码附加到按钮或事件:

      Bitmap bt = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
      Screen.PrimaryScreen.Bounds.Height);
      Graphics g = Graphics.FromImage(bt);
      g.CopyFromScreen(0, 0, 0, 0, bt.Size);
      SaveFileDialog sfd = new SaveFileDialog();
      sfd.ShowDialog();
      string name = sfd.FileName + ".jpg";
      bt.Save(name, ImageFormat.Jpeg);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-04
        • 1970-01-01
        相关资源
        最近更新 更多