D
deepfar
Unregistered / Unconfirmed
GUEST, unregistred user!
#include "stdAfx.h"
#include "Image.h"
// 从 BMP 文件中读取像素数组 ( 只支持 RGB24 真彩图 )
BYTE* LoadBmpFromFile(LPCTSTR strFileName, LONG& lWidth, LONG& lHeight)
{
HANDLE bmpFile = NULL;
bmpFile = CreateFile(strFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (bmpFile == INVALID_HANDLE_VALUE)
return NULL;
DWORD dwRead = 0;
BITMAPFILEHEADER bfh = { NULL };
ReadFile(bmpFile, &bfh, sizeof(BITMAPFILEHEADER), &dwRead, NULL);
if (bfh.bfType != 0x4d42)
{
CloseHandle(bmpFile);
return NULL;
}
BITMAPINFOHEADER bih = { NULL };
ReadFile(bmpFile, &bih, sizeof(BITMAPINFOHEADER), &dwRead, NULL);
if (bih.biBitCount != 24)
{
CloseHandle(bmpFile);
return NULL;
}
UINT bmpSize = bih.biHeight * WIDTHBYTES(bih.biWidth * bih.biBitCount);
BYTE* imageBuf = new BYTE[bmpSize];
ReadFile(bmpFile, imageBuf, bmpSize, &dwRead, NULL);
if (dwRead != bmpSize)
{
delete [] imageBuf;
imageBuf = NULL;
}
CloseHandle(bmpFile);
lWidth = bih.biWidth;
lHeight = bih.biHeight;
return imageBuf;
}
#include "Image.h"
// 从 BMP 文件中读取像素数组 ( 只支持 RGB24 真彩图 )
BYTE* LoadBmpFromFile(LPCTSTR strFileName, LONG& lWidth, LONG& lHeight)
{
HANDLE bmpFile = NULL;
bmpFile = CreateFile(strFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (bmpFile == INVALID_HANDLE_VALUE)
return NULL;
DWORD dwRead = 0;
BITMAPFILEHEADER bfh = { NULL };
ReadFile(bmpFile, &bfh, sizeof(BITMAPFILEHEADER), &dwRead, NULL);
if (bfh.bfType != 0x4d42)
{
CloseHandle(bmpFile);
return NULL;
}
BITMAPINFOHEADER bih = { NULL };
ReadFile(bmpFile, &bih, sizeof(BITMAPINFOHEADER), &dwRead, NULL);
if (bih.biBitCount != 24)
{
CloseHandle(bmpFile);
return NULL;
}
UINT bmpSize = bih.biHeight * WIDTHBYTES(bih.biWidth * bih.biBitCount);
BYTE* imageBuf = new BYTE[bmpSize];
ReadFile(bmpFile, imageBuf, bmpSize, &dwRead, NULL);
if (dwRead != bmpSize)
{
delete [] imageBuf;
imageBuf = NULL;
}
CloseHandle(bmpFile);
lWidth = bih.biWidth;
lHeight = bih.biHeight;
return imageBuf;
}