Save the screen as an image Save the current MFC program as an image C ++ VC

Source: Internet
Author: User
Tags bmp image

Save the screen as an image and use vs2008 for compilation.

View plaincopy to clipboardprint?
  1. # Include "stdafx. H"
  2. # Include <windows. h>
  3. # Include <atlimage. h>
  4. Int _ stdcall winmain (
  5. Hinstance,
  6. Hinstance hprevinstance,
  7. Lpstr lpcmdline,
  8. Int nshowcmd)
  9. {
  10. Hwnd =: getdomaintopwindow ();
  11. HDC =: getdc (hwnd); // obtain the screen DC
  12. Rect;
  13. : Getclientrect (hwnd, & rect );// Obtain the screen size
  14. HDC hdcmem =: createcompatibledc (HDC); // create compatible DC
  15. Hbitmap =: createcompatiblebitmap (HDC, rect. Right, rect. Bottom); // create a compatible bitmap
  16. Hbitmap holdmap = (hbitmap): SelectObject (hdcmem, hbitmap); // select the bitmap into the DC and save the returned value.
  17. : Bitblt (hdcmem, 0, 0, rect. Right, rect. Bottom, HDC, 0, 0, srccopy );// Copy the DC image of the screen to the memory DC
  18. Cimage image;
  19. Image. Attach (hbitmap );
  20. Image. Save (_ T ("C: \ B .jpg"); // if the file suffix is .bmp, save it as BMP.
  21. Image. Detach ();
  22. : SelectObject (hdcmem, holdmap );// Select the last returned value
  23. // Release
  24. : Deleteobject (hbitmap );
  25. : Deletedc (hdcmem );
  26. : Deletedc (HDC );
  27. Return 0;
  28. }

# Include "stdafx. H "# include <windows. h> # include <atlimage. h> int _ stdcall winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {hwnd =: get1_topwindow (); HDC =: getdc (hwnd ); // obtain the screen DC rect;: getclientrect (hwnd, & rect); // obtain the screen size HDC hdcmem =: createcompatibledc (HDC ); // create compatible DC hbitmap =: createcompatiblebitmap (HDC, rect. right, rect. bottom); // create a compatible bitmap hbitmap holdmap = (hbitmap): SelectObject (hdcmem, hbitmap); // select the bitmap into the DC and save the returned value: bitblt (hdcmem, 0, 0, rect. right, rect. bottom, HDC, 0, 0, srccopy); // copy the DC image to the memory DC. attach (hbitmap); image. save (_ T ("C: \ B .jpg"); // if the file suffix is .bmp, save it as a BMP image. detach ();: SelectObject (hdcmem, holdmap); // select the last returned value // release: deleteobject (hbitmap);: deletedc (hdcmem );:: deletedc (HDC); Return 0 ;}

Set the current MFCProgram(Here is a single document program) Save as an image, use vs2005. The first one uses the cimage class to save the image, and the second one uses bitmapinfo.

Save the screen as an image Save the current MFC program as an image C ++ VC Favorites

Save the screen as an image and use vs2008 for compilation.

View plaincopy to clipboardprint?
  1. # Include "stdafx. H"
  2. # Include <windows. h>
  3. # Include <atlimage. h>
  4. Int _ stdcall winmain (
  5. Hinstance,
  6. Hinstance hprevinstance,
  7. Lpstr lpcmdline,
  8. Int nshowcmd)
  9. {
  10. Hwnd =: getdomaintopwindow ();
  11. HDC =: getdc (hwnd); // obtain the screen DC
  12. Rect;
  13. : Getclientrect (hwnd, & rect );// Obtain the screen size
  14. HDC hdcmem =: createcompatibledc (HDC); // create compatible DC
  15. Hbitmap =: createcompatiblebitmap (HDC, rect. Right, rect. Bottom); // create a compatible bitmap
  16. Hbitmap holdmap = (hbitmap): SelectObject (hdcmem, hbitmap); // select the bitmap into the DC and save the returned value.
  17. : Bitblt (hdcmem, 0, 0, rect. Right, rect. Bottom, HDC, 0, 0, srccopy );// Copy the DC image of the screen to the memory DC
  18. Cimage image;
  19. Image. Attach (hbitmap );
  20. Image. Save (_ T ("C: \ B .jpg"); // if the file suffix is .bmp, save it as BMP.
  21. Image. Detach ();
  22. : SelectObject (hdcmem, holdmap );// Select the last returned value
  23. // Release
  24. : Deleteobject (hbitmap );
  25. : Deletedc (hdcmem );
  26. : Deletedc (HDC );
  27. Return 0;
  28. }

# Include "stdafx. H "# include <windows. h> # include <atlimage. h> int _ stdcall winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {hwnd =: get1_topwindow (); HDC =: getdc (hwnd ); // obtain the screen DC rect;: getclientrect (hwnd, & rect); // obtain the screen size HDC hdcmem =: createcompatibledc (HDC ); // create compatible DC hbitmap =: createcompatiblebitmap (HDC, rect. right, rect. bottom); // create a compatible bitmap hbitmap holdmap = (hbitmap): SelectObject (hdcmem, hbitmap); // select the bitmap into the DC and save the returned value: bitblt (hdcmem, 0, 0, rect. right, rect. bottom, HDC, 0, 0, srccopy); // copy the DC image to the memory DC. attach (hbitmap); image. save (_ T ("C: \ B .jpg"); // if the file suffix is .bmp, save it as a BMP image. detach ();: SelectObject (hdcmem, holdmap); // select the last returned value // release: deleteobject (hbitmap);: deletedc (hdcmem );:: deletedc (HDC); Return 0 ;}

Save the current MFC Program (here is a single document program) as an image and use vs2005. The first one uses the cimage class to save the image, and the second one uses bitmapinfo.

First:

View plaincopy to clipboardprint?
  1. Void cmainframe: ongetmap ()
  2. {
  3. Hwnd = This-> getsafehwnd ();
  4. HDC =: getwindowdc (hwnd); // obtain the DC
  5. Rect;
  6. : Getwindowrect (hwnd, & rect );// Obtain the screen size
  7. HDC hdcmem =: createcompatibledc (HDC); // create compatible DC
  8. Hbitmap =: createcompatiblebitmap (HDC, rect. right-rect.left, rect. bottom-rect.top); // create compatible bitmap
  9. Hbitmap holdmap = (hbitmap): SelectObject (hdcmem, hbitmap); // select the bitmap into the DC and save the returned value.
  10. : Bitblt (hdcmem, 0, 0, rect. right-rect.left, rect. bottom-rect.top, HDC, 0, 0, srccopy );// Copy the DC image of the screen to the memory DC
  11. Cimage image;// Required # include <atlimage. h>
  12. Image. Attach (hbitmap );
  13. Image. Save (_ T ("C: \ B .jpg"); // if the file suffix is .bmp, save it as BMP.
  14. Image. Detach ();
  15. : SelectObject (hdcmem, holdmap );// Select the last returned value
  16. // Release
  17. : Deleteobject (hbitmap );
  18. : Deletedc (hdcmem );
  19. : Deletedc (HDC );
  20. }

Void cmainframe: ongetmap () {hwnd = This-> getsafehwnd (); HDC =:: getwindowdc (hwnd); // obtain DC rect ;:: getwindowrect (hwnd, & rect); // obtain the screen size HDC hdcmem =: createcompatibledc (HDC); // create compatible DC hbitmap =: createcompatiblebitmap (HDC, rect. right-rect.left, rect. bottom-rect.top); // create compatible bitmap hbitmap holdmap = (hbitmap): SelectObject (hdcmem, hbitmap); // select the bitmap to DC and save the returned value: bitblt (hdcmem, 0, 0, rect. right-rect.left, rect. bottom-rect.top, HDC, 0, 0, srccopy); // copy the screen DC image to the memory DC cimage image; // need # include <atlimage. h> image. attach (hbitmap); image. save (_ T ("C: \ B .jpg"); // if the file suffix is .bmp, save it as a BMP image. detach ();: SelectObject (hdcmem, holdmap); // select the last returned value // release: deleteobject (hbitmap);: deletedc (hdcmem );:: deletedc (HDC );}

Second:

  1. Void cmainframe: ongetmap ()
  2. {
  3. CDC * PDC = getwindowdc ();
  4. Cbitmap bitmap;
  5. CDC memdc;
  6. Crect rect;
  7. Getwindowrect (rect );
  8. Memdc. createcompatibledc (PDC );
  9. Bitmap. createcompatiblebitmap (PDC, rect. Width (), rect. Height ());
  10. Memdc. SelectObject (& Bitmap );
  11. Memdc. bitblt (0, 0, rect. Width (), rect. Height (), PDC, 0, srccopy );
  12. Cfiledialog fdlg (false, _ T ("BMP"), null, ofn_hidereadonly | ofn_overwriteprompt, _ T ("bitmap file | *. BMP"), this );
  13. If (fdlg. domodal () = idok)
  14. {
  15. Cstring BMP file = fdlg. getpathname ();
  16. Cfile file (BMP file, cfile: modecreate | cfile: modewrite );
  17. Bitmap binfo;
  18. Bitmap. getbitmap (& binfo );
  19. // Calculate the palette size
  20. Int panelsize = 0;
  21. If (binfo. bmbitspixel <24) // non-real color
  22. {
  23. Panelsize = POW ((Double) 2, binfo. bmbitspixel) * sizeof (rgbquad );
  24. }
  25. // Define bitmap Information
  26. Bitmapinfo * bmapinfo = (bitmapinfo *) localalloc (lptr,Sizeof (bitmapinfo) + panelsize );
  27. Bmapinfo-> bmiheader. bibitcount = binfo. bmbitspixel;
  28. Bmapinfo-> bmiheader. biclrimportant = 0;
  29. Bmapinfo-> bmiheader. bicompression = 0;
  30. Bmapinfo-> bmiheader. biheight = binfo. bmheight;
  31. Bmapinfo-> bmiheader. biplanes = binfo. bmplanes;
  32. Bmapinfo-> bmiheader. bisize =Sizeof (bitmapinfo );
  33. Bmapinfo-> bmiheader. bisizeimage = binfo. bmheight * binfo. bmwidthbytes;
  34. Bmapinfo-> bmiheader. biwidth = binfo. bmwidth;
  35. Bmapinfo-> bmiheader. bixpelspermeter = 0;
  36. Bmapinfo-> bmiheader. biypelspermeter = 0;
  37. // Obtain the actual bitmap data
  38. Char * pdata = new char [bmapinfo-> bmiheader. bisizeimage];
  39. Int Len = getdibits (PDC-> m_hdc, bitmap, 0, binfo. bmheight, pdata, bmapinfo, dib_rgb_colors );
  40. Bitmapfileheader bfileheader;
  41. Bfileheader. bftype = 0x4d42;
  42. Bfileheader. bfreserved1 = 0;
  43. Bfileheader. bfreserved2 = 0;
  44. Bfileheader. bfsize =Sizeof (bitmapfileheader );
  45. Bfileheader. bfoffbits =Sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + panelsize;
  46. // Write bitmap data to the file
  47. File. Write (& bfileheader,Sizeof (bitmapfileheader ));
  48. File. Write (& bmapinfo-> bmiheader,Sizeof (bitmapinfoheader ));
  49. File. Write (pdata, bmapinfo-> bmiheader. bisizeimage + panelsize );
  50. File. Close ();
  51. Delete pdata;
  52. Localfree (bmapinfo );
  53. }
  54. Bitmap. deleteobject ();
  55. Memdc. deletedc ();
  56. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.