How does VC ++ Save the customer zone as BMP [zz]

Source: Internet
Author: User
Add the following functions to the View class:

Bool ctestestview: writewindowtodib (lptstr szfile, cwnd * pwnd)

{

Cbitmap bitmap;

Cwindowdc DC (pwnd );

CDC memdc;

Crect rect;

Memdc. createcompatibledc (& DC );

Pwnd-> getwindowrect (rect );

Bitmap. createcompatiblebitmap (& DC, rect. Width (), rect. Height ());

Cbitmap * poldbitmap = memdc. SelectObject (& Bitmap );

Memdc. bitblt (, rect. Width (), rect. Height (), & DC, srccopy );

Cpalette pal;

If (DC. getdevicecaps (rastercaps) & rc_palette)

{

Uint nsize = sizeof (logpalette) + (sizeof (paletteentry) * 256 );

Logpalette * PLP = (logpalette *) New byte [nsize];

PLP-> palversion = 0x300;

PLP-> palnumentries = getsystempaletteentries (DC, 0,255, PLP-> palpalentry );

Pal. createpalette (PLP );

Delete [] PLP;

 

}

Memdc. SelectObject (poldbitmap );

Handle hdib = ddbtodib (bitmap, bi_rgb, & pal );

If (hdib = NULL)

Return false;

Writedib (szfile, hdib );

Globalfree (hdib );

Return true;

 

}

 

Handle ctestestview: ddbtodib (cbitmap & bitmap, DWORD dwcompression, cpalette * ppal)

{

Bitmap bm;

Bitmapinfoheader Bi;

Lpbitmapinfoheader lpbi;

DWORD dwlen;

Handle hdib;

Handle handle;

HDC;

Hpalette hpal;

Assert (bitmap. getsafehandle ());

If (dwcompression = bi_bitfields)

Return NULL;

Hpal = (hpalette) ppal-> getsafehandle ();

If (hpal = NULL)

Hpal = (hpalette) getstockobject (default_palette );

Bitmap. GetObject (sizeof (BM), (lpstr) & BM );

Bi. bisize = sizeof (bitmapinfoheader );

Bi. biwidth = BM. bmwidth;

Bi. biheight = BM. bmheight;

Bi. biplanes = 1;

Bi. bibitcount = BM. bmplanes * BM. bmbitspixel;

Bi. bicompression = dwcompression;

Bi. bisizeimage = 0;

Bi. bixpelspermeter = 0;

Bi. biypelspermeter = 0;

Bi. biclrimportant = 0;

Bi. biclrused = 0;

 

Int ncolors = (1 <bi. bibitcount );

If (ncolors> 256)

Ncolors = 0;

Dwlen = Bi. bisize + ncolors * sizeof (rgbquad );

HDC =: getdc (null );

Hpal = selectpalette (HDC, hpal, false );

Realizepalette (HDC );

Hdib = globalalloc (gmem_fixed, dwlen );

If (! Hdib)

{

Selectpalette (HDC, hpal, false );

: Releasedc (null, HDC );

Return NULL;

 

}

Lpbi = (lpbitmapinfoheader) hdib;

* Lpbi = Bi;

Getdibits (HDC, (hbitmap) bitmap. getsafehandle (), 0l, (DWORD) Bi. biheight, (lpbyte) null, (lpbitmapinfo) lpbi, (DWORD) B _dirgb_colors );

Bi = * lpbi;

If (Bi. bisizeimage = 0)

{

Bi. bisizeimage = (Bi. biwidth * bi. bibitcount) + 31 )&~ 31)/8) * bi. biheight;

If (dwcompression! = Bi_rgb)

Bi. bisizeimage = (Bi. bisizeimage * 3)/2;

}

 

Dwlen + = Bi. bisizeimage;

If (handle = globalrealloc (hdib, dwlen, gmem_moveable ))

Hdib = handle;

Else

{

Globalfree (hdib );

Selectpalette (HDC, hpal, false );

: Releasedc (null, HDC );

Return NULL;

}

Lpbi = (lpbitmapinfoheader) hdib;

Bool bgotbits = getdibits (HDC, (hbitmap) bitmap. getsafehandle (), 0l, (DWORD) Bi. biheight, (lpbyte) lpbi + (Bi. bisize + ncolors * sizeof (rgbquad )),

(Lpbitmapinfo) lpbi, (DWORD) dib_rgb_colors );

If (! Bgotbits)

{

Globalfree (hdib );

Selectpalette (HDC, hpal, false );

: Releasedc (null, HDC );

Return NULL;

}

Selectpalette (HDC, hpal, false );

: Releasedc (null, HDC );

Return hdib;

}

 

Bool ctestestview: writedib (lptstr szfile, handle hdib)

{

Bitmapfileheader HDR;

Lpbitmapinfoheader lpbi;

If (! Hdib)

Return false;

Cfile file;

If (! File. Open (szfile, cfile: modewrite | cfile: modecreate ))

Return false;

Lpbi = (lpbitmapinfoheader) hdib;

Int ncolors = 1 <lpbi-> bibitcount;

HDR. bftype = (Word) ('M' <8) | 'B ');

HDR. bfsize =: globalsize (hdib) + sizeof (HDR );

HDR. bfreserved1 = 0;

HDR. bfreserved2 = 0;

HDR. bfoffbits = (DWORD) (sizeof (HDR) + lpbi-> bisize + ncolors * sizeof (rgbquad ));

File. Write (& HDR, sizeof (HDR ));

File. Write (lpbi, globalsize (hdib ));

Return true;

 

}

Call the writewindowtodib function when saving ()

Void ctestestview: onfilesave ()

{

// Todo: add your command handler code here

Writewindowtodib ("D: \ my.bmp", this );

}

 

I have tested it. You can add it directly, but note that the class name should be the same as yours.

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.