获取了指向bmp数据头的指针,请问通过何种方式,保存该数据为bmp文件到本地?

devil521 2009-05-07 05:15:08
获取了指向bmp数据头的指针,请问通过何种方式,保存该数据为bmp文件到本地?
...全文
242 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizhigang34 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 devil521 的回复:]
觉得不对啊,你给这个函数不是得用来到句柄的吧?是得给这个函数提供图片句柄吧~!~?
[/Quote]
你前面不是说,你已经得到了位图的数据吗??
这个函数会返回一个位图句柄的!
lambochan 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 devil521 的回复:]
引用 7 楼 lambochan 的回复:
引用楼主 devil521 的帖子:
获取了指向bmp数据头的指针,请问通过何种方式,保存该数据为bmp文件到本地?


具体说清楚点,那是什么数据的指针?
是整个BMP数据(BITMAPINFO & BitsData)还是只是些原始的raw数据?

是指向整个BMP数据的指针
[/Quote]

那太简单了,如果没有fileheader的话,自己填写就可以保存了..like:

#define WIDTHBYTES(w,b) ( ( ( ( w ) * ( b ) + 31 ) >> 5 ) << 2 )

BOOL Save( LPCTSTR lpszFileName /*文件名*/, LPBYTE lpBMP/*你的指针*/ )
{
CFile file;
if( !file.Open( lpszFileNaem, CFile::modeCreate|CFile::modeWrite ) )
return FALSE;
BITMAPFILEHEADER bmfh;
LPBITMAPINFOHEADER lpBmih = ( LPBITMAPINFOHEADER )lpBMP;
if( lpBmih == NULL )
return FALSE;
memset( &bmfh, 0x0, sizeof( BITMAPFILEHEADER ) );
DWORD dwColors = lpBmih->biBitCount <= 8 ? 1 << lpBmih->biBitCount : 0;
if( lpBmih->biCompression == BI_BITFIELD )
dwColors = 3;
DWORD dwInfoSize = sizeof( BITMAPINFOHEADER ) + dwColors * sizeof( RGBQUAD );
DWORD dwImageSize = WIDTHBYTES( llpBmih->biWidth, lpBmih->biBitCount ) * lpBmih->biHeight;

bmfh.bfType = 0x4d42;
bmfh.bfSize = sizeof( BITMAPFILEHEADER ) + dwInfoSize + dwImageSize;
bmfh.bfOffBits = sizeof( BITMAPFILEHEADER ) + dwInfoSize;
file.Write( &bmfh, sizeof( BITMAPFILEHEADER ) );
fiel.WriteHuge( lpBMP, dwInfoSize+dwImageSize );
return TRUE;
}
devil521 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lambochan 的回复:]
引用楼主 devil521 的帖子:
获取了指向bmp数据头的指针,请问通过何种方式,保存该数据为bmp文件到本地?


具体说清楚点,那是什么数据的指针?
是整个BMP数据(BITMAPINFO & BitsData)还是只是些原始的raw数据?
[/Quote]
是指向整个BMP数据的指针
lambochan 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 devil521 的帖子:]
获取了指向bmp数据头的指针,请问通过何种方式,保存该数据为bmp文件到本地?
[/Quote]

具体说清楚点,那是什么数据的指针?
是整个BMP数据(BITMAPINFO & BitsData)还是只是些原始的raw数据?
devil521 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lizhigang34 的回复:]
得到BMP数据后,可以用下面这个函数获得HBITMAP
C/C++ code
The SetDIBits function sets the pixels in a bitmap using the color data found in the specified DIB .

int SetDIBits(
HDC hdc, // handle to DC
HBITMAP hbmp, // handle to bitmap
UINT uStartScan, // starting scan line
UINT cScanLines, // number of scan lines
CONST VOID *lpvBit…
[/Quote]
觉得不对啊,你给这个函数不是得用来到句柄的吧?是得给这个函数提供图片句柄吧~!~?
yong_hen 2009-05-11
  • 打赏
  • 举报
回复
你都得到指针了,要保存很简单的了。
fopen(...);
fwrite(...);
fclose(..);

这样就可以了。

楼上给的是读取BMP的方法
devil521 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lambochan 的回复:]
引用 8 楼 devil521 的回复:
引用 7 楼 lambochan 的回复:
引用楼主 devil521 的帖子:
获取了指向bmp数据头的指针,请问通过何种方式,保存该数据为bmp文件到本地?
 
 
具体说清楚点,那是什么数据的指针?
是整个BMP数据(BITMAPINFO & BitsData)还是只是些原始的raw数据?

是指向整个BMP数据的指针


