VC + + Implementation View content Save as a picture method _c language

Source: Internet
Author: User

This article describes the VC + + implementation view content saved as a picture of the method. Share to everyone for your reference, specific as follows:

In a single document application, we often need to save content in view as a picture file in a variety of formats for printing. At first glance, may not know where to start, in fact, the main is to use the bitmap save method, such as:

HDC HMEMDC =:: CreateCompatibleDC (HDC);
Hbitmap hbmp =:: CreateCompatibleBitmap (HDC, Destrect.width (), Destrect.height ());
HANDLE hOld =:: SelectObject (HMEMDC, hbmp);
Graphics graphic (HMEMDC);
The following are a variety of text, the drawing of a graph, a picture ... ..... ..... ...... ..... ..... ..... ..... ..... ..... ...... ...
Bitmap Bitmap (hbmp, NULL);
CLSID clsid;
Pictures that hold different formats (. jpg,bmp,png) require different CLSID, imageformat the picture format that the user expects to save (
_t ("jpg") = = Imageformat.makelower ())
{
   Getencoderclsid (_t ("Image/jpeg"), &clsid);
}
else if (_t ("bmp") = = Imageformat.makelower ())
{
   getencoderclsid (_t ("Image/bmp"), &clsid)
;
else if (_t ("png") = = Imageformat.makelower ())
{
   getencoderclsid (_t ("Image/png"), &clsid)
;
Save as a picture, strfn the path and filename bitmap for the picture
. Save (STRFN, &clsid, NULL);:
: SelectObject (HMEMDC, hOld);
::D eleteobject (hbmp);
::D Eletedc ( HMEMDC);

The next important thing is to get the CLSID of the different picture formats, and look at the following code:

Int getencoderclsid (const tchar* format, clsid* pclsid)
{
  UINT num= 0;
  UINT size= 0;
  imagecodecinfo* pimagecodecinfo= NULL;
  Getimageencoderssize (&num, &size);
  if (size== 0)
  {
    return-1;
  }
  Pimagecodecinfo= (imagecodecinfo*) (malloc (size));
  if (pimagecodecinfo== NULL)
  {
    return-1
  }
  Getimageencoders (num, size, pimagecodecinfo);
  For (UINT j=0 j< num; ++j)
  {
    if (_tcscmp). MimeType, format) = = 0)
    {
      *pclsid= pimagecodecinfo[j]. Clsid;
      Free (pimagecodecinfo);
      return j;
    }
  }
  Free (pimagecodecinfo);
  return-1;
}

If you are directly printing a picture in view, then the PDC for the view's OnDraw function refers to the size of the printer's paper, and we don't need to do much processing, so we just need to convert the area of the view to the size of the paper, for example:

CRect RC;
GetClientRect (&RC);
Long width =0,height = 0;
if (pdc->isprinting)
{
int  xlogpixperinch = pdc->getdevicecaps (logpixelsx);
int  ylogpixperinch = pdc->getdevicecaps (logpixelsy);
The ratio of the device coordinates to the logical coordinates is obtained by
long  Xext = (long) rc.width () * XLOGPIXPERINCH/96;
Long  Yext = (long) rc.height () * YLOGPIXPERINCH/96;
width = xext;
Height = Yext;
After using GDI + for drawing
hbitmap hbmp =:: CreateCompatibleBitmap (PDC->M_HDC, width, height);
HANDLE hOld =:: SelectObject (HMEMDC, hbmp);
Graphics Tmp_graff (HMEMDC);
.......... ....... ....... ...... .....
. ...  Gets the size of the paper and then stretches the copy
int ipagewidth = Pdc->getdevicecaps (horzres);
int ipageheight = Pdc->getdevicecaps (vertres);
:: StretchBlt (PDC->M_HDC, 0, 0, ipagewidth, ipageheight, HMEMDC, 0, 0, width, height, srccopy);  :: SelectObject (H  MEMDC, hOld);
::D eleteobject (hbmp);
::D Eletedc (HMEMDC);
}

I hope this article will help you with VC + + program design.

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.