Use GDI + to save HDC as a bitmap file

Source: Internet
Author: User
// GDI + is used to load and store images. It shields the details of internal codec, compression, and decompression. Program Personnel bring great convenience // the current version of GDI + supports bmp jpeg gif tiff PNG and other formats // The basic idea of the following program is to first save the content on the Target DC to the memory bitmap // use GDI + bitmap again:: fromhbitmap loads the memory bitmap and saves it as a file. // This version is tested in Win32 and MFC environments. /// use GDI + to save HDC as a bitmap file # 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; _ gdiplus Token () {If (gdiplus: gdiplusstartup (& gdiplustoken, & gdiplusstartupinput, 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} bool savehdctofile (HDC, lprect) {bool Bret = false; int nwidth = lprect-> right-lprect-> left; int nheight = lprect-> bottom-lprect-> top; // map the target region to the memory bitmap HDC memdc = createcompatibledc (HDC); hbitmap hbmp = createcompatiblebitmap (HDC, nwidth, nheight); SelectObject (memdc, hbmp); bitblt (memdc, lprect-> left, lprect-> top, nwidth, nheight, HDC, 0, 0, srccopy ); // save it as a file {// L "image/BMP" L "image/JPEG" L "image/GIF" L "image/tiff" L "image/PNG" CLSID pngclsid; getencoderclsid (L "image/BMP", & pngclsid); // take BMP as an example. Select the corresponding type for other formats, for example, JPG uses l "image/JPEG" gdiplus :: bitmap * pbmsrc = gdiplus: bitmap: fromhbitmap (hbmp, null); If (pbmsrc-> Save (L "C: \ test.bmp", & pngclsid) = OK) {Bret = true;} Delete pbmsrc;} // cleanup SelectObject (memdc, (hbitmap) null); deletedc (memdc); deleteobject (hbmp); Return Bret ;}

 

 

 
// Test case lresult callback wndproc (hwnd, uint message, wparam, lparam) {int wmid, wmevent; paintstruct pS; HDC; Switch (Message) {Case wm_paint: HDC = beginpaint (hwnd, & PS); // todo: add any drawing hereCode... Textout (HDC, 0, 0, _ T ("ABCD"), 4); endpaint (hwnd, & PS); break; Case wm_keydown: {HDC = getdc (hwnd); rect rcrect; getclientrect (hwnd, & rcrect); savehdctofile (HDC, & rcrect); releasedc (hwnd, HDC); break ;} case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}

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.