How to display BMP and jpg images in the MFC window

Source: Internet
Author: User

// Draw images through resources
Static void drawbitmap (uint bitmap, CDC * PDC, crect rect)
{
Cbitmap BMP;
If (BMP. loadbitmap (Bitmap ))
{
Bitmap BMP Info;
BMP. getbitmap (& BMP info );
CDC dcmemory;
Dcmemory. createcompatibledc (PDC );
Dcmemory. SelectObject (& BMP );
PDC-> setstretchbltmode (halftone );
PDC-> stretchblt (rect. left, rect. top, rect. width (), rect. height (), & dcmemory, 0, BMP info. bmwidth, BMP info. bmheight, srccopy );
}
}

// Draw a picture using a BMP file
Static void drawbitmapformfile (cstring bitmapfilepath, CDC * PDC, crect rect)
{
Handle filehandle =: LoadImage (null, bitmapfilepath, image_bitmap, 0, 0, lr_loadfromfile );
If (filehandle! = NULL)
{
Cbitmap BMP;
If (BMP. Attach (filehandle ))
{
Bitmap BMP Info;
BMP. getbitmap (& BMP info );
CDC dcmemory;
Dcmemory. createcompatibledc (PDC );
Dcmemory. SelectObject (& BMP );
PDC-> setstretchbltmode (halftone );
PDC-> stretchblt (rect. left, rect. top, rect. width (), rect. height (), & dcmemory, 0, BMP info. bmwidth, BMP info. bmheight, srccopy );
BMP. Detach ();
}
}
}
// Draw images through resources
Static void drawbitmap (uint bitmap, CDC * PDC, crect rect)
{
Cbitmap BMP;
If (BMP. loadbitmap (Bitmap ))
{
Bitmap BMP Info;
BMP. getbitmap (& BMP info );
CDC dcmemory;
Dcmemory. createcompatibledc (PDC );
Dcmemory. SelectObject (& BMP );
PDC-> setstretchbltmode (halftone );
PDC-> stretchblt (rect. left, rect. top, rect. width (), rect. height (), & dcmemory, 0, BMP info. bmwidth, BMP info. bmheight, srccopy );
}
}

// Draw a picture using a BMP file
Static void drawbitmapformfile (cstring bitmapfilepath, CDC * PDC, crect rect)
{
Handle filehandle =: LoadImage (null, bitmapfilepath, image_bitmap, 0, 0, lr_loadfromfile );
If (filehandle! = NULL)
{
Cbitmap BMP;
If (BMP. Attach (filehandle ))
{
Bitmap BMP Info;
BMP. getbitmap (& BMP info );
CDC dcmemory;
Dcmemory. createcompatibledc (PDC );
Dcmemory. SelectObject (& BMP );
PDC-> setstretchbltmode (halftone );
PDC-> stretchblt (rect. left, rect. top, rect. width (), rect. height (), & dcmemory, 0, BMP info. bmwidth, BMP info. bmheight, srccopy );
BMP. Detach ();
}
}
}

View plaincopy to clipboardprint?
Bool cshowbitmapview: showjpggif (CDC * PDC, cstring strpath, int X, int y) // open jpg and other image files by reading file information
{
Istream * PSTM;
Cfilestatus fstatus;
Cfile file;
Long CB;
// Open the file and check the file Validity
If (file. Open (strpath, cfile: moderead )&&
File. getstatus (strpath, fstatus )&&
(Cb = fstatus. m_size )! =-1 ))
{
Hglobal = globalalloc (gmem_moveable, CB );
Lpvoid pvdata = NULL;
If (hglobal! = NULL)
{
Pvdata = globallock (hglobal );
If (pvdata! = NULL)
{
// File. readhuge (pvdata, CB); // This function may be used in 6.0.
File. Read (pvdata, CB); // use this function in vc2005.net
Globalunlock (hglobal );
Createstreamonhglobal (hglobal, true, & PSTM );
}
}
}
Else
{
Return false;
} // The end of opening the file
// Display JPEG and GIF images. a GIF can only display one frame, but not an animation,
// Use the Active Control to display animated GIF images.
Ipicture * PPIC;
// Load image from file stream
If (succeeded (oleloadpicture (PSTM, fstatus. m_size, true, iid_ipicture, (lpvoid *) & PPIC )))
{
Ole_xsize_himetric hmwidth;
Ole_ysize_himetric hmheight;
PPIC-> get_width (& hmwidth );
PPIC-> get_height (& hmheight );
Double FX, FY;
// Get image height and width
FX = (double) PDC-> getdevicecaps (horzres) * (double) hmwidth/(double) PDC-> getdevicecaps (horzsize) * 100.0 );
FY = (double) PDC-> getdevicecaps (vertres) * (double) hmheight/(double) PDC-> getdevicecaps (vertsize) * 100.0 );
// Use render function display image
If (failed (PPIC-> render (* PDC, X, Y, (DWORD) FX, (DWORD) FY, 0, hmheight, hmwidth,-hmheight, null )))
{
PPIC-> release ();
Return false;
}
PPIC-> release ();
}
Else
{
Return false;
}
Return true;
}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/logic_nut/archive/2009/11/20/4843923.aspx

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.