بله!
در ضمن برای کاری که pswin اشاره کرد یه تابع خفن نوشتم ببینید:
کد:
PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp)
{
BITMAP bmp;
WORD cClrBits;
PBITMAPINFO pbmi;
if(!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
return NULL;
cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if(cClrBits == 1)
cClrBits = 1;
else if(cClrBits <= 4)
cClrBits = 4;
else if(cClrBits <= 8)
cClrBits = 8;
else if(cClrBits <= 16)
cClrBits = 16;
else if(cClrBits <= 24)
cClrBits = 24;
else cClrBits = 32;
if(cClrBits != 24)
pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPV5HEADER) + sizeof(RGBQUAD) * (1 << cClrBits));
else
pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPV5HEADER));
ZeroMemory(&pbmi, sizeof(pbmi));
if(cClrBits < 24)
pbmi->bmiHeader.biClrUsed = (1 << cClrBits);
pbmi->bmiHeader.biClrImportant = 0;
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biCompression = BI_RGB;//BI_JPEG;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
pbmi->bmiHeader.biSize = sizeof(BITMAPV5HEADER);
pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits + 31)& ~31) / 8 * pbmi->bmiHeader.biHeight;
return pbmi;
}
VOID CreateBMPFile(LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC)
{
DWORD cb;
BYTE *hp;
HANDLE hf;
DWORD dwTmp;
DWORD dwTotal;
LPBYTE lpBits;
PBITMAPV5HEADER pbih;
BITMAPFILEHEADER hdr;
pbih = (PBITMAPV5HEADER)pbi;
//pbih->bV5Compression = BI_JPEG;
lpBits = (LPBYTE)GlobalAlloc(GMEM_FIXED, pbih->bV5SizeImage);
if(!GetDIBits(hDC, hBMP, 0, (WORD)pbih->bV5Height, lpBits, pbi, DIB_RGB_COLORS))
{
CHAR szBuf[80];
DWORD dw = GetLastError();
sprintf(szBuf, "Save bitmap failed: GetLastError returned %u\n", dw);
MessageBox(NULL, szBuf, "Error", MB_OK);
}
hf = CreateFile(pszFile, GENERIC_READ | GENERIC_WRITE,
(DWORD)NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
if(hf == INVALID_HANDLE_VALUE)
Show("3 Error when create bitmap fiile");
hdr.bfType = 0x4d42;
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfSize = (DWORD)(sizeof(BITMAPFILEHEADER) +
pbih->bV5Size + pbih->bV5ClrUsed * sizeof(RGBQUAD) + pbih->bV5SizeImage);
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + pbih->bV5Size + pbih->bV5ClrUsed * sizeof(RGBQUAD);
if(!WriteFile(hf, (LPVOID)&hdr, sizeof(BITMAPFILEHEADER), (LPDWORD)&dwTmp, NULL))
Show("4 Error when create bitmap fiile");
if(!WriteFile(hf, (LPVOID)pbih, sizeof(BITMAPV5HEADER) +
pbih->bV5ClrUsed * sizeof(RGBQUAD), (DWORD*)&dwTmp, 0))
Show("5 Error when create bitmap fiile");
hp = lpBits;
dwTotal = cb = pbih->bV5SizeImage;
if(!WriteFile(hf, (LPSTR)hp, (int)cb, (LPDWORD)&dwTmp, NULL))
Show("6 if(!WriteFile(hf, (LPSTR)hp, (int) cb, (LPDWORD)&dwTmp, NULL)) ");
if(!CloseHandle(hf))
Show("7 CloseHandle");
GlobalFree((HGLOBAL)lpBits);
}
فرض کنید با کمک تابع فوق می خواهید یک کار خفن بکنید
مثلا از صفحه عکس بگیرید اینو داشته باشید!
کد:
HDC l_hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL),
l_hdcCompatible = CreateCompatibleDC(l_hdcScreen);
INT l_intWidth = GetDeviceCaps(l_hdcScreen, HORZRES),
l_intHeight = GetDeviceCaps(l_hdcScreen, VERTRES);
HBITMAP l_hbmScreen = CreateCompatibleBitmap(l_hdcScreen, l_intWidth, l_intHeight);
SelectObject(l_hdcCompatible, l_hbmScreen);
BitBlt(l_hdcCompatible, 0, 0, l_intWidth, l_intHeight, l_hdcScreen, 0, 0, SRCCOPY);
CHAR szBuf[80];
sprintf(szBuf, "E:\\Images\\_%u_.bmp", GetTickCount());
CreateBMPFile(szBuf, CreateBitmapInfoStruct(l_hbmScreen), l_hbmScreen, l_hdcCompatible);
سعید ت