如何在C#中使用OpenGL捕获桌面截图?

3
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个回答

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

有没有其他的替代方案,不需要引用“System.Windows.Forms”? - Benj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接