Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

capture d'ecran en C

3 réponses
Avatar
SplitbErzing
Bonjour,
Dans le cadre d'un projet avec mon école je souhaiterais effectuer une
capture d'ecran automatique au debut de mon programme en C, pour ensuite
utiliser le .bmp comme fond d'ecran avec la librairie graphique allegro.

J'ai entendu dire qu'il était possible de faire ça avec API windows, mais je
ne connais pas du tout ce language!

Pourriez-vous donc me donner la solution svp.?

3 réponses

Avatar
Christian ASTOR
SplitbErzing wrote:
Quelqu'un pourrait me donner le code complet en C svp...
Je ne connait pas du tout MSDN...



Mais il suffit de cliquer (!)
sur "Capturing an Image" et "Storing an Image"

ou une fonction plus simple en GDI+
(GetEncoderClsid() que j'utilise étant copié de MSDN),
qu'on appelle par exemple par :
WindowToJpeg(GetDesktopWindow(), L"test.jpg");
=>


BOOL WindowToJpeg(HWND hWnd, LPWSTR pszName)
{
HDC hDCMem = NULL, hDC;
RECT rect = { 0 };
BITMAPINFOHEADER bi = { 0 };
HBITMAP hBitmap = NULL, hBitmapOld = NULL;
PBYTE pBytes = NULL;
CLSID jpgClsid;
Bitmap *pBitmap = NULL;
HRESULT hr = E_FAIL;
int nRet = 0;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
if (GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) == Ok)
{
hDC = GetWindowDC(hWnd);
hDCMem = CreateCompatibleDC(hDC);
if (hDC && hDCMem)
{
GetWindowRect(hWnd, &rect);
bi.biSize = sizeof(bi);
bi.biWidth = rect.right - rect.left;
bi.biHeight = rect.bottom - rect.top;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
hBitmap = CreateDIBSection(hDCMem, (BITMAPINFO*)&bi,
DIB_RGB_COLORS, (void**)&pBytes, NULL, NULL);
if (hBitmap)
{
hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap);
if (BitBlt(hDCMem, 0, 0, bi.biWidth, bi.biHeight, hDC, 0, 0, SRCCOPY))
{
pBitmap = new Bitmap((BITMAPINFO*)&bi, (void*)pBytes);
if (pBitmap)
{
// GetEncoderClsid() vient de MSDN
if (SUCCEEDED(GetEncoderClsid(L"image/jpeg", &jpgClsid)))
{
CLSID encoderClsid;
EncoderParameters encoderParameters;
hr = GetEncoderClsid(L"image/jpeg", &encoderClsid);
if (SUCCEEDED(hr))
{
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
long quality = 100;
encoderParameters.Parameter[0].Value = &quality;
}
hr = pBitmap->Save(pszName, &jpgClsid, &encoderParameters);
if (SUCCEEDED(hr))
nRet = 1;
}
delete pBitmap;
}
}
SelectObject(hDCMem, hBitmapOld);
DeleteObject(hBitmap);
}
DeleteDC(hDCMem);
ReleaseDC(hWnd, hDC);
}
GdiplusShutdown(gdiplusToken);
}
return nRet;
}
Avatar
Christian ASTOR
On 12 mar, 15:48, "SplitbErzing" wrote:
Bonjour,
Dans le cadre d'un projet avec mon école je souhaiterais effectuer une
capture d'ecran automatique au debut de mon programme en C, pour ensuite
utiliser le .bmp comme fond d'ecran avec la librairie graphique allegro.

J'ai entendu dire qu'il était possible de faire ça avec API windows, mais je
ne connais pas du tout ce language!



MSDN : "Capturing an Image" et "Storing an Image"
http://msdn.microsoft.com/en-us/library/dd145170(VS.85).aspx
ou plus simple avec GDI+ (Bitmap::Save())
Avatar
SplitbErzing
Quelqu'un pourrait me donner le code complet en C svp...
Je ne connait pas du tout MSDN...

Merci d'avance.

"Christian ASTOR" a écrit dans le message de
news:
On 12 mar, 15:48, "SplitbErzing" wrote:
Bonjour,
Dans le cadre d'un projet avec mon école je souhaiterais effectuer une
capture d'ecran automatique au debut de mon programme en C, pour ensuite
utiliser le .bmp comme fond d'ecran avec la librairie graphique allegro.

J'ai entendu dire qu'il était possible de faire ça avec API windows, mais
je
ne connais pas du tout ce language!



MSDN : "Capturing an Image" et "Storing an Image"
http://msdn.microsoft.com/en-us/library/dd145170(VS.85).aspx
ou plus simple avec GDI+ (Bitmap::Save())