如何在c#中使用opengl捕捉桌面截图
本文关键字:桌面 opengl | 更新日期: 2023-09-27 17:59:28
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;
}