Hwnd to bmp file (win32)

Description

Variation sur le theme de la precedente source.
Fonction qui prend une photo de fenetre en BMP.
HwndToBmpFile(HWND hwnd, char *pszflname);
Dans l'exemple fourni la fenetre passe son hwnd.
On peut faire la photo d'une autre fenetre mais il faut la passer au 1er plan.
Tout dans le zip.

Source / Exemple :


int __stdcall HwndToBmpFile(HWND hwnd, char *pszflname)
{
  HDC memdc, hdc;
  HANDLE hfl;
  DWORD dwBytes, dwNumColors;
  void *pBits;
  HBITMAP hbmp;
  BITMAPFILEHEADER fileheader;
  BITMAPINFOHEADER infoheader;
  RGBQUAD colors[256];
  BITMAPINFO bmpinfo;
  HGDIOBJ hret;
  RECT rct;
  hdc = GetWindowDC(hwnd);
  if(!hdc) return 0;
  GetWindowRect(hwnd, &rct);
  rct.bottom -= rct.top;
  rct.right -= rct.left;
  rct.top = GetDeviceCaps(hdc, BITSPIXEL);
  if(rct.top <= 8) dwNumColors = 256;
  else dwNumColors = 0;
  if(!(memdc = CreateCompatibleDC(hdc))) goto relHwndDc;
  bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  bmpinfo.bmiHeader.biWidth = rct.right;
  bmpinfo.bmiHeader.biHeight = rct.bottom;
  bmpinfo.bmiHeader.biPlanes = 1;
  bmpinfo.bmiHeader.biBitCount = (WORD) rct.top;
  bmpinfo.bmiHeader.biCompression = BI_RGB;
  bmpinfo.bmiHeader.biSizeImage = 0;
  bmpinfo.bmiHeader.biXPelsPerMeter = 0;
  bmpinfo.bmiHeader.biYPelsPerMeter = 0;
  bmpinfo.bmiHeader.biClrUsed = dwNumColors;
  bmpinfo.bmiHeader.biClrImportant = dwNumColors;
  hbmp = CreateDIBSection(hdc, &bmpinfo, DIB_PAL_COLORS, &pBits, NULL, 0);
  if(!hbmp) goto errato;
  hret = SelectObject(memdc, hbmp);
  if(!hret || (hret == HGDI_ERROR)) goto errato;
  if(!BitBlt(memdc, 0, 0, rct.right, rct.bottom, hdc, 0, 0, SRCCOPY)) goto errato;
  if(dwNumColors) dwNumColors = GetDIBColorTable(memdc, 0, dwNumColors, colors);
  fileheader.bfType = 0x4D42;
  rct.left = dwNumColors * sizeof(RGBQUAD);
  fileheader.bfSize = ((rct.right * rct.bottom * rct.top) >> 3) + rct.left + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  fileheader.bfReserved1 = fileheader.bfReserved2 = 0;
  fileheader.bfOffBits = rct.left + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  infoheader.biSize = sizeof(BITMAPINFOHEADER);
  infoheader.biWidth = rct.right;
  infoheader.biHeight = rct.bottom;
  infoheader.biPlanes = 1;
  infoheader.biBitCount = (WORD) rct.top;
  infoheader.biCompression = BI_RGB;
  infoheader.biSizeImage = infoheader.biClrImportant = 0;
  infoheader.biXPelsPerMeter = infoheader.biYPelsPerMeter = 0;
  infoheader.biClrUsed = dwNumColors;
  hfl = CreateFile(pszflname,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
  if(hfl == INVALID_HANDLE_VALUE) {DeleteObject(hbmp); goto errato;}
  WriteFile(hfl, &fileheader, sizeof(BITMAPFILEHEADER), &dwBytes, 0);
  WriteFile(hfl, &infoheader, sizeof(BITMAPINFOHEADER), &dwBytes, 0);
  if(!dwNumColors) WriteFile(hfl, colors, rct.left, &dwBytes, 0);
  WriteFile(hfl, pBits, (rct.right * rct.bottom * rct.top) >> 3, &dwBytes, 0);
  CloseHandle(hfl);
  DeleteObject(hbmp);
  DeleteDC(memdc);
  return 1;
errato:
  DeleteDC(memdc);
relHwndDc:
  ReleaseDC(hwnd, hdc); return 0;
}

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.