那太简单了,如果没有fileheader的话,自己填写就可以保存了..like:
C/C++ code
#define WIDT…
[/Quote]噢~我给指针给错了....谢谢你的代码~~
lambochan 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 devil521 的回复:]
就是最后的file.Write( &DestBuf, dwInfoSize+dwImageSize ); 触发了异常~
[/Quote]

那你的那个指针估计有问题,如下修改试试:
BOOL Save( LPCTSTR lpszFileName /*文件名*/, LPBYTE lpBMP/*你的指针*/ )
{
CFile file;
if( !file.Open( lpszFileNaem, CFile::modeCreate|CFile::modeWrite ) )
return FALSE;
BITMAPFILEHEADER bmfh;
LPBITMAPINFOHEADER lpBmih = ( LPBITMAPINFOHEADER )lpBMP;
if( lpBmih == NULL )
return FALSE;
// add this..
if( lpBmih->biSize != sizeof( BITMAPINFOHEADER ){
MessageBox( NULL, "错误:不是位图数据! , "ERROR!", MB_OK );
return FALSE;
}
memset( &bmfh, 0x0, sizeof( BITMAPFILEHEADER ) );
DWORD dwColors = lpBmih->biBitCount <= 8 ? 1 << lpBmih->biBitCount : 0;
if( lpBmih->biCompression == BI_BITFIELD )
dwColors = 3;
DWORD dwInfoSize = sizeof( BITMAPINFOHEADER ) + dwColors * sizeof( RGBQUAD );
DWORD dwImageSize = WIDTHBYTES( lpBmih->biWidth, lpBmih->biBitCount ) * lpBmih->biHeight;

bmfh.bfType = 0x4d42;
bmfh.bfSize = sizeof( BITMAPFILEHEADER ) + dwInfoSize + dwImageSize;
bmfh.bfOffBits = sizeof( BITMAPFILEHEADER ) + dwInfoSize;
file.Write( &bmfh, sizeof( BITMAPFILEHEADER ) );
fiel.WriteHuge( lpBMP, dwInfoSize+dwImageSize );
return TRUE;
}

devil521 2009-05-11
  • 打赏
  • 举报
回复
就是最后的file.Write( &DestBuf, dwInfoSize+dwImageSize ); 触发了异常~
devil521 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lambochan 的回复:]
引用 8 楼 devil521 的回复:
引用 7 楼 lambochan 的回复:
引用楼主 devil521 的帖子:
获取了指向bmp数据头的指针,请问通过何种方式,保存该数据为bmp文件到本地?
 
 
具体说清楚点,那是什么数据的指针?
是整个BMP数据(BITMAPINFO & BitsData)还是只是些原始的raw数据?

是指向整个BMP数据的指针


那太简单了,如果没有fileheader的话,自己填写就可以保存了..like:
C/C++ code
#define WIDT…
[/Quote]

谢谢你的代码,我在试你的代码的时候,在最后的写入中,触发了异常,说不能访问我创建的图片文件。

CFile file;
file.Open( L"1.bmp", CFile::modeCreate|CFile::modeWrite );

BITMAPFILEHEADER bmfh;
LPBITMAPINFOHEADER lpBmih = ( LPBITMAPINFOHEADER )DestBuf;

memset( &bmfh, 0x0, sizeof( BITMAPFILEHEADER));
DWORD dwColors = lpBmih->biBitCount <= 8 ? 1 << lpBmih->biBitCount : 0;
if( lpBmih->biCompression == BI_BITFIELDS )
dwColors = 3;
DWORD dwInfoSize = sizeof( BITMAPINFOHEADER ) + dwColors * sizeof( RGBQUAD );
DWORD dwImageSize = WIDTHBYTES( lpBmih->biWidth, lpBmih->biBitCount ) * lpBmih->biHeight;

bmfh.bfType = 0x4d42;
bmfh.bfSize = sizeof( BITMAPFILEHEADER ) + dwInfoSize + dwImageSize;
bmfh.bfOffBits = sizeof( BITMAPFILEHEADER ) + dwInfoSize;
file.Write( &bmfh, sizeof( BITMAPFILEHEADER ) );
file.Write( &DestBuf, dwInfoSize+dwImageSize );
devil521 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 lizhigang34 的回复:]
引用 6 楼 devil521 的回复:
觉得不对啊,你给这个函数不是得用来到句柄的吧?是得给这个函数提供图片句柄吧~!~?

你前面不是说,你已经得到了位图的数据吗??
这个函数会返回一个位图句柄的!
[/Quote]
我得到的数据并不是位图类的,只是图像的数据....(或者是我没听懂你的意思???)
biweilun 2009-05-07
  • 打赏
  • 举报
回复
http://blog.csdn.net/wltg2001/archive/2008/04/17/2300258.aspx
看这个
lizhigang34 2009-05-07
  • 打赏
  • 举报
