How does VC ++ Save the customer partition as BMP and VC to output the self-drawing to the BMP file?

Source: Internet
Author: User

How does VC ++ Save the customer zone as BMP?

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.

 

Reprinted statement: This article from http://wmnmtm.blog.163.com/blog/static/38245714200963032449/

========================================================== ======================================

 

VC implements self-Drawing Output to BMP files

When using VC as a program, you often need to save the output text and graphics in place to the graph file. Of course, there can be ready-made controls for implementation, but you can never write them as you wish! For example:

I. Implementation Method

To save text and graphics in place, as long as you have a certain understanding of the bitmap structure, everything is OK. First, you must create a memory device environment, and then the dib region created in the memory device environment. Do not forget to create a cbitmap object. The cbitmap object must be associated with the dib region, select the cbitmap object to the current device environment, and then output the text and graphics in the current device environment.

II. The specific implementation code is as follows:

Void ctestsavebmpview: saveasbmp (cstring filename)
{
// Define the image size
Int iwidth = 800;
Int iheight = 600;
Int ipixel = 16;
// Graphic format parameters
Lpbitmapinfo lpbmih = new bitmapinfo;
Lpbmih-> bmiheader. bisize = sizeof (bitmapinfoheader );
Lpbmih-> bmiheader. biwidth = iwidth;
Lpbmih-> bmiheader. biheight = iheight;
Lpbmih-> bmiheader. biplanes = 1;
Lpbmih-> bmiheader. bibitcount = ipixel;
Lpbmih-> bmiheader. bicompression = bi_rgb;
Lpbmih-> bmiheader. bisizeimage = 0;
Lpbmih-> bmiheader. bixpelspermeter = 0;
Lpbmih-> bmiheader. biypelspermeter = 0;
Lpbmih-> bmiheader. biclrused = 0;
Lpbmih-> bmiheader. biclrimportant = 0;
 
// Create bitmap data
HDC, hdcmem;
Hbitmap = NULL;
Cbitmap * pbitmap = NULL;
CDC * pmemdc = NULL;
Byte * pbits;
 
HDC = createic (text ("display"), null );
Hdcmem = createcompatibledc (HDC );
Hbitmap = createdibsection (hdcmem, lpbmih, dib_pal_colors, (void **) & pbits, null, 0 );
Pbitmap = new cbitmap;
Pbitmap-> attach (hbitmap );
Pmemdc = new CDC;
Pmemdc-> attach (hdcmem );
Pmemdc-> SelectObject (pbitmap );
//
Crect RC (0, 0, iwidth, iheight );
Pmemdc-> setbkmode (transparent );
// Add a self-drawing shape
Drawcurve (pmemdc, RC );
// Save it to a file and create a bitmap Structure
Bitmapfileheader bmfh;
Zeromemory (& bmfh, sizeof (bitmapfileheader ));
* (Char *) & bmfh. bftype) = 'B '';
* (Char *) & bmfh. bftype) + 1) = 'M '';
Bmfh. bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader );
Bmfh. bfsize = bmfh. bfoffbits + (iwidth * iheight) * ipixel/8;
 
Tchar szbmp filename [128];
Int ibmpbytes = iwidth * iheight * ipixel/8;
Strcpy (szbmp filename, filename );
Cfile file;
If (file. Open (szbmp filename, cfile: modewrite | cfile: modecreate ))
{
File. Write (& bmfh, sizeof (bitmapfileheader ));
File. Write (& (lpbmih-> bmiheader), sizeof (bitmapinfoheader ));
File. Write (pbits, ibmpbytes );
File. Close ();
}
 
Pmemdc-> deletedc ();
Delete pmemdc; pmemdc = NULL;
Delete pbitmap; pbitmap = NULL;
Delete lpbmih; lpbmih = NULL;
}

// Output text and graphics

Void ctestsavebmpview: drawcurve (CDC * PDC, crect rcclient)
{
// Page background color
Cbrush brushctl;
Brushctl. createsolidbrush (RGB (255,255,255 ));
PDC-> rectangle (rcclient );
PDC-> fillrect (rcclient, & brushctl );
Brushctl. deleteobject ();
 
Cpen pen;
Pen. createpen (ps_solid, 1, RGB (255, 0, 0 ));
Cpen * oldpen = PDC-> SelectObject (& pen );
 
Double xmin = 10.00f, xmax = 100.00f;
Double ymin = 10.00f, Ymax = 200.00f;
 
Double dbx1 = (xmax-xmin)/100 + xmin;
Double dby1 = 600/dbx1;
For (INT I = 1; I <100; I ++) // Curve
{
Double dbx2 = (xmax-xmin) * I/100 + xmin;
Double dby2 = 600/dbx2;

PDC-> moveTo (INT (rcclient. Left + (dbx1-xmin) * rcclient. Width ()/(xmax-xmin )),
INT (rcclient. Bottom-(dby1-ymin) * rcclient. Height ()/(Ymax-ymin )));
PDC-> lineto (INT (rcclient. Left + (dbx2-xmin) * rcclient. Width ()/(xmax-xmin )),
INT (rcclient. Bottom-(dby2-ymin) * rcclient. Height ()/(Ymax-ymin )));

Dbx1 = dbx2;
Dby1 = dby2;
}
PDC-> SelectObject (oldpen );
Pen. deleteobject ();
Oldpen = NULL;
}

// Save implementation
Void ctestsavebmpview: onrbuttondown (uint nflags, cpoint point)
{
 
Cfiledialog DLG (false, null, null, ofn_hidereadonly | ofn_overwriteprompt,
"Bitmap file (*. BMP) | *. BMP | ",
Null );
If (DLG. domodal ()! = Idok) return;
Cstring filename = DLG. getfilename () + ". BMP ";
Saveasbmp (filename );
 
Cview: onrbuttondown (nflags, point );
}

 

Reprinted statement: This article from http://wmnmtm.blog.163.com/blog/static/38245714200963005616282/

========================================================== ======================================

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.