如何在c#中使用opengl捕获桌面的截图?

时间:2015-06-25 05:25:17

标签: c#

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

1 个答案:

答案 0 :(得分:1)

要捕获完整屏幕截图,请使用以下命令:

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;
}