CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    May 2009
    Posts
    140

    Question 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);
    
    }

  2. #2
    Join Date
    Dec 2008
    Posts
    114

    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)

  3. #3
    Join Date
    May 2009
    Posts
    140

    Re: GDI+ screenshot save to JPG

    i searched thru that group with no results =\
    can you help me with me code?

  4. #4
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    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" )
    Last edited by CBasicNet; May 13th, 2009 at 12:51 AM. Reason: Adding the gdiplus.lib linkage

  5. #5
    Join Date
    May 2009
    Posts
    140

    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= "&#37;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);
    Last edited by Owyn; May 13th, 2009 at 06:22 AM.

  6. #6
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    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);

  7. #7
    Join Date
    May 2009
    Posts
    140

    Re: GDI+ screenshot save to JPG

    doesn't work, says impossible to convert param 1 from 'wchar_t[200] to 'LPSTR' =\

  8. #8
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    Re: GDI+ screenshot save to JPG

    you are using sprintf or wsprintf? GDI+ is using wide characters strings, not ASCII character strings.

  9. #9
    Join Date
    May 2009
    Posts
    140

    Re: GDI+ screenshot save to JPG

    i'm useing code you gave me above, so wsprintf

  10. #10
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    Re: GDI+ screenshot save to JPG

    Can you post the line which got this compilation error? The code is working fine on my side.

  11. #11
    Join Date
    May 2009
    Posts
    140

    Re: GDI+ screenshot save to JPG

    Code:
    wsprintf(filename,L"screen&#37;02d:%02d:%02d h.jpeg",SysTime.wHour,SysTime.wMinute,SysTime.wSecond);

  12. #12
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    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);

  13. #13
    Join Date
    May 2009
    Posts
    140

    Re: GDI+ screenshot save to JPG

    Quote Originally Posted by CBasicNet View Post
    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 =\

    Error 149 error C2664: wsprintfA: impossible to convert parameter 2 from 'const wchar_t [28]' to 'LPCSTR'

  14. #14
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    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);

  15. #15
    Join Date
    May 2009
    Posts
    140

    Re: GDI+ screenshot save to JPG

    Quote Originally Posted by CBasicNet View Post
    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);
    ...
    Error 149 error C2664: wsprintfW: impossible to convert parameter 1 from 'TCHAR [200]' to 'LPWSTR'

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured