資源簡介
C語言讀取BMP圖片并存入工程文件夾中,適用于初學者理解BMP文件的結構和格式。開發環境:VS2010

代碼片段和文件信息
#include?
#include?
#include?“BITMAPINFO.h“
using?namespace?std;
#define?LENGTH_NAME_BMP?30 //bmp?file?name?
BITMAPFILEHEADER?strHead;
BITMAPINFOHEADER?strInfo;
RGBQUAD?strPalette[256];
void?showBmpHead(BITMAPFILEHEADER?pBmpHead)
{
cout?<“the?bitmap?header?file:?“?< cout?<“the?size?of?bitmap:?“?< cout<“Reserved?1:?“?< cout<“Reserved?2:?“?< cout<“shifted?number?of?bytes?in?image?data:?“?<}
void?showBmpInfoHead(tagBITMAPINFOHEADER?pBmpInfoHead)
{
cout?<“the?bitmap?info?header:?“?< cout?<“length?of?struct:?“?< cout?<“width?of?bitmap:?“?< cout?<“height?of?bitmap:?“?< cout?<“number?of?bit?planes:?“?< cout?<“biBitCount:?“?< cout?<“compression?method:?“?< cout?<“biSizeImage:?“?< cout?<“X?resolution:?“?< cout?<“Y?resolution:?“?< cout?<“the?number?of?used?colors:?“?< cout?<“important?colors:?“?<}
int?main()
{
char?strFile[LENGTH_NAME_BMP]?=?“lena.bmp“;
IMAGEDATA?*imagedata?=?NULL;
IMAGEDATA?*imagedataOut?=?NULL;
int?width;
int?height;
FILE?*fp?*fpo;
fp?=?fopen(strFile?“rb“);
if(?fp?!=?NULL?)
{
WORD?bfType; //the?type?of?the?file
fread(&bfType?sizeof(WORD)?1?fp);
if(?0x4d42?!=?bfType?)
{
cout?<“The?file?is?not?bmp!“?< return?0;
}
fread(&strHead?sizeof(tagBITMAPFILEHEADER)?1?fp);
showBmpHead(strHead);
fread(&strInfo?sizeof(tagBITMAPINFOHEADER)?1?fp);
showBmpInfoHead(strInfo);
//讀取調色板
for(int?i?=?0;?i? {
fread((char?*)&(strPalette[i].rgbBlue)?sizeof(BYTE)?1?fp);
fread((char?*)&(strPalette[i].rgbGreen)?sizeof(BYTE)?1?fp);
fread((char?*)&(strPalette[i].rgbRed)?sizeof(BYTE)?1?fp);
fread((char?*)&(strPalette[i].rgbReserved)?sizeof(BYTE)?1?fp);
}
width?=?strInfo.biWidth;
height?=?strInfo.biHeight;
//width?=?(width?*?sizeof(IMAGEDATA)?+?3)?/4?*?4;
width??=?((width?%?4?==?0????width?:?width?+?(4?-?(width?%?4)))?*?strInfo.biBitCount)?/?8;
imagedata?=?(IMAGEDATA?*)malloc(?width?*?height?);
imagedataOut?=?(IMAGEDATA?*)malloc(?width?*?height?*?sizeof(imagedata));
for(int?j?=?0;?j? {
for(int?i?=?0?;?i? {
(*(imagedata?+?j?*?width?+?i)).blue?=?0;
(*(imagedataOut?+?j?*?width?+?i)).blue?=?0;
}
}
//讀出圖片的像素數據??
fread(imagedata?sizeof(struct?tagIMAGEDATA)?*?width?height?fp);
fclose(fp);
}
el
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3996??2015-02-03?11:13??1_readBmp_1\1_readBmp_1.vcxproj
?????文件???????1079??2015-02-03?11:13??1_readBmp_1\1_readBmp_1.vcxproj.filters
?????文件????????143??2015-02-03?10:46??1_readBmp_1\1_readBmp_1.vcxproj.user
?????文件???????1182??2015-02-03?14:53??1_readBmp_1\BITMAPINFO.h
?????文件??????66614??2014-10-25?14:51??1_readBmp_1\lena.bmp
?????文件??????66614??2015-02-03?14:53??1_readBmp_1\out.bmp
?????文件???????4174??2015-02-03?14:34??1_readBmp_1\test.cpp
?????文件????????900??2015-02-03?10:46??1_readBmp_1.sln
?????目錄??????????0??2015-02-03?15:07??1_readBmp_1
-----------?---------??----------?-----??----
???????????????144702????????????????????9
評論
共有 條評論