Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Take screen shot and dump to .bmp file.

Got a new guitar? Tell us all about it here!

Take screen shot and dump to .bmp file.

Postby GrimSoul on Thu Jul 23, 2009 8:01 pm

Yes hi , my first post...im the shit blah blah

anyways , my first post will demonstrate how to take a screen shot and dump it to a .bmp file.
it has very little annotations or comments , because i have no time. but its here for people who want to use it, but if people want to learn from it / me, then i will spread my knowledge in the near future.

one should include windows.h in their program when using this fucntion and make sure Gdi32.lib is linked in as well. If your not sure how this is done, it is
Code: Select all
-lGDI32
in Dev-C++. Go to tools -> compiler options , tick "Add these commands to linker command line" and enter the above into the text box. Those using another compiler should google how to link external libraries.

Code: Select all
void TakeScreenShot(char* filename)
    {
       keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, 0); //emulate printscreen key
       keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
       HBITMAP h; //declare a variable of type hbitmap ( under windows.h i think )
        /* the way print screen works under windows is that it dumps the contents of your screen to the clipboard, put simply. we will get the contents of the clipboard and put it into our buffer. but first we must open the clipboard*/
       OpenClipboard(NULL);
       h = (HBITMAP)GetClipboardData(CF_BITMAP);//self explanatory, puts the contents of the clipboard into our respective buffer
       CloseClipboard(); // good programming
       HDC hdc=NULL;
    FILE*fp=NULL;
    LPVOID pBuf=NULL;
    BITMAPINFO bmpInfo;
    BITMAPFILEHEADER bmpFileHeader;
    do
       {
          hdc=GetDC(NULL);
    ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
    bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
    GetDIBits(hdc,h,0,0,NULL,&bmpInfo,DIB_RGB_COLORS); // func under windows.h
    if(bmpInfo.bmiHeader.biSizeImage<=0)
             bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
    if((pBuf = malloc(bmpInfo.bmiHeader.biSizeImage))==NULL)


        {
        MessageBox( NULL, "Unable to Allocate Bitmap Memory", "Error", MB_OK|MB_ICONERROR); // error
           break;
              }
        bmpInfo.bmiHeader.biCompression=BI_RGB;
        GetDIBits(hdc,h,0,bmpInfo.bmiHeader.biHeight,pBuf, &bmpInfo, DIB_RGB_COLORS);
        if((fp = fopen(filename,"wb"))==NULL) // checks to see if you provided a valid path and file name


            {
               MessageBox( NULL, "Unable to Create Bitmap File", "Error", MB_OK|MB_ICONERROR);
            break;
        }

        bmpFileHeader.bfReserved1=0;
        bmpFileHeader.bfReserved2=0;
        bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
        bmpFileHeader.bfType='MB';
        bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
        //begin write procedure
        fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
        fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
        fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp);
           }
           
           while(false);
              if(hdc)ReleaseDC(NULL,hdc);
        if(pBuf) free(pBuf);
        if(fp)fclose(fp);
    }


should have no problems , although i didnt extensivly test it. feel free to modify.

it should also be noted that this function expects a pointer of type char. supplying just a file name will dump the file to the directory of the program , HOWEVER if you wish to send it to another path, you should always use double black slashes. Example: C://Dir1//hi.bmp
and dont forget the .bmp extention. im sure you all know this , but this is sp00nfed after all (:

have fun with it , expect more - higher level fun snippetes in the future for those of you who want to get their hands a bit dirty.
Do not underestimate my post count.

Image
GrimSoul
 
Posts: 8
Joined: Thu Jul 23, 2009 7:27 pm

Re: Take screen shot and dump to .bmp file.

Postby zorro59 on Sat Jul 25, 2009 9:38 am

This is very usefull...
Thx!
Do you have any idea how to send the bmp using email???
Or to upload the file on a server without intervention?
zorro59
 
Posts: 80
Joined: Tue May 12, 2009 2:28 am

Re: Take screen shot and dump to .bmp file.

Postby GrimSoul on Mon Jul 27, 2009 4:41 pm

zorro59 wrote:This is very usefull...
Thx!
Do you have any idea how to send the bmp using email???
Or to upload the file on a server without intervention?

wow you are using it for evil purposes, weve all been there so i wont judge.
its possible to do both, or you could use winsock to send it over a socket. a tutorial on that will be here soon.
Do not underestimate my post count.

Image
GrimSoul
 
Posts: 8
Joined: Thu Jul 23, 2009 7:27 pm

Re: Take screen shot and dump to .bmp file.

Postby zorro59 on Wed Jul 29, 2009 5:36 am

I am waiting for the tut...
I dont want to use it for "evil" purposes...
I want to combine by control panel with sending data, so i can remotely control my pc...
I have 2-3 mb/s upload so speed wont be a issue...
zorro59
 
Posts: 80
Joined: Tue May 12, 2009 2:28 am

Re: Take screen shot and dump to .bmp file.

Postby GrimSoul on Thu Jul 30, 2009 3:25 pm

zorro59 wrote:I am waiting for the tut...
I dont want to use it for "evil" purposes...
I want to combine by control panel with sending data, so i can remotely control my pc...
I have 2-3 mb/s upload so speed wont be a issue...

its @ showcase full programs
Do not underestimate my post count.

Image
GrimSoul
 
Posts: 8
Joined: Thu Jul 23, 2009 7:27 pm

Re: Take screen shot and dump to .bmp file.

Postby zorro59 on Thu Aug 13, 2009 2:41 pm

Question:
Is it your program?
http://planetsourcecode.com/vb/scripts/ ... 4&lngWId=3
Why it always says that corrupted bmp???
zorro59
 
Posts: 80
Joined: Tue May 12, 2009 2:28 am

Re: Take screen shot and dump to .bmp file.

Postby C++ on Thu Aug 13, 2009 4:03 pm

Moved to Off-Topic / Social Space

The file I/O used here is C code. There's nothing wrong with posting this, but the full programs & snippets section are for demonstrating code in C++
Obviously there are things that can't be done with C++ standard code and using a library is just fine.
Just try to avoid posting C code in the sections that are about C++
This might be confusing to new people.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am


Return to Off-Topic / Social Space

Who is online

Users browsing this forum: No registered users and 0 guests