Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1556 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  Capture d'écran

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Capture d'écran

n°1151363
ayorosmage
Posté le 16-07-2005 à 18:20:26  profilanswer
 

Salut à tous, j'aimerais réaliser un programme permettant de prendre un screenshot et ensuite d'afficher le code RVB du screenshot pris. Seulement je ne sais pas quel est la fonction qu'il faut utiliser pour prendre une capture d'écran...

mood
Publicité
Posté le 16-07-2005 à 18:20:26  profilanswer
 

n°1151680
redbug
Posté le 17-07-2005 à 10:29:55  profilanswer
 

Code :
  1. BITMAPINFO bmInfo;
  2. HBITMAP hBitmap;
  3. BYTE *bits;
  4. HDC hdcMem;
  5. HDC wndHDC;
  6. wndHDC = GetDC(NULL);
  7. hdcMem = CreateCompatibleDC(wndHDC);
  8. hBitmap = CreateCompatibleBitmap(wndHDC, 800, 600);
  9. SelectObject(hdcMem, hBitmap);
  10. StretchBlt(hdcMem, 0, 0, 800, 600, wndHDC, 0, 0, 1280, 1024, SRCCOPY);


En gros  :)  
Pour la taille de l'ecran, je suppose que tu trouveras facilement.
La fonction principale, c'est GetDC(NULL)
 
A propos, pourquoi veux-tu avoir le code rvb du screenshot ?


Message édité par redbug le 17-07-2005 à 10:33:47

---------------
Kyuran's World: Karaoke pour pocket pc, outil de synchro pour sunbird, ...  
n°1151727
ayorosmage
Posté le 17-07-2005 à 13:11:57  profilanswer
 

Il me faut le code rvb de l'écran afin de détecter si certaines choses sont affichés au moment ou je prends le screenshot ou non.
 
Au fait, avec le code que tu m'as indiqué ci-dessus, ou se situe le "codage" du bitmap ?

n°1152047
redbug
Posté le 17-07-2005 à 22:05:00  profilanswer
 

J'ai trouvé ceci sur les newsgroups
 

