c#中,从OpenGL缓存利用glReadPixels读取出RGB像素,生成BMP出错,求解决
林中静思 2009-11-23 10:11:35 我想从OpenGL缓存中读取图片,然后保存到本地
// 获得客户区矩形
[DllImport("user32.dll")]
public static extern int GetClientRect(IntPtr hWnd, out RECT lpRect);
// 矩形结构
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
IntPtr hwnd = this.Handle;
RECT rc;
GetClientRect(hwnd,out rc);
int uWidth=rc.right-rc.left,uHeight=rc.bottom-rc.top;
//分配BMP图片所需空间
byte [] pImage = new byte[uWidth * uHeight * 3];
//uint [] pImage= new uint[uWidth * uHeight * 3];
// 读取屏幕像素
CsGL.OpenGL.GL.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
CsGL.OpenGL.GL.glReadPixels(0, 0, uWidth, uHeight, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pImage);
CsGL.OpenGL.GL.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
下面怎么写?我是这样写的,很笨拙的办法
Bitmap bit = new Bitmap(uWidth, uHeight);
int i = 0;
for(int k=0;k<uWidth;k++)
{
for(int j=0;j<uHeight;j++)
{
//因为得到的是BGR像素,所以这里反过来
bit.SetPixel(k, j, Color.FromArgb(pImage[i + 2], pImage[i + 1], pImage[i]));
i++;
}
}
bit.Save("C:\\222.bmp", ImageFormat.Bmp);
出来的图是一片黑点点,求高手解决