public Bitmap GrabScreenshot()
{
Bitmap bmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
System.Drawing.Imaging.BitmapData data = bmp.LockBits(this.ClientRectangle, System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
CsGL.OpenGL.GL.glReadPixels(0, 0, 800, 600, CsGL.OpenGL.GL.GL_3D, CsGL.OpenGL.GL.GL_8X_BIT_ATI, data.Scan0);
CsGL.OpenGL.GL.glFinish();
bmp.UnlockBits(data);
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
return bmp;
}
private void button1_Click(object sender, EventArgs e)
{
GrabScreenshot();
Bitmap bmp = GrabScreenshot();
bmp.Save("C:\\temp\\test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
最佳答案
要捕获完整的屏幕截图,请使用:
public static Bitmap CaptureScreen()
{
Rectangle bounds = SystemInformation.VirtualScreen;
Bitmap Target = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppRgb);
using (Graphics g = Graphics.FromImage(Target))
{
g.CopyFromScreen(0, 0, 0, 0, bounds.Size);
}
return Target;
}
如果您需要特定屏幕,请使用此选项:
private enum CaptureType
{
AllScreens,
PrimaryScreen,
VirtualScreen,
WorkingArea
}
private static Bitmap[] Capture(CaptureType typeOfCapture)
{
Bitmap[] images = null;
Bitmap memoryImage;
int count = 1;
Screen[] screens = Screen.AllScreens;
Rectangle SourceRectangle;
switch (typeOfCapture)
{
case CaptureType.PrimaryScreen:
SourceRectangle = Screen.PrimaryScreen.Bounds;
break;
case CaptureType.VirtualScreen:
SourceRectangle = SystemInformation.VirtualScreen;
break;
case CaptureType.WorkingArea:
SourceRectangle = Screen.PrimaryScreen.WorkingArea;
break;
case CaptureType.AllScreens:
count = screens.Length;
typeOfCapture = CaptureType.WorkingArea;
SourceRectangle = screens[0].WorkingArea;
break;
default:
SourceRectangle = SystemInformation.VirtualScreen;
break;
}
// allocate a member for saving the captured image(s)
images = new Bitmap[count];
// cycle across all desired screens
for (int index = 0; index < count; index++)
{
if (index > 0)
{
SourceRectangle = screens[index].WorkingArea;
}
// redefine the size on multiple screens
memoryImage = new Bitmap(SourceRectangle.Width, SourceRectangle.Height, PixelFormat.Format32bppArgb);
using (Graphics memoryGrahics = Graphics.FromImage(memoryImage))
{
memoryGrahics.CopyFromScreen(SourceRectangle.X, SourceRectangle.Y, 0, 0, SourceRectangle.Size, CopyPixelOperation.SourceCopy);
}
images[index] = memoryImage;
}
return images;
}
关于c# - 如何在 C# 中使用 opengl 捕获桌面屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041844/