【问题标题】:C# Screenshot Full WindowC#截图全窗口
【发布时间】:2019-01-13 23:05:48
【问题描述】:
【问题讨论】:
标签:
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.Drawing、System.Drawing.Imaging 和 System.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);