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

出来的图是一片黑点点,求高手解决
...全文
588 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
diy2005 2011-06-23
  • 打赏
  • 举报
回复
而且要交换前后缓存
diy2005 2011-06-23
  • 打赏
  • 举报
回复
k,j要反过来吧
林中静思 2009-11-23
  • 打赏
  • 举报
回复
解决了,顺便也把正确的贴出来给大家参考:

for (int k =uHeight-1; k>=0; k--)
{
for (int j =0; j< uWidth; j++)
{


//不需要反过来
bit.SetPixel(k, j, Color.FromArgb(pImage[i], pImage[i + 1], pImage[i + 2]));
i++;
}

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