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 ();
}
}
}
}