Code :
  1. #include<windows.h>
  2. // variables
  3. static HWND main_window = 0; // main window
  4. static HWND capture = 0;     // window we want to capture
  5. static HBITMAP bitmap = 0;   // dibsection
  6. static BYTE* memory = 0;     // dibsection memory
  7. // functions
  8. LRESULT CALLBACK MainWndProc(HWND window, UINT message, WPARAM wparam,
  9. LPARAM lparam);
  10. HBITMAP CreateDibsection24(HDC winDC, size_t dx, size_t dy, BYTE** memory);
  11. BOOL CaptureWindow(void);
  12. void OnIdle(void);
  13. int WINAPI WinMain(HINSTANCE instance, HINSTANCE, char*, int)
  14. {
  15. WNDCLASS wc;
  16. wc.style         = CS_DBLCLKS;
  17. wc.lpfnWndProc   = MainWndProc;
  18. wc.cbClsExtra    = 0;
  19. wc.cbWndExtra    = 0;
  20. wc.hInstance     = instance;
  21. wc.hIcon         = 0;
  22. wc.hCursor       = LoadCursor(0, IDC_ARROW);
  23. wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  24. wc.lpszMenuName  = 0;
  25. wc.lpszClassName = "Fundamental Window";
  26. if(!RegisterClass(&wc))
  27.     return MessageBox(0, "Could not register main window.", "Error",
  28. MB_ICONSTOP);
  29. int x = CW_USEDEFAULT; int w = CW_USEDEFAULT;
  30. int y = CW_USEDEFAULT; int h = CW_USEDEFAULT;
  31. HWND window = CreateWindow("Fundamental Window", "Fundamental Window",
  32. WS_OVERLAPPEDWINDOW, x, y, w, h, 0, 0, instance, 0);
  33. if(!window) return MessageBox(0, "Could not create main window.", "Error",
  34. MB_ICONSTOP);
  35. ShowWindow(window, SW_SHOW);
  36. UpdateWindow(window);
  37. MSG msg;
  38. for(;;) {
  39.      if(PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE)) {
  40.         if(GetMessage(&msg, 0, 0, 0)) {
  41.            TranslateMessage(&msg);
  42.            DispatchMessage(&msg);
  43.           }
  44.         else break;
  45.        }
  46.      else
  47.         OnIdle();
  48.     } return (int)msg.wParam;
  49. }
  50. LRESULT CALLBACK MainWndProc(HWND window, UINT message, WPARAM wparam,
  51. LPARAM lparam)
  52. {
  53. switch(message) {
  54.    case(WM_CREATE) : {
  55.         // create bitmap
  56.         HDC winDC = GetDC(window);
  57.         bitmap = CreateDibsection24(winDC, 800, 600, &memory);
  58.         ReleaseDC(window, winDC);
  59.         // set window variables
  60.         main_window = window;
  61.         capture = GetDesktopWindow();
  62.         // do default
  63.         return DefWindowProc(window, message, wparam, lparam);
  64.        }
  65.    case(WM_DESTROY) : {
  66.         // destroy bitmap
  67.         DeleteObject(bitmap);
  68.         bitmap = 0;
  69.         // erase window variables
  70.         main_window = 0;
  71.         capture = 0;
  72.         // quit application
  73.         PostQuitMessage(0);
  74.         break;
  75.        }
  76.   } return DefWindowProc(window, message, wparam, lparam);
  77. }
  78. HBITMAP CreateDibsection24(HDC winDC, size_t dx, size_t dy, BYTE** memory)
  79. {
  80. // bitmap information
  81. BITMAPINFOHEADER infoHeader;
  82. infoHeader.biSize          = sizeof(infoHeader);
  83. infoHeader.biWidth         = (LONG)dx;
  84. infoHeader.biHeight        = (LONG)dy;
  85. infoHeader.biPlanes        = 1;
  86. infoHeader.biBitCount      = 24;
  87. infoHeader.biCompression   = BI_RGB;
  88. infoHeader.biSizeImage     = 0;
  89. infoHeader.biXPelsPerMeter = 0;
  90. infoHeader.biYPelsPerMeter = 0;
  91. infoHeader.biClrUsed       = 0;
  92. infoHeader.biClrImportant  = 0;
  93. // dibsection information
  94. BITMAPINFO info;
  95. info.bmiHeader = infoHeader;
  96. return CreateDIBSection(winDC, &info, DIB_RGB_COLORS, (void**)memory, 0,
  97. 0);
  98. }
  99. BOOL CaptureWindow(void)
  100. {
  101. // bitmap must be a dibsection
  102. DIBSECTION ds;
  103. if(!GetObject(bitmap, sizeof(ds), &ds)) return FALSE;
  104. int bitmap_dx = ds.dsBmih.biWidth;
  105. int bitmap_dy = ds.dsBmih.biHeight;
  106. // get window dimensions
  107. RECT rect;
  108. GetWindowRect(capture, &rect);
  109. int window_dx = rect.right - rect.left;
  110. int window_dy = rect.bottom - rect.top;
  111. // get window context
  112. HDC winDC = GetWindowDC(capture);
  113. if(!winDC) return FALSE;
  114. // create a memory context to select the dibsection into
  115. HDC memDC = CreateCompatibleDC(winDC);
  116. if(!memDC) { ReleaseDC(capture, winDC); return FALSE; }
  117. // fill dibsection with black
  118. // then copy the contents of the winDC into the dibsection
  119. HBRUSH oldbrush = (HBRUSH)SelectObject(memDC, GetStockObject
  120. (BLACK_BRUSH));
  121. HBITMAP oldbitmap = (HBITMAP)SelectObject(memDC, bitmap);
  122. PatBlt(memDC, 0, 0, bitmap_dx, bitmap_dy, PATCOPY);
  123. BitBlt(memDC, 0, 0, window_dx, window_dy, winDC, 0, 0, SRCCOPY);
  124. // cleanup contexts
  125. SelectObject(memDC, oldbrush);
  126. SelectObject(memDC, oldbitmap);
  127. DeleteDC(memDC);
  128. ReleaseDC(capture, winDC);
  129. return TRUE;
  130. }
  131. void OnIdle(void)
  132. {
  133. // capture the desktop window  
  134. if(!bitmap) return;
  135. else CaptureWindow();
  136. // the pixel position to extract the color for
  137. int x = 41;
  138. int y = 32;
  139. // use GetPixel
  140. HDC winDC = GetWindowDC(capture);
  141. COLORREF c1 = GetPixel(winDC, x, y);
  142. ReleaseDC(capture, winDC);
  143. // use dibsection memory
  144. DIBSECTION ds;
  145. GetObject(bitmap, sizeof(ds), &ds);
  146. int pitch_bytes = (((24*ds.dsBmih.biWidth + 31) & (~31))/8);
  147. y = (ds.dsBmih.biHeight - 1) - y;
  148. int index_b = (pitch_bytes*y + 3*x);
  149. int index_g = index_b + 1;
  150. int index_r = index_b + 2;
  151. COLORREF c2 = RGB(memory[index_r], memory[index_g], memory[index_b]);
  152. // print colors to string
  153. char buffer[256];
  154. wsprintf(buffer, "cGetPixel = (%d,%d,%d) and cDibsection = (%d,%d,%d).",
  155.   GetRValue(c1), GetGValue(c1), GetBValue(c1),
  156.   GetRValue(c2), GetGValue(c2), GetBValue(c2));
  157. SetWindowText(main_window, buffer);
  158. }


 
La fonction OnIdle devrait te convenir :)


---------------
Kyuran's World: Karaoke pour pocket pc, outil de synchro pour sunbird, ...  
n°1152863
ayorosmage
Posté le 18-07-2005 à 18:55:35  profilanswer
 

J'ai compilé le code avec dev-cpp, une fenêtre apparaît mais rien de plus... La prog windows c'est pas trop mon truc, je voudrais juste pouvoir prendre un screen et enregistrer la capture d'écran en bitmap :d


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  Capture d'écran

 

Sujets relatifs
réaliser un fond d'écran animéCreer un DIV prenant la totalité de l'ecran avec une marge
capture d'écran d'un siteCapture d'écran en VBA
[C++] Capture d'ecran et sauvegarde en JpegComment faire une capture d'écran d'une url donnée
capture d'écran d'un vidéo ?capture d'ecran
[Turbo Pascal] Capture d'ecran en mode graphiqueCapture d'ecran VC++
Plus de sujets relatifs à : Capture d'écran


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR