8 Replies - 7227 Views - Last Post: 02 July 2012 - 04:09 PM Rate Topic: -----

#1 MrCAT22  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 27-June 12

how to save full screen into a bitmap file

Posted 01 July 2012 - 05:11 AM

Hi,
I in trying to translate MASM32 code from
http://www.asmcommun...p?topic=20194.0

into C code
Here is what I had done. It successfull to create a BMP file with 1366x768(my screen resolution) in directory C:\file1.bmp

But, when I open it, it's only blank black screen and it's size only 54 Bytes.

What problem? I think I already make it correctly? :online2long: :P :no:

	BITMAPFILEHEADER hdr;
	BITMAPINFOHEADER pbih;
	BITMAPINFO bmpinfo;
	BITMAPFILEHEADER bmpFileHeader;
	HANDLE hfile;
	HBITMAP hbitmap;
	HDC hdc,cdc;
	DWORD dwWidth, dwHeight, dwBPP, dwNumColors,ColorSize,dwByte;
	LPVOID pBits;
	RGBQUAD ColorTable[256];

	hdc=CreateDC("DISPLAY", NULL, NULL, NULL);
	if(hdc==NULL)
	{
		MessageBox(NULL, "Couldn''t create DC", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
	}

	dwWidth=GetDeviceCaps(hdc, HORZRES);
	dwHeight=GetDeviceCaps(hdc, VERTRES);
	dwBPP = GetDeviceCaps(hdc, BITSPIXEL);
	dwBPP=24;		//24 Bit for LWF | JPEG | JPEG2000 output, else doesnt work
	if(dwBPP<=8)
	{
		dwNumColors = GetDeviceCaps(hdc, NUMCOLORS);
		dwNumColors = 256;
	}
	else
	{
		dwNumColors = 0;
	}


	cdc=CreateCompatibleDC(hdc);
	if(cdc==NULL)
	{
		DeleteDC(hdc);
		MessageBox(NULL, "Couldn''t create compatible DC", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
		return ;
	}



	// Create bitmap
	bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmpinfo.bmiHeader.biWidth = dwWidth;
	bmpinfo.bmiHeader.biHeight = dwHeight;
	bmpinfo.bmiHeader.biPlanes = 1;			//LOWORD(0x00000001);
	bmpinfo.bmiHeader.biBitCount = (WORD) dwBPP;	//LOWORD(dwBPP);
	bmpinfo.bmiHeader.biCompression = BI_RGB;
	bmpinfo.bmiHeader.biSizeImage = 0;
	bmpinfo.bmiHeader.biXPelsPerMeter = 0;
	bmpinfo.bmiHeader.biYPelsPerMeter = 0;
	bmpinfo.bmiHeader.biClrUsed = dwNumColors;
	bmpinfo.bmiHeader.biClrImportant = dwNumColors;
	hbitmap = CreateDIBSection(hdc, &bmpinfo,DIB_PAL_COLORS, &pBits, NULL, 0);
	if(hbitmap==NULL)
	{
		DeleteDC(hdc);
		DeleteDC(cdc);
		MessageBox(NULL, "Couldn''t create compatible bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
	}

	if(!SelectObject(cdc,hbitmap))
	{
		DeleteDC(hdc);
		DeleteDC(cdc);
		MessageBox(NULL, "Couldn''t select bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
	}


	if(!BitBlt(cdc,0,0,dwWidth,dwHeight,hdc,0,0,SRCCOPY))
	{
		DeleteDC(hdc);
		DeleteDC(cdc);
		MessageBox(NULL, "Couldn''t copy bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
	}

	if(dwNumColors!=0)
	{
		dwNumColors=GetDIBColorTable(cdc,0,dwNumColors,ColorTable);
	}


	hdr.bfType = 0x4d42;	// File type designator "BM" 0x42 = "B" 0x4d = "M"
	ColorSize=dwNumColors*sizeof(RGBQUAD);
	hdr.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ColorSize+((dwWidth*dwHeight*dwBPP)/8);
	hdr.bfReserved1=0;
	hdr.bfReserved2=0;
	hdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ColorSize;

	pbih.biSize=sizeof(BITMAPINFOHEADER);
	pbih.biWidth=dwWidth;
	pbih.biHeight=dwHeight;
	pbih.biPlanes=1;			//LOWORD(0x00000001)
	pbih.biBitCount=(WORD) dwBPP;		//LOWORD(dwBPP)
	pbih.biCompression=BI_RGB;
	pbih.biSizeImage=0;
	pbih.biXPelsPerMeter=0;
	pbih.biYPelsPerMeter=0;
	pbih.biClrUsed=dwNumColors;
	pbih.biClrImportant=0;
	strcpy(strText,"C:\\file1.bmp");
	hfile = CreateFile(
			strText,
			GENERIC_WRITE,
			(DWORD) 0,
			NULL, 
			CREATE_ALWAYS,
			FILE_ATTRIBUTE_NORMAL,
			(HANDLE) NULL);
	if (hfile == INVALID_HANDLE_VALUE)
	{
		DeleteObject(hbitmap);
		DeleteDC(cdc);
		DeleteDC(hdc);
		MessageBox(NULL, "Couldn''t write file to disk.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
	}
	WriteFile(hfile, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwByte,  NULL);
	WriteFile(hfile, (LPVOID) &pbih, sizeof(BITMAPINFOHEADER), (LPDWORD) &dwByte, NULL);
	if(dwNumColors!=0)
	{
		WriteFile(hfile, ColorTable,ColorSize, (LPDWORD) &dwByte,  NULL);
	}
	ColorSize=(dwWidth*dwHeight*dwBPP)/8;		// No of pixels
	WriteFile(hfile,&pBits,ColorSize,&dwByte,NULL);

	CloseHandle(hfile);
	MessageBox(NULL, "Bitmap created.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
	DeleteObject(hbitmap);
	DeleteDC(cdc);
	DeleteDC(hdc);



Is This A Good Question/Topic? 0
  • +

Replies To: how to save full screen into a bitmap file

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,465
  • Joined: 05-May 12

Re: how to save full screen into a bitmap file

Posted 01 July 2012 - 05:55 AM

Have you actually traced through your code to find out why you are only writing out 54 bytes?

In particular interest is line 120: is dwNumColors != 0? With the number of times you clobbered that variable before getting to line 120, I wasn't sure anymore what its value is supposed to be.
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: how to save full screen into a bitmap file

Posted 01 July 2012 - 02:03 PM

Hi,

You do a lot of things right and do a lot of things wrong...

It's probably better to point you to one of my Tutorials

In which there is a bitmap saving routine which actually works.

I would read the whole Tutorial if I were you.

Paying particular attention to Part 18.


The code is heavily commented.

Then compare it to your own code

and identify all the areas where you are going wrong and ammend them.


My Tutorial

Paying particular attention to Memory allocation
and how to retrieve the Color Table from the DIB.

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

#4 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • Joined: 19-February 09

Re: how to save full screen into a bitmap file

Posted 01 July 2012 - 05:58 PM

Also pBits is already a pointer, so the address-of operator is not needed.

125	WriteFile(hfile,&pBits,ColorSize,&dwByte,NULL);


Was This Post Helpful? 0
  • +
  • -

#5 MrCAT22  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 27-June 12

Re: how to save full screen into a bitmap file

Posted 02 July 2012 - 04:31 AM

Hi #define, thank you for the clue.
It can make it's size become 3MB.
And I make a few repairing. Now, it's not blank black screen but still not get expected bitmap.
Here is the bitmap:

How to repair it? Where part of the code I must fix it?

And here is the code:
				BITMAPFILEHEADER hdr;
				BITMAPINFOHEADER pbih;
				BITMAPINFO bmpinfo;
				BITMAPFILEHEADER bmpFileHeader;
				HANDLE hfile;
				HBITMAP hbitmap;
				HDC hdc,cdc;
				DWORD dwWidth, dwHeight, dwBPP, dwNumColors,ColorSize,dwByte;
				LPVOID pBits;
				RGBQUAD ColorTable[256];


				hdc=CreateDC("DISPLAY", NULL, NULL, NULL);
				if(hdc==NULL)
				{
					MessageBox(NULL, "Couldn''t create DC", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
				}

				dwWidth=GetDeviceCaps(hdc, HORZRES);
				dwHeight=GetDeviceCaps(hdc, VERTRES);
				dwBPP = GetDeviceCaps(hdc, BITSPIXEL);
				dwBPP=24;
				if(dwBPP<=8)
				{
					dwNumColors = GetDeviceCaps(hdc, NUMCOLORS);
					dwNumColors = 256;
				}
				else
				{
					dwNumColors = 0;
				}


				cdc=CreateCompatibleDC(hdc);
				if(cdc==NULL)
				{
					DeleteDC(hdc);
					MessageBox(NULL, "Couldn''t create compatible DC", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
					return ;
				}



				// Create bitmap
				bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
				bmpinfo.bmiHeader.biWidth = dwWidth;
				bmpinfo.bmiHeader.biHeight = dwHeight;
				bmpinfo.bmiHeader.biPlanes = 1;			//LOWORD(0x00000001);
				bmpinfo.bmiHeader.biBitCount = (WORD) dwBPP;	//LOWORD(dwBPP);
				bmpinfo.bmiHeader.biCompression = BI_RGB;
				bmpinfo.bmiHeader.biSizeImage = 0;
				bmpinfo.bmiHeader.biXPelsPerMeter = 0;
				bmpinfo.bmiHeader.biYPelsPerMeter = 0;
				bmpinfo.bmiHeader.biClrUsed = dwNumColors;
				bmpinfo.bmiHeader.biClrImportant = dwNumColors;

				hbitmap = CreateDIBSection(hdc, &bmpinfo,DIB_PAL_COLORS,(LPVOID *)&pBits, NULL, 0);
				if(hbitmap==NULL)
				{
					DeleteDC(hdc);
					DeleteDC(cdc);
					MessageBox(NULL, "Couldn''t create compatible bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
				}



				if(!SelectObject(cdc,hbitmap))
				{
					DeleteDC(hdc);
					DeleteDC(cdc);
					MessageBox(NULL, "Couldn''t select bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
				}


				if(!BitBlt(cdc,0,0,dwWidth,dwHeight,hdc,0,0,SRCCOPY))
				{
					DeleteDC(hdc);
					DeleteDC(cdc);
					MessageBox(NULL, "Couldn''t copy bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
				}

				if(dwNumColors!=0)
				{
					dwNumColors=GetDIBColorTable(cdc,0,dwNumColors,ColorTable);

				}


				hdr.bfType = 0x4d42;	// File type designator "BM" 0x42 = "B" 0x4d = "M"
				ColorSize=dwNumColors*sizeof(RGBQUAD);
				hdr.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ColorSize+((dwWidth*dwHeight*dwBPP)/8);
				hdr.bfReserved1=0;
				hdr.bfReserved2=0;
				hdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ColorSize;


				pbih.biSize=sizeof(BITMAPINFOHEADER);
				pbih.biWidth=dwWidth;
				pbih.biHeight=dwHeight;
				pbih.biPlanes=1;			//LOWORD(0x00000001)
				pbih.biBitCount=16;			//LOWORD(dwBPP)
				pbih.biCompression=BI_RGB;
				pbih.biSizeImage=0;
				pbih.biXPelsPerMeter=0;
				pbih.biYPelsPerMeter=0;
				pbih.biClrUsed=dwNumColors;
				pbih.biClrImportant=0;
				strcpy(strText,"C:\\file1.bmp");
				hfile = CreateFile(
						strText,
						GENERIC_WRITE,
						(DWORD) 0,
						NULL, 
						CREATE_ALWAYS,
						FILE_ATTRIBUTE_NORMAL,
						(HANDLE) NULL);
				if (hfile == INVALID_HANDLE_VALUE)
				{
					DeleteObject(hbitmap);
					DeleteDC(cdc);
					DeleteDC(hdc);
					MessageBox(NULL, "Couldn''t write file to disk.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
				}


				WriteFile(hfile, &hdr, sizeof(BITMAPFILEHEADER), &dwByte,  NULL);
				WriteFile(hfile, &pbih, sizeof(BITMAPINFOHEADER), &dwByte, NULL);
				if(dwNumColors!=0)
				{
					WriteFile(hfile, ColorTable,ColorSize, &dwByte,  NULL);
				}
				ColorSize=(dwWidth*dwHeight*dwBPP)/8;		// No of pixels
				WriteFile(hfile,pBits,ColorSize,&dwByte,NULL);

				CloseHandle(hfile);
				MessageBox(NULL, "Bitmap created.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
				DeleteObject(hbitmap);
				DeleteDC(cdc);
				DeleteDC(hdc);


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,465
  • Joined: 05-May 12

Re: how to save full screen into a bitmap file

Posted 02 July 2012 - 06:37 AM

Not to be harsh, but do you understand the code you are writing, or are you blindly translating/porting code from the link that you referenced in the original post?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: how to save full screen into a bitmap file

Posted 02 July 2012 - 07:27 AM

Quite. Retry your google fu. Assembly code? ASM windows is stunt programming; it all goes through the same layer.

You think you have enough to do a better search? "C screen capture GDI" should give you a viable hit.

You even have a viable function to riff off of, so swap CreateDIBSection in there and see how far you get.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,465
  • Joined: 05-May 12

Re: how to save full screen into a bitmap file

Posted 02 July 2012 - 02:23 PM

To the OP: Check out one of the newer questions today: http://www.dreaminco...api-screenshot/

Looks like he's missing the rest of his code to write out the bitmap headers an contents.

This post has been edited by Skydiver: 02 July 2012 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: how to save full screen into a bitmap file

Posted 02 July 2012 - 04:09 PM

God,

are we still going on with this ...?
If the OP had taken my advice he could have ended up with this... in no time at all

BITMAPFILEHEADER hdr;
PBITMAPINFOHEADER pbih;
PBITMAPINFO bmpinfo;

HANDLE hfile;
HBITMAP hbitmap;
HDC hdc,cdc;
DWORD dwWidth, dwHeight, dwBPP, dwNumColors,ColorSize,dwByte,dwPlanes;
LPVOID pBits;
RGBQUAD ColorTable[256];
CHAR strText[25]= "file1.bmp";

hdc=CreateDC("DISPLAY", NULL, NULL, NULL);
if(hdc==NULL)
{
	MessageBox(NULL, "Couldn''t create DC", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
}

dwWidth=GetDeviceCaps(hdc, HORZRES);
dwHeight=GetDeviceCaps(hdc, VERTRES);
dwPlanes = GetDeviceCaps(hdc, PLANES);
dwBPP=24;
dwNumColors = 0;


cdc=CreateCompatibleDC(hdc);
if(cdc==NULL)
{
	DeleteDC(hdc);
	MessageBox(NULL, "Couldn''t create compatible DC", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
	return 2 ;
}



// Create bitmap
bmpinfo = (PBITMAPINFO) LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1<< 24));


bmpinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo->bmiHeader.biWidth = dwWidth;
bmpinfo->bmiHeader.biHeight = dwHeight;
bmpinfo->bmiHeader.biPlanes = (WORD)1;			//LOWORD(0x00000001);
bmpinfo->bmiHeader.biBitCount = (WORD) dwBPP;	//LOWORD(dwBPP);
bmpinfo->bmiHeader.biCompression = 0;
bmpinfo->bmiHeader.biSizeImage = ((bmpinfo->bmiHeader.biWidth * 24 +31) & ~31) /8 * bmpinfo->bmiHeader.biHeight;; 
bmpinfo->bmiHeader.biXPelsPerMeter = 0;
bmpinfo->bmiHeader.biYPelsPerMeter = 0;
bmpinfo->bmiHeader.biClrUsed = 0;
bmpinfo->bmiHeader.biClrImportant = 0;
pBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, bmpinfo->bmiHeader.biSizeImage);
hbitmap = CreateDIBSection(hdc, bmpinfo,DIB_RGB_COLORS,&pBits, NULL, 0);
if(hbitmap==NULL)
{
	DeleteDC(hdc);
	DeleteDC(cdc);
	MessageBox(NULL, "Couldn''t create compatible bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
}



if(!SelectObject(cdc,hbitmap))
{
	DeleteDC(hdc);
	DeleteDC(cdc);
	MessageBox(NULL, "Couldn''t select bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
}


if(!BitBlt(cdc,0,0,dwWidth,dwHeight,hdc,0,0,SRCCOPY))
{
	DeleteDC(hdc);
	DeleteDC(cdc);
	MessageBox(NULL, "Couldn''t copy bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
}

pbih = (PBITMAPINFOHEADER) bmpinfo;

hdr.bfType = 0x4d42;	// File type designator "BM" 0x42 = "B" 0x4d = "M"

hdr.bfSize=(DWORD)(sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage);;
hdr.bfReserved1=0;
hdr.bfReserved2=0;
hdr.bfOffBits=(DWORD)sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD);

hfile = CreateFile(
		strText,
		GENERIC_WRITE,
		(DWORD) 0,
		NULL, 
		CREATE_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		(HANDLE) NULL);
if (hfile == INVALID_HANDLE_VALUE)
{
	DeleteObject(hbitmap);
	DeleteDC(cdc);
	DeleteDC(hdc);
	MessageBox(NULL, "Couldn''t write file to disk.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
}


WriteFile(hfile,(char*)&hdr, sizeof(BITMAPFILEHEADER), &dwByte,  NULL);
WriteFile(hfile, (char*)pbih, sizeof(BITMAPINFOHEADER)+ pbih->biClrUsed * sizeof (RGBQUAD), &dwByte, NULL);


ColorSize= pbih->biSizeImage;
WriteFile(hfile,(char*)pBits,(int)ColorSize,&dwByte,NULL);

CloseHandle(hfile);
MessageBox(NULL, "Bitmap created.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
DeleteObject(hbitmap);
DeleteDC(cdc);
DeleteDC(hdc);
LocalFree(bmpinfo);
GlobalFree(pBits);



As I said before he does a lot of things right and a lot wrong...

He probably doesn't really have a much of an insight into this subject

Snoopy.

This post has been edited by snoopy11: 03 July 2012 - 02:21 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1