各位大虾帮帮忙:
声明变量:
BITMAPFILEHEADER * pbmfh ;
BITMAPINFO * pbmi ;
BYTE * pBits ;
WORD cxDib, cyDib ;
DWORD dwBytesWritten ;
HANDLE hFile ;
STCUTIMAGEINFO stInfo;
STNEWSIZE stNewSize; 利用 DibLoadImage ((PTSTR)AA) 得到pbmfh = DibLoadImage ((PTSTR)AA) ;其中AA为文件名,
pbmi = (BITMAPINFO *) (pbmfh + 1) ;
pBits = (BYTE *) pbmfh + pbmfh->bfOffBits ;
得到位图长和宽:
if (pbmi->bmiHeader.biSize == sizeof (BITMAPCOREHEADER))
{
cxDib = ((BITMAPCOREHEADER *) pbmi)->bcWidth ;
cyDib = ((BITMAPCOREHEADER *) pbmi)->bcHeight ;
}
else
{
cxDib = (WORD)pbmi->bmiHeader.biWidth ;
cyDib = abs (pbmi->bmiHeader.biHeight) ;
}
然后通过两个函数裁减位图中的“黑边”,
GetCutImageArea((LPBYTE)pBits, &stInfo, &stNewSize);
CutImage((WORD)RR, (LPBYTE)pBits, &cxDib, &cyDib, &stNewSize);
(上面参数stInfo、stNewSize与位图无关)
从而改变了位图的pBits,输出的cxDib,cyDib为裁减后的长和宽。
现在的问题是想将裁减后的位图保存为文件名为BB的文件,请问各位大虾我应 该填哪些有关位图文件的信息(如文件头,文件大小、)
hFile = CreateFile (AB, GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;
if (hFile == INVALID_HANDLE_VALUE)
return ;
在写前我应做些什么
WriteFile(hFile, pBits, pbmfh->bfSize, &dwBytesWritten, NULL);
其中WriteFile里的参数可能有错!有哪位知道帮帮忙告诉我!全题100分,有UP的也给分!
声明变量:
BITMAPFILEHEADER * pbmfh ;
BITMAPINFO * pbmi ;
BYTE * pBits ;
WORD cxDib, cyDib ;
DWORD dwBytesWritten ;
HANDLE hFile ;
STCUTIMAGEINFO stInfo;
STNEWSIZE stNewSize; 利用 DibLoadImage ((PTSTR)AA) 得到pbmfh = DibLoadImage ((PTSTR)AA) ;其中AA为文件名,
pbmi = (BITMAPINFO *) (pbmfh + 1) ;
pBits = (BYTE *) pbmfh + pbmfh->bfOffBits ;
得到位图长和宽:
if (pbmi->bmiHeader.biSize == sizeof (BITMAPCOREHEADER))
{
cxDib = ((BITMAPCOREHEADER *) pbmi)->bcWidth ;
cyDib = ((BITMAPCOREHEADER *) pbmi)->bcHeight ;
}
else
{
cxDib = (WORD)pbmi->bmiHeader.biWidth ;
cyDib = abs (pbmi->bmiHeader.biHeight) ;
}
然后通过两个函数裁减位图中的“黑边”,
GetCutImageArea((LPBYTE)pBits, &stInfo, &stNewSize);
CutImage((WORD)RR, (LPBYTE)pBits, &cxDib, &cyDib, &stNewSize);
(上面参数stInfo、stNewSize与位图无关)
从而改变了位图的pBits,输出的cxDib,cyDib为裁减后的长和宽。
现在的问题是想将裁减后的位图保存为文件名为BB的文件,请问各位大虾我应 该填哪些有关位图文件的信息(如文件头,文件大小、)
hFile = CreateFile (AB, GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;
if (hFile == INVALID_HANDLE_VALUE)
return ;
在写前我应做些什么
WriteFile(hFile, pBits, pbmfh->bfSize, &dwBytesWritten, NULL);
其中WriteFile里的参数可能有错!有哪位知道帮帮忙告诉我!全题100分,有UP的也给分!
// Returns - TRUE on success
// szFile - Name of file to write to
// hDIB - Handle of the DIB
BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi; if (!hDIB)
return FALSE; CFile file;
if( !file.Open( szFile, 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) ); return TRUE;
}
// DDBToDIB - Creates a DIB from a DDB
// bitmap - Device dependent bitmap
// dwCompression - Type of compression - see BITMAPINFOHEADER
// pPal - Logical palette
HANDLE DDBToDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* pPal )
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
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); // Initialize the 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;
}