一旦我用LoadImage从文件中加载位图:
HBITMAP renderBMP = (HBITMAP)LoadImage( NULL, filePath, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE );有没有一种方法可以方便地单独访问和编辑像素?
我可以使用它来获取位图对象,但它似乎没有帮助,
BITMAP bm;
GetObject(renderBMP, sizeof(bm), &bm);因为结构中bmBits的值是0。
更新:
现在我在这个解决方案中遇到了一个bug:
struct Pixel { unsigned char r,g,b,a; };
void Frame::PushMemory(HDC hdc)
{
BITMAPINFO bi;
ZeroMemory(&bi.bmiHeader, sizeof(BITMAPINFOHEADER));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
GetDIBits(hdc, renderBMP, 0, bi.bmiHeader.biHeight, NULL, &bi, DIB_RGB_COLORS);
/* Allocate memory for bitmap bits */
Pixel* pixels = new Pixel[bi.bmiHeader.biHeight * bi.bmiHeader.biWidth];
int n = sizeof(Pixel) * bi.bmiHeader.biHeight * bi.bmiHeader.biWidth;
int m = bi.bmiHeader.biSizeImage;
GetDIBits(hdc, renderBMP, 0, bi.bmiHeader.biHeight, pixels, &bi, DIB_RGB_COLORS);
// Recompute the output
//ComputeOutput(pixels);
// Push back to windows
//SetDIBits(hdc, renderBMP, 0, bi.bmiHeader.biHeight, pixels, &bi, DIB_RGB_COLORS );
//delete pixels;
}我得到了这个错误:
运行时检查失败#2 -变量'bi‘周围的堆栈已损坏。
最后三行似乎与是否添加注释并不重要。
发布于 2010-12-16 02:55:38
使用GetDIBits访问像素。它将所有像素复制到指定的缓冲区中。修改像素后,可以使用SetDIBits将像素写回到位图中。
编辑:代码示例:
LPVOID lpvBits=NULL; // pointer to bitmap bits array
BITMAPINFO bi;
ZeroMemory(&bi.bmiHeader, sizeof(BITMAPINFOHEADER));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
if (!GetDIBits(hDC, hBmp, 0, height, NULL, &bi, DIB_RGB_COLORS))
return NULL;
/* Allocate memory for bitmap bits */
if ((lpvBits = new char[bi.bmiHeader.biSizeImage]) == NULL)
return NULL;
if (!GetDIBits(hDC, hBmp, 0, height, lpvBits, &bi, DIB_RGB_COLORS))
return NULL;
/* do something with bits */
::SetDIBits( hDC, hBmp, 0, height, ( LPVOID )lpvBits, &bi, DIB_RGB_COLORS );发布于 2010-12-16 03:05:24
如果您将LR_CREATEDIBSECTION标志传递给LoadImage,它将创建一种特殊类型的位图,其中包含一个包含位图的位的用户模式内存区。
DIBSection位图上的GetObject将填充位图结构的bmPits指针,甚至用额外的数据填充DIBSECTION结构。
https://stackoverflow.com/questions/4453677
复制相似问题