请教高手BMP图象的读入及图象数据的提取问题

point13 2006-04-20 04:00:39
请问如何才能把一幅BMP图象读到内存中去,把图象数据提取出来存入一个二维数组中去,请高手多多指教,小弟不胜感激!!!!!!!!!!!!
...全文
174 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
buggycode 2006-04-21
  • 打赏
  • 举报
回复
上面的程序没有用的小心。不信的自己那PhotoShop各种类型试。

soaroc 2006-04-20
  • 打赏
  • 举报
回复
出自《精通VisualC++图像处理编程》
先通过CreateFile取得位图的hFile
/*************************************************************************
*
* Function: ReadDIBFile (int)
*
* Purpose: Reads in the specified DIB file into a global chunk of
* memory.
*
* Returns: A handle to a dib (hDIB) if successful.
* NULL if an error occurs.
*
* Comments: BITMAPFILEHEADER is stripped off of the DIB. Everything
* from the end of the BITMAPFILEHEADER structure on is
* returned in the global memory handle.
*
*
* NOTE: The DIB API were not written to handle OS/2 DIBs, so this
* function will reject any file that is not a Windows DIB.
*
*************************************************************************/
HANDLE ReadDIBFile(HANDLE hFile)
{
BITMAPFILEHEADER bmfHeader;
DWORD dwBitsSize;
UINT nNumColors; // Number of colors in table
HANDLE hDIB;
HANDLE hDIBtmp; // Used for GlobalRealloc() //MPB
LPBITMAPINFOHEADER lpbi;
DWORD offBits;
DWORD dwRead;

// get length of DIB in bytes for use when reading

dwBitsSize = GetFileSize(hFile, NULL);

// Allocate memory for header & color table. We'll enlarge this
// memory as needed.

hDIB = GlobalAlloc(GMEM_MOVEABLE, (DWORD)(sizeof(BITMAPINFOHEADER) +
256 * sizeof(RGBQUAD)));

if (!hDIB)
return NULL;

lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

if (!lpbi)
{
GlobalFree(hDIB);
return NULL;
}

// read the BITMAPFILEHEADER from our file

if (!ReadFile(hFile, (LPBYTE)&bmfHeader, sizeof (BITMAPFILEHEADER),
&dwRead, NULL))
goto ErrExit;

if (sizeof (BITMAPFILEHEADER) != dwRead)
goto ErrExit;

if (bmfHeader.bfType != 0x4d42) // 'BM'
goto ErrExit;

// read the BITMAPINFOHEADER

if (!ReadFile(hFile, (LPBYTE)lpbi, sizeof(BITMAPINFOHEADER), &dwRead,
NULL))
goto ErrExit;

if (sizeof(BITMAPINFOHEADER) != dwRead)
goto ErrExit;

// Check to see that it's a Windows DIB -- an OS/2 DIB would cause
// strange problems with the rest of the DIB API since the fields
// in the header are different and the color table entries are
// smaller.
//
// If it's not a Windows DIB (e.g. if biSize is wrong), return NULL.

if (lpbi->biSize == sizeof(BITMAPCOREHEADER))
goto ErrExit;

// Now determine the size of the color table and read it. Since the
// bitmap bits are offset in the file by bfOffBits, we need to do some
// special processing here to make sure the bits directly follow
// the color table (because that's the format we are susposed to pass
// back)

if (!(nNumColors = (UINT)lpbi->biClrUsed))
{
// no color table for 24-bit, default size otherwise

if (lpbi->biBitCount != 24)
nNumColors = 1 << lpbi->biBitCount; // standard size table
}

// fill in some default values if they are zero

if (lpbi->biClrUsed == 0)
lpbi->biClrUsed = nNumColors;

if (lpbi->biSizeImage == 0)
{
lpbi->biSizeImage = ((((lpbi->biWidth * (DWORD)lpbi->biBitCount) +
31) & ~31) >> 3) * lpbi->biHeight;
}

// get a proper-sized buffer for header, color table and bits

GlobalUnlock(hDIB);
hDIBtmp = GlobalReAlloc(hDIB, lpbi->biSize + nNumColors *
sizeof(RGBQUAD) + lpbi->biSizeImage, 0);

if (!hDIBtmp) // can't resize buffer for loading
goto ErrExitNoUnlock; //MPB
else
hDIB = hDIBtmp;

lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

// read the color table

ReadFile (hFile, (LPBYTE)(lpbi) + lpbi->biSize,
nNumColors * sizeof(RGBQUAD), &dwRead, NULL);

// offset to the bits from start of DIB header

offBits = lpbi->biSize + nNumColors * sizeof(RGBQUAD);

// If the bfOffBits field is non-zero, then the bits might *not* be
// directly following the color table in the file. Use the value in
// bfOffBits to seek the bits.

if (bmfHeader.bfOffBits != 0L)
SetFilePointer(hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);

if (ReadFile(hFile, (LPBYTE)lpbi + offBits, lpbi->biSizeImage, &dwRead,
NULL))
goto OKExit;

ErrExit:
GlobalUnlock(hDIB);

ErrExitNoUnlock:
GlobalFree(hDIB);
return NULL;

OKExit:
GlobalUnlock(hDIB);
return hDIB;
}
取得了句柄后要使用时使用GlobalLock即可获得指针,不过这是一维数组,试一下memcpy可不可以自动的转化为二维的
FOX7899 2006-04-20
  • 打赏
  • 举报
回复
恩 好 我也学习
菜牛 2006-04-20
  • 打赏
  • 举报
回复
ReadFile
thisisll 2006-04-20
  • 打赏
  • 举报
回复
http://www.vckbase.com/document/viewdoc/?id=674
http://community.csdn.net/Expert/topic/4701/4701726.xml?temp=.4967157
Kudeet 2006-04-20
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=202953
cup51 2006-04-20
  • 打赏
  • 举报
回复
要是用matlab很简单,就看看imread这个命令,help一下就行了
要是MFC也不难
网上有很多程序

19,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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