2 Replies - 2954 Views - Last Post: 02 July 2012 - 09:58 PM Rate Topic: -----

#1 Senior Slick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-July 12

Question - Win32 API Screenshot?

Posted 02 July 2012 - 12:38 PM

Ok I've been doing some research on how to take a bitmap screenshot. I've been overloaded with information everywhere I went and I can't seem to get my function working. I prefer if I understand what I'm doing, so here's what I got so far.

1. Get the handle of DeviceContent.
2. Create Memory Device.
3. Get Screen resolution.
4. Create a bitmap with x and y and copy the pixels onto it using bitblt();
5. Now my last step would be to save the bitmap to disk. However I'm trying to save it as a jpeg instead of a bmp.
How would I go around doing this?

Appreciated, thank you.


#include <Windows.h>
#include <iostream>

using namespace std;

int main()
{
	HDC hScreenDC = GetDC(NULL);
	HDC hMemoryDC = CreateCompatibleDC(hScreenDC);

	int x = GetDeviceCaps(hScreenDC, HORZRES);
	int y = GetDeviceCaps(hScreenDC, VERTRES);

	HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);
	HGDIOBJ hOldBitmap = SelectObject(hMemoryDC, hBitmap);
	
	BitBlt(hMemoryDC, 0, 0, x, y, hScreenDC, 0, 0, SRCCOPY);
	//hBitmap = SelectObject(hMemoryDC, hOldBitmap);

	HANDLE hFile = CreateFileA("screenshot.bmp", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	DeleteDC(hScreenDC);
	DeleteDC(hMemoryDC);

	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Question - Win32 API Screenshot?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,986
  • Joined: 05-May 12

Re: Question - Win32 API Screenshot?

Posted 02 July 2012 - 02:28 PM

I suggest downloading a library, or use the built in Windows Imaging Component: http://msdn.microsof...719871(v=vs.85)

You don't want to reinvent the wheel to do the JPEG encoding by rolling your own.
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 768
  • View blog
  • Posts: 2,237
  • Joined: 20-March 10

Re: Question - Win32 API Screenshot?

Posted 02 July 2012 - 09:58 PM

Hi,

There is also GDI+ Image,

as Skydiver has said you don't want to roll your own especially when Windows will do it for you.

look into other GDI+ functions

ImageCodecInfo

GetImageEncodersSize

GetImageEncoders

GDI+ reference

GDI+ reference

Snoopy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1