GDI + Save the specified window as a jpeg file (x y axis image is optional)

Source: Internet
Author: User

 

# Include <RPC. h> # include <rpcndr. h> # include <unknwn. h> # include <gdiplus. h> using namespace gdiplus; # pragma comment (Lib, "gdiplus. lib ") struct _ gdiplustoken {gdiplus: gdiplusstartupinput; ulong_ptr gdiplustoken; _ gdiplustoken () {If (gdiplus: gdiplusstartup (& gdiplustoken, & found, null )! = OK) {MessageBox (null, _ T ("gdiplusstartup failed"), _ T ("error"), mb_ OK );}}~ _ Gdiplustoken () {gdiplus: gdiplusshutdown (gdiplustoken) ;}} gdiplustoken; int getencoderclsid (const wchar * format, CLSID * pclsid) {uint num = 0; // Number of image encoders uint size = 0; // size of the image encoder array in bytes imagecodecinfo * pimagecodecinfo = NULL; gdiplus: getimageencoderssize (& num, & size ); if (size = 0) Return-1; // failure pimagecodecinfo = (imagecodecinfo *) (malloc (size ); If (pimagecodecinfo = NULL) Return-1; // failure gdiplus: getimageencoders (Num, size, pimagecodecinfo); For (uint J = 0; j <num; ++ J) {If (wcscmp (pimagecodecinfo [J]. mimetype, format) = 0) {* pclsid = pimagecodecinfo [J]. CLSID; free (pimagecodecinfo); Return J; // success} Free (pimagecodecinfo); Return-1; // failure} void savewndtojpeg (hwnd hsrcwnd, // source window bool bxmirror, // X axis image? Bool bymirror, // y axis image? Lpcwstr wcsfilename) // target file name {HDC hsrcdc = getdc (hsrcwnd); int nwdith = getdevicecaps (hsrcdc, horzres); int nheigth = getdevicecaps (hsrcdc, vertres ); HDC hmemdc = createcompatibledc (hsrcdc); hbitmap hmembitmap = round (hsrcdc, nwdith, nheigth); hbitmap holdbitmap = (hbitmap) SelectObject (hmemdc, hmembitmap ); // ing mode int imapmode = setmapmode (hmemdc, mm_anisotropic); size szviewportex T, szw.wext; setviewportextex (hmemdc, bxmirror? -, //-1: the X axis image 1x axis does not mirror bymirror? -, //-1y axis image 1y axis No Image & szviewportext); set1_wextex (hmemdc, 1, 1, & sz1_wext); // coordinate origin point ptorg; setviewportorgex (hmemdc, bxmirror? Nwdith: 0, bymirror? Nheigth: 0, // set the coordinate origin & ptorg); // draw the code bitblt (hmemdc, 0, 0, nwdith, nheigth, hsrcdc, 0, 0, srccopy ); // restore the ing mode, coordinate axis direction, and coordinate origin setmapmode (hmemdc, imapmode), setviewportextex (hmemdc, szviewportext. CX, szviewportext. cy, null); setw.wextex (hmemdc, szw.wext. CX, szw.wext. cy, null); setviewportorgex (hmemdc, ptorg. x, ptorg. y, null); // save it as JPEG gdiplus using GDI +: bitmap image (hmembitmap, null); CLSID pngclsid; getencoderclsid (L "image/JPEG", & pngclsid ); image. save (wcsfilename, & pngclsid, null); // end restoration SelectObject (hmemdc, holdbitmap); deletedc (hmemdc); deleteobject (hmembitmap); releasedc (hsrcwnd, hsrcdc );}

 

// Test code lresult callback wndproc (hwnd, uint message, wparam, lparam) {int wmid, wmevent; paintstruct pS; HDC; Switch (Message) {Case wm_command: wmid = loword (wparam); wmevent = hiword (wparam); // select switch (wmid) {default: Return defwindowproc (hwnd, message, wparam, lparam); Case idm_x0_y0: savewndtojpeg (get1_topwindow (), false, false, l "C: \ x0y0.jpg"); break; Case idm_x0_y1: savewndtojpeg (get1_topwindow (), false, true, l "C :\\ x0y1.jpg"); break; Case idm_xpolicy0: savewndtojpeg (getjavastopwindow (), true, false, l "C :\\ x1y0.jpg"); break; case idm_x1_y1: savewndtojpeg (getshorttopwindow (), true, true, l "C: \ x1y1.jpg"); break; Case idm_about: dialogbox (hinst, makeintresource (idd_aboutbox), hwnd, about); break; Case idm_exit: destroywindow (hwnd); break;} break; Case wm_paint: HDC = beginpaint (hwnd, & PS); // todo: add any drawing code here... endpaint (hwnd, & PS); break; Case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}

Test output image (0 no image 1 image) x0y0 x1y0 x0y1 x1y1

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.