我正在尝试使用.NET Framework编写控制台应用程序。我想截图我的屏幕。我在SO上使用了其他答案,例如:
https://stackoverflow.com/a/24879511/9457997
问题在于,这无法捕获我的整个屏幕。底部和右侧缺少大约1/5。
如何使用C#.NET Framework捕获整个屏幕?
答案 0 :(得分:1)
您可以使用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
,此代码示例才能在控制台应用程序中工作。
答案 1 :(得分:0)
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 :(得分:0)
很遗憾,我没有找到从控制台应用程序截屏的方法,但这是我的截屏方法,让用户使用 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);