BitBlt(dcMem, 0, 0, 120,40, *dc, 0, 0, SRCCOPY);
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((120*40)*3);
FILE *mStream = NULL;
if((mStream = fopen(".\\Pic\\code.bmp", "wb")))
{
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
fwrite(&(RGB16BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
fwrite(lpBitmapBits, 120*40*3, 1, mStream);
fclose(mStream);
}
http://hi.csdn.net/space-10297462-do-album-picid-1054829-goto-down.html
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((120*40)*3);
fwrite(lpBitmapBits, 3*120*40, 1, mStream);
图片的宽和高怎么设置才能保存进去啊。我用上面的信息头里面没有宽、高
LONG biWidth;
LONG biHeight;
6 个解决方案
#2
BITMAPFILEHEADER bmfHeader;
ZeroMemory(&bmfHeader,sizeof(bmfHeader));
bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ sizeof(RGBQUAD) * 256;
bmfHeader.bfSize = pBitmapInfo->bmiHeader.biSizeImage + bmfHeader.bfOffBits;
bmfHeader.bfType='MB';
HANDLE hFile=CreateFileA("test1.bmp",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
DWORD dwRet=0;
WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
WriteFile(hFile,&pBitmapInfo->bmiHeader,sizeof(pBitmapInfo->bmiHeader),&dwRet,NULL);
WriteFile(hFile,&pBitmapInfo->bmiColors[0],sizeof(RGBQUAD) * 256,&dwRet,NULL);
WriteFile(hFile,lpDib,pBitmapInfo->bmiHeader.biSizeImage,&dwRet,NULL);
CloseHandle(hFile);
}
ZeroMemory(&bmfHeader,sizeof(bmfHeader));
bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ sizeof(RGBQUAD) * 256;
bmfHeader.bfSize = pBitmapInfo->bmiHeader.biSizeImage + bmfHeader.bfOffBits;
bmfHeader.bfType='MB';
HANDLE hFile=CreateFileA("test1.bmp",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
DWORD dwRet=0;
WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
WriteFile(hFile,&pBitmapInfo->bmiHeader,sizeof(pBitmapInfo->bmiHeader),&dwRet,NULL);
WriteFile(hFile,&pBitmapInfo->bmiColors[0],sizeof(RGBQUAD) * 256,&dwRet,NULL);
WriteFile(hFile,lpDib,pBitmapInfo->bmiHeader.biSizeImage,&dwRet,NULL);
CloseHandle(hFile);
}
#4
这是我的完整程序:信息头里没有120×40(就是40 38)没有;
CBitmap hbitmap;//参数可选
HDC dcMem;
dcMem = CreateCompatibleDC(*dc); //guanlian
BITMAPINFO RGB16BitsBITMAPINFO;
ZeroMemory(&RGB16BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB16BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB16BitsBITMAPINFO.bmiHeader.biWidth = 120;
RGB16BitsBITMAPINFO.bmiHeader.biHeight = 40;
RGB16BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB16BitsBITMAPINFO.bmiHeader.biBitCount = 24;
BYTE *lpBitmapBits = NULL;
HBITMAP directBmp = CreateDIBSection(dcMem, (BITMAPINFO*)&RGB16BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(dcMem, directBmp);
BitBlt(dcMem, 0, 0, 120,40, *dc, 0, 0, SRCCOPY);
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfReserved1=0;
bmBITMAPFILEHEADER.bfReserved2=0;
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((120*40)*3);
FILE *mStream = NULL;
if((mStream = fopen(".\\Pic\\code.bmp", "wb")))
{
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
fwrite(&(RGB16BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
fwrite(lpBitmapBits, 3*120*40, 1, mStream);
fclose(mStream);
}
DeleteObject(dcMem);
DeleteObject(dc);
DeleteObject(directBmp);
DeleteObject(previousObject);
示例都看了,看不出个所以然来。我的用上面的程序保存24位BMP文件后,再读取该位图数据就出错??
CBitmap hbitmap;//参数可选
HDC dcMem;
dcMem = CreateCompatibleDC(*dc); //guanlian
BITMAPINFO RGB16BitsBITMAPINFO;
ZeroMemory(&RGB16BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB16BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB16BitsBITMAPINFO.bmiHeader.biWidth = 120;
RGB16BitsBITMAPINFO.bmiHeader.biHeight = 40;
RGB16BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB16BitsBITMAPINFO.bmiHeader.biBitCount = 24;
BYTE *lpBitmapBits = NULL;
HBITMAP directBmp = CreateDIBSection(dcMem, (BITMAPINFO*)&RGB16BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(dcMem, directBmp);
BitBlt(dcMem, 0, 0, 120,40, *dc, 0, 0, SRCCOPY);
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfReserved1=0;
bmBITMAPFILEHEADER.bfReserved2=0;
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((120*40)*3);
FILE *mStream = NULL;
if((mStream = fopen(".\\Pic\\code.bmp", "wb")))
{
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
fwrite(&(RGB16BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
fwrite(lpBitmapBits, 3*120*40, 1, mStream);
fclose(mStream);
}
DeleteObject(dcMem);
DeleteObject(dc);
DeleteObject(directBmp);
DeleteObject(previousObject);
示例都看了,看不出个所以然来。我的用上面的程序保存24位BMP文件后,再读取该位图数据就出错??
#5
我读取的程序是这样的:
FILE *fp=fopen(BMP_filename,"rb");
if(fp==NULL)
{
AfxMessageBox("文件不存在!");
}
BITMAPFILEHEADER fileheader;
BITMAPINFO info;
fread(&fileheader,sizeof(fileheader),1,fp);
if(fileheader.bfType!=0x4D42)
{
AfxMessageBox("无位图文件请选择位图文件");
fclose(fp);
}
fread(&info.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
long width=info.bmiHeader.biWidth;
long height=info.bmiHeader.biHeight;
UCHAR *buffer=new UCHAR[info.bmiHeader.biSizeImage];
fseek(fp,fileheader.bfOffBits,0);
fseek(fp,4,0);
fread(buffer,info.bmiHeader.biSizeImage,1,fp);
if(info.bmiHeader.biBitCount==24)
{
AfxMessageBox("位图 == 24");
int pitch=width%4;
if(height>0)
{
for(int i=height;i>0;i--)
{
int realPitch=i*pitch;
for(int j=0;j<width;j++)
{ ///此处就出错了///
YZM_red[i][j] = buffer[(i*width+j)*3+2+realPitch];
YZM_green[i][j] = buffer[(i*width+j)*3+1+realPitch];
YZM_blue[i][j] = buffer[(i*width+j)*3+realPitch];
FILE *fp=fopen(BMP_filename,"rb");
if(fp==NULL)
{
AfxMessageBox("文件不存在!");
}
BITMAPFILEHEADER fileheader;
BITMAPINFO info;
fread(&fileheader,sizeof(fileheader),1,fp);
if(fileheader.bfType!=0x4D42)
{
AfxMessageBox("无位图文件请选择位图文件");
fclose(fp);
}
fread(&info.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
long width=info.bmiHeader.biWidth;
long height=info.bmiHeader.biHeight;
UCHAR *buffer=new UCHAR[info.bmiHeader.biSizeImage];
fseek(fp,fileheader.bfOffBits,0);
fseek(fp,4,0);
fread(buffer,info.bmiHeader.biSizeImage,1,fp);
if(info.bmiHeader.biBitCount==24)
{
AfxMessageBox("位图 == 24");
int pitch=width%4;
if(height>0)
{
for(int i=height;i>0;i--)
{
int realPitch=i*pitch;
for(int j=0;j<width;j++)
{ ///此处就出错了///
YZM_red[i][j] = buffer[(i*width+j)*3+2+realPitch];
YZM_green[i][j] = buffer[(i*width+j)*3+1+realPitch];
YZM_blue[i][j] = buffer[(i*width+j)*3+realPitch];
#6
用画图保存的BMP就可以,不出错!
#1
#2
BITMAPFILEHEADER bmfHeader;
ZeroMemory(&bmfHeader,sizeof(bmfHeader));
bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ sizeof(RGBQUAD) * 256;
bmfHeader.bfSize = pBitmapInfo->bmiHeader.biSizeImage + bmfHeader.bfOffBits;
bmfHeader.bfType='MB';
HANDLE hFile=CreateFileA("test1.bmp",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
DWORD dwRet=0;
WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
WriteFile(hFile,&pBitmapInfo->bmiHeader,sizeof(pBitmapInfo->bmiHeader),&dwRet,NULL);
WriteFile(hFile,&pBitmapInfo->bmiColors[0],sizeof(RGBQUAD) * 256,&dwRet,NULL);
WriteFile(hFile,lpDib,pBitmapInfo->bmiHeader.biSizeImage,&dwRet,NULL);
CloseHandle(hFile);
}
ZeroMemory(&bmfHeader,sizeof(bmfHeader));
bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ sizeof(RGBQUAD) * 256;
bmfHeader.bfSize = pBitmapInfo->bmiHeader.biSizeImage + bmfHeader.bfOffBits;
bmfHeader.bfType='MB';
HANDLE hFile=CreateFileA("test1.bmp",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
DWORD dwRet=0;
WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
WriteFile(hFile,&pBitmapInfo->bmiHeader,sizeof(pBitmapInfo->bmiHeader),&dwRet,NULL);
WriteFile(hFile,&pBitmapInfo->bmiColors[0],sizeof(RGBQUAD) * 256,&dwRet,NULL);
WriteFile(hFile,lpDib,pBitmapInfo->bmiHeader.biSizeImage,&dwRet,NULL);
CloseHandle(hFile);
}
#3
#4
这是我的完整程序:信息头里没有120×40(就是40 38)没有;
CBitmap hbitmap;//参数可选
HDC dcMem;
dcMem = CreateCompatibleDC(*dc); //guanlian
BITMAPINFO RGB16BitsBITMAPINFO;
ZeroMemory(&RGB16BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB16BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB16BitsBITMAPINFO.bmiHeader.biWidth = 120;
RGB16BitsBITMAPINFO.bmiHeader.biHeight = 40;
RGB16BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB16BitsBITMAPINFO.bmiHeader.biBitCount = 24;
BYTE *lpBitmapBits = NULL;
HBITMAP directBmp = CreateDIBSection(dcMem, (BITMAPINFO*)&RGB16BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(dcMem, directBmp);
BitBlt(dcMem, 0, 0, 120,40, *dc, 0, 0, SRCCOPY);
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfReserved1=0;
bmBITMAPFILEHEADER.bfReserved2=0;
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((120*40)*3);
FILE *mStream = NULL;
if((mStream = fopen(".\\Pic\\code.bmp", "wb")))
{
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
fwrite(&(RGB16BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
fwrite(lpBitmapBits, 3*120*40, 1, mStream);
fclose(mStream);
}
DeleteObject(dcMem);
DeleteObject(dc);
DeleteObject(directBmp);
DeleteObject(previousObject);
示例都看了,看不出个所以然来。我的用上面的程序保存24位BMP文件后,再读取该位图数据就出错??
CBitmap hbitmap;//参数可选
HDC dcMem;
dcMem = CreateCompatibleDC(*dc); //guanlian
BITMAPINFO RGB16BitsBITMAPINFO;
ZeroMemory(&RGB16BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB16BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB16BitsBITMAPINFO.bmiHeader.biWidth = 120;
RGB16BitsBITMAPINFO.bmiHeader.biHeight = 40;
RGB16BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB16BitsBITMAPINFO.bmiHeader.biBitCount = 24;
BYTE *lpBitmapBits = NULL;
HBITMAP directBmp = CreateDIBSection(dcMem, (BITMAPINFO*)&RGB16BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(dcMem, directBmp);
BitBlt(dcMem, 0, 0, 120,40, *dc, 0, 0, SRCCOPY);
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfReserved1=0;
bmBITMAPFILEHEADER.bfReserved2=0;
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((120*40)*3);
FILE *mStream = NULL;
if((mStream = fopen(".\\Pic\\code.bmp", "wb")))
{
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
fwrite(&(RGB16BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
fwrite(lpBitmapBits, 3*120*40, 1, mStream);
fclose(mStream);
}
DeleteObject(dcMem);
DeleteObject(dc);
DeleteObject(directBmp);
DeleteObject(previousObject);
示例都看了,看不出个所以然来。我的用上面的程序保存24位BMP文件后,再读取该位图数据就出错??
#5
我读取的程序是这样的:
FILE *fp=fopen(BMP_filename,"rb");
if(fp==NULL)
{
AfxMessageBox("文件不存在!");
}
BITMAPFILEHEADER fileheader;
BITMAPINFO info;
fread(&fileheader,sizeof(fileheader),1,fp);
if(fileheader.bfType!=0x4D42)
{
AfxMessageBox("无位图文件请选择位图文件");
fclose(fp);
}
fread(&info.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
long width=info.bmiHeader.biWidth;
long height=info.bmiHeader.biHeight;
UCHAR *buffer=new UCHAR[info.bmiHeader.biSizeImage];
fseek(fp,fileheader.bfOffBits,0);
fseek(fp,4,0);
fread(buffer,info.bmiHeader.biSizeImage,1,fp);
if(info.bmiHeader.biBitCount==24)
{
AfxMessageBox("位图 == 24");
int pitch=width%4;
if(height>0)
{
for(int i=height;i>0;i--)
{
int realPitch=i*pitch;
for(int j=0;j<width;j++)
{ ///此处就出错了///
YZM_red[i][j] = buffer[(i*width+j)*3+2+realPitch];
YZM_green[i][j] = buffer[(i*width+j)*3+1+realPitch];
YZM_blue[i][j] = buffer[(i*width+j)*3+realPitch];
FILE *fp=fopen(BMP_filename,"rb");
if(fp==NULL)
{
AfxMessageBox("文件不存在!");
}
BITMAPFILEHEADER fileheader;
BITMAPINFO info;
fread(&fileheader,sizeof(fileheader),1,fp);
if(fileheader.bfType!=0x4D42)
{
AfxMessageBox("无位图文件请选择位图文件");
fclose(fp);
}
fread(&info.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
long width=info.bmiHeader.biWidth;
long height=info.bmiHeader.biHeight;
UCHAR *buffer=new UCHAR[info.bmiHeader.biSizeImage];
fseek(fp,fileheader.bfOffBits,0);
fseek(fp,4,0);
fread(buffer,info.bmiHeader.biSizeImage,1,fp);
if(info.bmiHeader.biBitCount==24)
{
AfxMessageBox("位图 == 24");
int pitch=width%4;
if(height>0)
{
for(int i=height;i>0;i--)
{
int realPitch=i*pitch;
for(int j=0;j<width;j++)
{ ///此处就出错了///
YZM_red[i][j] = buffer[(i*width+j)*3+2+realPitch];
YZM_green[i][j] = buffer[(i*width+j)*3+1+realPitch];
YZM_blue[i][j] = buffer[(i*width+j)*3+realPitch];
#6
用画图保存的BMP就可以,不出错!