Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,103 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,148 people online right now. Registration is fast and FREE... Join Now!





Flashot

Takes a snapshot of your current screen and then saves it into a .bmp file.

Submitted By: WolfCoder
Actions:
Rating:
Views: 2,273

Language: C++

Last Modified: March 17, 2008
Instructions: Run to take a snapshot. It saves the bitmap in the same path of the program.

Snippet


  1. // Takes a snapshot of the screen and saves it to the disk
  2. #include <windows.h>
  3. // by: Wolf Coder
  4. #include <windows.h>
  5. RGBQUAD tempimg[1024*768];
  6. RGBTRIPLE outimg[1024*768];
  7. int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,PSTR szcmdline,int icmdshow)
  8. {
  9.      HDC hdc,xdc,mdc;
  10.      HWND hwnd;
  11.      HBITMAP hbitmap; // For frame locking
  12.      HPEN hpen = CreatePen(PS_SOLID,0,RGB(0,0,0));
  13.      HBRUSH hbrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
  14.      if(LockWindowUpdate(hwnd = GetDesktopWindow()))
  15.      {
  16.           hdc = GetDCEx(hwnd,NULL,DCX_CACHE | DCX_LOCKWINDOWUPDATE);
  17.           xdc = CreateCompatibleDC(hdc);
  18.           mdc = CreateCompatibleDC(hdc);
  19.           // Flashshot the screen
  20.           hbitmap = CreateCompatibleBitmap(hdc,1024,768);
  21.           SelectObject(xdc,hbitmap);
  22.           BitBlt(xdc,0,0,1024,768,hdc,0,0,SRCCOPY);
  23.           // Open a channel to bitmap
  24.           DWORD written;
  25.           HANDLE hfile = CreateFile("snapshot.bmp",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  26.           // Fill out a bitmap header form
  27.           BITMAPFILEHEADER bfh;
  28.           bfh.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
  29.           bfh.bfReserved1 = 0;
  30.           bfh.bfReserved2 = 0;
  31.           bfh.bfSize = sizeof(BITMAPFILEHEADER);
  32.           bfh.bfType = 0x4d42;
  33.           WriteFile(hfile,&bfh,sizeof(bfh),&written,NULL);
  34.           // Fill out an info form
  35.           BITMAPINFOHEADER bih;
  36.           bih.biBitCount = 24;
  37.           bih.biClrImportant = 0;
  38.           bih.biClrUsed = 0;
  39.           bih.biCompression = BI_RGB;
  40.           bih.biHeight = 768;
  41.           bih.biPlanes = 1;
  42.           bih.biSize = sizeof(bih);
  43.           bih.biSizeImage = 3*1024*768;
  44.           bih.biWidth = 1024;
  45.           bih.biXPelsPerMeter = 2400;
  46.           bih.biYPelsPerMeter = 2400;
  47.           WriteFile(hfile,&bih,sizeof(bih),&written,NULL);
  48.           // Copy the bits to a buffer
  49.           GetBitmapBits(hbitmap,1024*768*4,&tempimg);
  50.           // Write the bits to a second buffer
  51.           for(int y = 0;y < 768;y++)
  52.           {
  53.                for(int x = 0;x < 1024;x++)
  54.                {
  55.                     int index = ((767-y)*1024)+x;
  56.                     int from_index = x+y*1024;
  57.                     outimg[index].rgbtBlue = tempimg[from_index].rgbBlue;
  58.                     outimg[index].rgbtGreen = tempimg[from_index].rgbGreen;
  59.                     outimg[index].rgbtRed = tempimg[from_index].rgbRed;
  60.                }
  61.           }
  62.           // Write to file
  63.           WriteFile(hfile,&outimg[0],1024*768*3,&written,NULL);
  64.           // File is done
  65.           CloseHandle(hfile);
  66.           // Now pack everything away
  67.           DeleteObject(hbitmap);
  68.           ReleaseDC(hwnd,hdc);
  69.           DeleteDC(hdc);
  70.           DeleteDC(xdc);
  71.           DeleteDC(mdc);
  72.           LockWindowUpdate(hwnd);
  73.      }
  74.      DeleteObject(hpen);
  75.      DeleteObject(hbrush);
  76.      MessageBox(hwnd,"SCREENSHOT TAKEN","complete",MB_OK);
  77.      return 1;
  78. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month