1,105,169 Community Members

How to convert screenshot(bmp) to jpeg?

Member Avatar
pie555
Newbie Poster
4 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have some code I found that takes screenshots and saves them as a bitmap image. I'm trying to get it to save the screenshots as .jpg and I don't know how to. I don't want to use a large third party library if I don't have to, I would prefer something small and specialised. Any ideas? I would prefer the screenshot not be written to file then converted, but rather be converted "in ram".

Code at:http://pastebin.com/XB6U2yDY

Member Avatar
daviddoria
Posting Virtuoso
1,968 posts since Feb 2008
Reputation Points: 334 [?]
Q&As Helped to Solve: 204 [?]
Skill Endorsements: 8 [?]
Featured
 
0
 

ImageMagick has a c++ API: http://www.imagemagick.org/script/index.php that could certainly do this.

Otherwise, libjpeg sounds like a reasonable choice: http://www.ijg.org/ though I've never used it.

David

Member Avatar
pie555
Newbie Poster
4 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you very much. Also, if I were to use one of those libs which variable(s) would I want to plug into a function?
Edit: It seems ImageMagick would be a completely seperate tool from my program?

Member Avatar
pie555
Newbie Poster
4 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok, so I'm trying to make the IJG library but I have no idea what to do after I have jconfig.h.

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

Which compiler are you using? They do have a makefile for VC++ version 10. If you have cygwin, etc. you can use the build instructions for *nix.

Member Avatar
ashishchoure
Junior Poster
105 posts since Sep 2008
Reputation Points: 9 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
1
 

If you have support to MFC library then you can use CImage class. Its very easy to saving image as jpeg using CImage. Just pass handle to bitmap to CImage or load it from hard drive. Call Save function and specify jpeg as saving type.

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

If you have support to MFC library then you can use CImage class. Its very easy to saving image as jpeg using CImage. Just pass handle to bitmap to CImage or load it from hard drive. Call Save function and specify jpeg as saving type.

True. There are also options available in .NET if you want to go on that adventure (since if you use the express editions, they don't have MFC)

Member Avatar
pie555
Newbie Poster
4 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I was able to solve my problem when I found this code, posting it here so others can use it:
http://www.codeguru.com/forum/showthread.php?t=476912&page=2

void gdiscreen()
{
	SYSTEMTIME SysTime;
	GetLocalTime(&SysTime);
	wchar_t filename[200];
	memset(filename,0,sizeof(filename));
	wsprintfW(filename,L"C:\\screen%02d:%02d:%02d h.jpeg",SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
	
	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(filename, &clsid);

		SelectObject(memdc, hOldBitmap);

		DeleteObject(memdc);

		DeleteObject(membit);

		::ReleaseDC(0,scrdc);
	}

	GdiplusShutdown(gdiplusToken);
}
Question Answered as of 3 Years Ago by jonsca, daviddoria and ashishchoure
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article