我想从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);出来的图是一片黑点点,求高手解决
// 获得客户区矩形
[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);出来的图是一片黑点点,求高手解决
解决方案 »
- C# 的线程问题...这问题困扰一星期了,哎问了好多次了,没人可以解答得清楚么?
- C/S 软件自动运行问题
- WINFORM窗体tab键的问题
- 小弟 新手 求CS开发中文件下载的 代码
- 如何将一个string的字符串转成一个对象变量
- vs2005运行添加删除,说找不到vs_setup.msi,把HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\下包含vs_setup.msi的那个子项中的Patches子项删除,还是报这错,怎么
- 求C# IO读写的问题!
- BindgingNavigator 如何控件DataGridVIew?
- OUTLOOK 加密码 急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 难就一个字!——在引用非托管WIN API中函数时,对原型中的char*如何转换成托管中的类型?
- C#自动填写窗体(类似外挂功能)
- 用户 'sa' 登录失败。该用户与可信 SQL Server 连接无关联。
{
for (int j =0; j< uWidth; j++)
{
//不需要反过来
bit.SetPixel(k, j, Color.FromArgb(pImage[i], pImage[i + 1], pImage[i + 2]));
i++;
}