回复
得到BMP数据后,可以用下面这个函数获得HBITMAP

The SetDIBits function sets the pixels in a bitmap using the color data found in the specified DIB .

int SetDIBits(
HDC hdc, // handle to DC
HBITMAP hbmp, // handle to bitmap
UINT uStartScan, // starting scan line
UINT cScanLines, // number of scan lines
CONST VOID *lpvBits, // array of bitmap bits
CONST BITMAPINFO *lpbmi, // bitmap data
UINT fuColorUse // type of color indexes to use
);
devil521 2009-05-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 aa3000 的回复:]
void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi,
                  HBITMAP hBMP, HDC hDC)
{
    HANDLE hf;                // file handle
    BITMAPFILEHEADER hdr;      // bitmap file-header
    PBITMAPINFOHEADER pbih;    // bitmap info-header
    LPBYTE lpBits;              // memory pointer
    DWORD dwTotal;              // total count of bytes
    DW…
[/Quote]
我该怎么用这个函数呢??怎么获取图像的句柄呢??
aa3000 2009-05-07
  • 打赏
  • 举报
回复
void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi,
HBITMAP hBMP, HDC hDC)
{
HANDLE hf; // file handle
BITMAPFILEHEADER hdr; // bitmap file-header
PBITMAPINFOHEADER pbih; // bitmap info-header
LPBYTE lpBits; // memory pointer
DWORD dwTotal; // total count of bytes
DWORD cb; // incremental count of bytes
BYTE *hp; // byte pointer
DWORD dwTmp;

pbih = (PBITMAPINFOHEADER) pbi;
lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);

if (!lpBits)
errhandler("GlobalAlloc", hwnd);

// Retrieve the color table (RGBQUAD array) and the bits
// (array of palette indices) from the DIB.
if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi,
DIB_RGB_COLORS))
{
errhandler("GetDIBits", hwnd);
}

// Create the .BMP file.
hf = CreateFile(pszFile,
GENERIC_READ | GENERIC_WRITE,
(DWORD) 0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hf == INVALID_HANDLE_VALUE)
errhandler("CreateFile", hwnd);
hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file.
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;

// Compute the offset to the array of color indices.
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof (RGBQUAD);

// Copy the BITMAPFILEHEADER into the .BMP file.
if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),
(LPDWORD) &dwTmp, NULL))
{
errhandler("WriteFile", hwnd);
}

// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
+ pbih->biClrUsed * sizeof (RGBQUAD),
(LPDWORD) &dwTmp, ( NULL))
errhandler("WriteFile", hwnd);

// Copy the array of color indices into the .BMP file.
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))
errhandler("WriteFile", hwnd);

// Close the .BMP file.
if (!CloseHandle(hf))
errhandler("CloseHandle", hwnd);

// Free memory.
GlobalFree((HGLOBAL)lpBits);
}
aa3000 2009-05-07
  • 打赏
  • 举报
回复
void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi,
HBITMAP hBMP, HDC hDC)
{
HANDLE hf; // file handle
BITMAPFILEHEADER hdr; // bitmap file-header
PBITMAPINFOHEADER pbih; // bitmap info-header
LPBYTE lpBits; // memory pointer
DWORD dwTotal; // total count of bytes
DWORD cb; // incremental count of bytes
BYTE *hp; // byte pointer
DWORD dwTmp;

pbih = (PBITMAPINFOHEADER) pbi;
lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);

if (!lpBits)
errhandler("GlobalAlloc", hwnd);

// Retrieve the color table (RGBQUAD array) and the bits
// (array of palette indices) from the DIB.
if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi,
DIB_RGB_COLORS))
{
errhandler("GetDIBits", hwnd);
}

// Create the .BMP file.
hf = CreateFile(pszFile,
GENERIC_READ | GENERIC_WRITE,
(DWORD) 0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hf == INVALID_HANDLE_VALUE)
errhandler("CreateFile", hwnd);
hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file.
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;

// Compute the offset to the array of color indices.
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof (RGBQUAD);

// Copy the BITMAPFILEHEADER into the .BMP file.
if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),
(LPDWORD) &dwTmp, NULL))
{
errhandler("WriteFile", hwnd);
}

// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
+ pbih->biClrUsed * sizeof (RGBQUAD),
(LPDWORD) &dwTmp, ( NULL))
errhandler("WriteFile", hwnd);

// Copy the array of color indices into the .BMP file.
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))
errhandler("WriteFile", hwnd);

// Close the .BMP file.
if (!CloseHandle(hf))
errhandler("CloseHandle", hwnd);

// Free memory.
GlobalFree((HGLOBAL)lpBits);
}

19,469

社区成员

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

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