我现在有位图数据在CBitmap中
欲将其连同其它数据一同保存到一个文件中,
用的方法是序列化方法
打开后再创建CBitmap
请问如何实现。解决后将给200分
请回答两次
欲将其连同其它数据一同保存到一个文件中,
用的方法是序列化方法
打开后再创建CBitmap
请问如何实现。解决后将给200分
请回答两次
解决方案 »
- vc++实现的一个滑动窗体,collapeswnd,有需要的可以收藏
- 微软的弱智设计(文件拷贝)
- CListBox怎么设定高度
- 输入法打字的疑难问题。
- Curllib 問題~(附程式和結果)
- 8位bmp 转avi 问题??
- 菜鸟初学vc,我在窗体上放了两个radio,一个标题是‘男’,另一个标题是‘女’,还有一个button,怎么实现当用户选种标题是‘男’的那个rad
- 吐血大送分
- 请教《windows程序设计》里的一个问题
- switch 后面不可以是CString?
- 新买了一块硬盘,可是98下两块盘的盘符交错排列。有什么办法可以先老盘的:c,d,e,f,再新盘的:c,d,e,f(肯定结账)
- 我想做两个程序,可以互相传递文件,用什么类可以做到呢?谢谢大家!
{
HANDLE hDIB;
if(!m_hBitmap)
return FALSE;
hDIB=DDBToDIB(m_Bitmap,BI_RGB,NULL); //转化DDB2DIB if( hDIB == NULL )
return FALSE;
// Write it to file
BITMAPFILEHEADER hdr; //位图文件头
LPBITMAPINFOHEADER lpbi; //位图信息头 CFile file; if( !file.Open( lpszPathName, CFile::modeWrite|CFile::modeCreate) )
return FALSE; lpbi = (LPBITMAPINFOHEADER)hDIB; int nColors = 1 << lpbi->biBitCount; // Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0; //保留字
hdr.bfReserved2 = 0; //保留字
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD)); // Write the file header
file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) );
file.Flush();
file.Close(); // Free the memory allocated by DDBToDIB for the DIB
GlobalFree( hDIB );
return TRUE;
}HANDLE CWrapBitmap::DDBToDIB(CBitmap &bitmap, DWORD dwCompression, CPalette *pPal)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE handle;
HANDLE hDIB;
HDC hDC;
HPALETTE hPal;
ASSERT( bitmap.GetSafeHandle() ); // The function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
return NULL; // If a palette has not been supplied use defaul palette
hPal = (HPALETTE) pPal->GetSafeHandle();
if (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE); // Get bitmap information
bitmap.GetObject(sizeof(bm),(LPSTR)&bm); //>> 填充位图信息头 bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
//<< // Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD); // We need a device context to get the DIB from
hDC = GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC); // Allocate enough memory to hold bitmapinfoheader and color table
hDIB = GlobalAlloc(GMEM_FIXED,dwLen); if (!hDIB){
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
} lpbi = (LPBITMAPINFOHEADER)hDIB; *lpbi = bi; // Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS); bi = *lpbi; // If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight; // If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
} // Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
else{
GlobalFree(hDIB); // Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
} // Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB; // FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // Use RGB for color table if( !bGotBits )
{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
} SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return hDIB;
}
{
HANDLE hDIB;
if(!m_hBitmap)
return FALSE;
hDIB=DDBToDIB(m_Bitmap,BI_RGB,NULL); //转化DDB2DIB if( hDIB == NULL )
return FALSE;
// Write it to file
BITMAPFILEHEADER hdr; //位图文件头
LPBITMAPINFOHEADER lpbi; //位图信息头 CFile file; if( !file.Open( lpszPathName, CFile::modeWrite|CFile::modeCreate) )
return FALSE; lpbi = (LPBITMAPINFOHEADER)hDIB; int nColors = 1 << lpbi->biBitCount; // Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0; //保留字
hdr.bfReserved2 = 0; //保留字
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD)); // Write the file header
file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) );
file.Flush();
file.Close(); // Free the memory allocated by DDBToDIB for the DIB
GlobalFree( hDIB );
return TRUE;
}HANDLE CWrapBitmap::DDBToDIB(CBitmap &bitmap, DWORD dwCompression, CPalette *pPal)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE handle;
HANDLE hDIB;
HDC hDC;
HPALETTE hPal;
ASSERT( bitmap.GetSafeHandle() ); // The function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
return NULL; // If a palette has not been supplied use defaul palette
hPal = (HPALETTE) pPal->GetSafeHandle();
if (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE); // Get bitmap information
bitmap.GetObject(sizeof(bm),(LPSTR)&bm); //>> 填充位图信息头 bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
//<< // Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD); // We need a device context to get the DIB from
hDC = GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC); // Allocate enough memory to hold bitmapinfoheader and color table
hDIB = GlobalAlloc(GMEM_FIXED,dwLen); if (!hDIB){
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
} lpbi = (LPBITMAPINFOHEADER)hDIB; *lpbi = bi; // Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS); bi = *lpbi; // If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight; // If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
} // Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
else{
GlobalFree(hDIB); // Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
} // Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB; // FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // Use RGB for color table if( !bGotBits )
{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
} SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return hDIB;
}
{
HANDLE hDIB;
if(!m_hBitmap)
return FALSE;
hDIB=DDBToDIB(m_Bitmap,BI_RGB,NULL); //转化DDB2DIB if( hDIB == NULL )
return FALSE;
// Write it to file
BITMAPFILEHEADER hdr; //位图文件头
LPBITMAPINFOHEADER lpbi; //位图信息头 CFile file; if( !file.Open( lpszPathName, CFile::modeWrite|CFile::modeCreate) )
return FALSE; lpbi = (LPBITMAPINFOHEADER)hDIB; int nColors = 1 << lpbi->biBitCount; // Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0; //保留字
hdr.bfReserved2 = 0; //保留字
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD)); // Write the file header
file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) );
file.Flush();
file.Close(); // Free the memory allocated by DDBToDIB for the DIB
GlobalFree( hDIB );
return TRUE;
}HANDLE CWrapBitmap::DDBToDIB(CBitmap &bitmap, DWORD dwCompression, CPalette *pPal)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE handle;
HANDLE hDIB;
HDC hDC;
HPALETTE hPal;
ASSERT( bitmap.GetSafeHandle() ); // The function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
return NULL; // If a palette has not been supplied use defaul palette
hPal = (HPALETTE) pPal->GetSafeHandle();
if (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE); // Get bitmap information
bitmap.GetObject(sizeof(bm),(LPSTR)&bm); //>> 填充位图信息头 bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
//<< // Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD); // We need a device context to get the DIB from
hDC = GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC); // Allocate enough memory to hold bitmapinfoheader and color table
hDIB = GlobalAlloc(GMEM_FIXED,dwLen); if (!hDIB){
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
} lpbi = (LPBITMAPINFOHEADER)hDIB; *lpbi = bi; // Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS); bi = *lpbi; // If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight; // If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
} // Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
else{
GlobalFree(hDIB); // Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
} // Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB; // FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // Use RGB for color table if( !bGotBits )
{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
} SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return hDIB;
}