MFC uses hbitmap and cbitmap to display bitmap files

Source: Internet
Author: User

 

There are n methods for displaying bitmap files:

1. Use dual-buffer technology;

Source code:

Void showbmp ()

{

Cstring STR;
Cstring filter = "bitmap files (*. BMP) | *. BMP | WMF files (*. WMF) | *. WMF | ";
Cfiledialog DLG (true, null, null, ofn_hidereadonly | ofn_overwriteprompt, (lpctstr) filter, null );
If (DLG. domodal () = idok)
{

STR = DLG. getpathname ();
Hbitmap = (hbitmap) LoadImage (AfxGetInstanceHandle (), STR, image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection );
Cbitmap bitmap;
Bitmap. Attach (hbitmap); // associate a bitmap object
CDC * PDC = getdc ();
CDC dcimage;
Dcimage. createcompatibledc (PDC); // memory DC
Bitmap BMP;
Bitmap. getbitmap (& BMP); // retrieves bitmap information.
Dcimage. SelectObject (& Bitmap); // select a bitmap object
PDC-> bitblt (, BMP. bmwidth, BMP. bmheight, & dcimage, srccopy); // display
}
};

2 Use cximage. Draw ()

Source code:

Void cmap2dlg: onbutton3 ()
{
// Todo: add your control notification handler code here
Cximage image;
// Cimage image;
Cstring filename;
Cstring filter = "JPEG files (*. JPG) | *. JPG | bitmap files (*. BMP) | *. BMP | WMF files (*. WMF) | *. WMF | ";
Cfiledialog DLG (true, null, null, ofn_hidereadonly | ofn_overwriteprompt, (lpctstr) filter, null );
If (DLG. domodal () = idok)
{
Filename = DLG. getfilename ();
Cstring fileext; // image Extension
Int Len = filename. getlength ();
For (INT I = len-1; I> = 0; I --) // get the image Extension
{
If (filename [I] = '.')
{
Fileext = filename. mid (I + 1 );
Break;
}
}
Fileext. makelower (); // convert the extension to lowercase.
If (fileext! = _ T (""))
{
// Create a cximage object. The static method cximage: gettypeidfromname is used to obtain the ID representation of the image format based on the extension.

Cximage image (filename, cximage: gettypeidfromname (fileext);/**/cximage_format_jpg );*/

If (image. isvalid ())
{
HDC = (HDC) getforegroundwindow ();
CDC * PDC = getdc ();
Crect rect;
Getwindowrect (& rect );
Cpaintdc DC (this );
Image. Draw (DC. getsafehdc (), rect, 0, 0 );
Image. Destroy ();
}
}

}
}

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.