gpt4 book ai didi

c# - 如何捕获使用opentk绘制的图像

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:36 24 4
gpt4 key购买 nike

我的目标是在 GL_Control(一个 gui 控件)上用 c# 中的 opentk 绘制东西,并在每次调用 paint 事件时将其保存到 Bitmap 对象中。我有这段代码:

private void glControl1_Paint(object sender, PaintEventArgs e)
{
// do lots of drawing here
GL.Finish();
GL.Flush();
glControl1.SwapBuffers();
gl_image = TakeScreenshot();
}

public Bitmap TakeScreenshot()
{
if (GraphicsContext.CurrentContext == null)
throw new GraphicsContextMissingException();
int w = glControl1.ClientSize.Width;
int h = glControl1.ClientSize.Height;
Bitmap bmp = new Bitmap(w, h);
System.Drawing.Imaging.BitmapData data =
bmp.LockBits(glControl1.ClientRectangle, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
GL.ReadPixels(0, 0, w, h, PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0);
bmp.UnlockBits(data);

bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
return bmp;
}

所以在交换缓冲区后的绘画事件中,我拍摄了 schreenshot。问题是捕获的图像是绘图之前的状态。这意味着如果我想捕捉图像,我需要运行两次绘画事件。我试过 GL.flush 和 finish 以及 swapbuffer。有谁知道如何克服这个问题。另请注意,我曾尝试使用异步方式但失败了,因为您无法从另一个线程访问 opentk 图像数据。

最佳答案

我遇到了完全相同的问题,这是我解决它的方法。当您调用 glControl1.Invalidate() 刷新图像时,OpenTK 实际上会在最后完成。因此,如果您在该滴答期间抓取屏幕截图,则缓冲区将在下一个周期之前不会更新。

你需要做的是强制刷新glControl1,这是代码。

public void GLrefresh()
{
glControl1.Invalidate();
glControl1.Update();
glControl1.Refresh();
}

在抓取屏幕截图之前调用此函数

GLrefresh();
Image I = TakeScreenshot();
//Now Image I should be the image of the current buffer

关于c# - 如何捕获使用opentk绘制的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25173224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com