c# - 如何在 C# 中使用 opengl 捕获桌面屏幕截图?

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

最佳答案

要捕获完整的屏幕截图,请使用:

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/

相关文章:

c# - 在 Azure DevOps Repos 上拆分大型软件项目

c# - 使用泛型创建自定义控件

c# - 确定系统架构

c# - Redis 消息包含 unicode?

c# - 在 SQL 2005/2008 中转义双引号

c# - JavaScript 到 C# 的转换 - 未知数据类型

javascript - javascript 的 unescape() 的 c# 等价物是什么?

c# - 跨域创建cookie

c# - 如何从网站中删除ajax工具包

c# - 始终在 WCF 服务中使用 Json 返回 XML 字符串