# 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