GDI+ screenshot save to JPG
hi, i have a code to screen and save screen as png with GDI+, how do i change it to save as jpg?
Code:
void gdiscreen()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
HDC scrdc, memdc;
HBITMAP membit;
// Получаем HDC рабочего стола
// Параметр HWND для рабочего стола всегда равен нулю.
scrdc = GetDC(0);
// Определяем разрешение экрана
int Height, Width;
Height = GetSystemMetrics(SM_CYSCREEN);
Width = GetSystemMetrics(SM_CXSCREEN);
// Создаем новый DC, идентичный десктоповскому и битмап размером с экран.
memdc = CreateCompatibleDC(scrdc);
membit = CreateCompatibleBitmap(scrdc, Width, Height);
SelectObject(memdc, membit);
// Улыбаемся... Снято!
BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
HBITMAP hBitmap;
hBitmap =(HBITMAP) SelectObject(memdc, membit);
Gdiplus::Bitmap bitmap(hBitmap, NULL);
bitmap.Save(L"c:\\screen.png", &png);
DeleteObject(hBitmap);
}
Re: GDI+ screenshot save to JPG
This code is wrong (memory leaks, etc)
The proper code had been posted on Win32 group
(http://groups.google.com/group/comp....topics?lnk=srg)
Re: GDI+ screenshot save to JPG
i searched thru that group with no results =\
can you help me with me code?
Re: GDI+ screenshot save to JPG
Here is the modified code to save to jpeg with the leaks fixed.
Code:
#include <GdiPlus.h>
void gdiscreen()
{
using namespace Gdiplus;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
{
HDC scrdc, memdc;
HBITMAP membit;
scrdc = ::GetDC(0);
int Height = GetSystemMetrics(SM_CYSCREEN);
int Width = GetSystemMetrics(SM_CXSCREEN);
memdc = CreateCompatibleDC(scrdc);
membit = CreateCompatibleBitmap(scrdc, Width, Height);
HBITMAP hOldBitmap =(HBITMAP) SelectObject(memdc, membit);
BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
Gdiplus::Bitmap bitmap(membit, NULL);
CLSID clsid;
GetEncoderClsid(L"image/jpeg", &clsid);
bitmap.Save(L"D:\\screen.jpeg", &clsid);
SelectObject(memdc, hOldBitmap);
DeleteObject(memdc);
DeleteObject(membit);
::ReleaseDC(0,scrdc);
}
GdiplusShutdown(gdiplusToken);
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
using namespace Gdiplus;
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return 0;
}
To compile and link the code, you need to add the gdiplus.lib to Project Properties->Linker->Input->Additional Dependencies. Or you can put the below pragma in source code.
Code:
#pragma comment( lib, "gdiplus" )
Re: GDI+ screenshot save to JPG
thank you very much =)
one more question, how do i format filename? i have little problems with it, i want it to be
Code:
SYSTEMTIME SysTime;
GetLocalTime(&SysTime);
char *filename= "%02d:%02d:%02d h";
sprintf(filename,"screen%02d:%02d:%02d h.jpeg",timestring,SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
but can't figure out how to use it in
Code:
bitmap.Save(L"D:\\screen.jpeg", &clsid);
Re: GDI+ screenshot save to JPG
Code:
SYSTEMTIME SysTime;
GetLocalTime(&SysTime);
wchar_t filename[200];
memset(filename,0,sizeof(filename));
wsprintf(filename,L"screen%02d:%02d:%02d h.jpeg",SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
Re: GDI+ screenshot save to JPG
doesn't work, says impossible to convert param 1 from 'wchar_t[200] to 'LPSTR' =\
Re: GDI+ screenshot save to JPG
you are using sprintf or wsprintf? GDI+ is using wide characters strings, not ASCII character strings.
Re: GDI+ screenshot save to JPG
i'm useing code you gave me above, so wsprintf
Re: GDI+ screenshot save to JPG
Can you post the line which got this compilation error? The code is working fine on my side.
Re: GDI+ screenshot save to JPG
Code:
wsprintf(filename,L"screen%02d:%02d:%02d h.jpeg",SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
Re: GDI+ screenshot save to JPG
Strange. What compiler are you using? Meanwhile you can try the below.
Code:
SYSTEMTIME SysTime;
GetLocalTime(&SysTime);
TCHAR filename[200];
memset(filename,0,sizeof(filename));
wsprintf(filename,L"screen%02d:%02d:%02d h.jpeg",SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
Re: GDI+ screenshot save to JPG
Quote:
Originally Posted by
CBasicNet
Strange. What compiler are you using? Meanwhile you can try the below.
Code:
SYSTEMTIME SysTime;
GetLocalTime(&SysTime);
TCHAR filename[200];
memset(filename,0,sizeof(filename));
wsprintf(filename,L"screen%02d:%02d:%02d h.jpeg",SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
visual studio 2008
this code gives same result on wsprintf line =\
Quote:
Error 149 error C2664: wsprintfA: impossible to convert parameter 2 from 'const wchar_t [28]' to 'LPCSTR'
Re: GDI+ screenshot save to JPG
Use wsprintfW then.
Code:
SYSTEMTIME SysTime;
GetLocalTime(&SysTime);
TCHAR filename[200];
memset(filename,0,sizeof(filename));
wsprintfW(filename,L"screen%02d:%02d:%02d h.jpeg",SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
Re: GDI+ screenshot save to JPG
Quote:
Originally Posted by
CBasicNet
Use wsprintfW then.
Code:
SYSTEMTIME SysTime;
GetLocalTime(&SysTime);
TCHAR filename[200];
memset(filename,0,sizeof(filename));
wsprintfW(filename,L"screen%02d:%02d:%02d h.jpeg",SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
...
Quote:
Error 149 error C2664: wsprintfW: impossible to convert parameter 1 from 'TCHAR [200]' to 'LPWSTR'