Display of BMP, JPG, and GIF images in VC

Source: Internet
Author: User
It is very convenient to use the picture control of VB to display BMP, JPG, and GIF, but it cannot be directly displayed under VC and MFC. So I wrote an image display function by referring to the online example, we hope this will make it easier for you to use.
Windows provides a standard method for displaying BMP, JPG, and GIF images. You only need to instantiate ipicture and call it, use the oleloadpicture function to read an image from the stream and create an ipicture object for the image:
Oleloadpicture (PSTM, dwfilesize, true, iid_ipicture, (lpvoid *) & PPIC );
It is defined in msdn as follows:
Stdapi oleloadpicture (
Istream * pstream,
// Pointer to the stream that contains picture's data
Long lsize, // number of bytes read from the stream
Bool frunmode,
// The opposite of the initial value of the picture's
// Property
Refiid riid, // reference to the identifier of the interface
// Describing the type of interface pointer to return
Void ppvobj // address of output variable that has es Interface
// Pointer requested in riid
);

The following is an image display function displaypic in the cnewformdlg dialog box. You can use this function to display an image on a control. This function can display BMP, JPG, and GIF files. The parameters are as follows:
Char * lpimagefile Image File Name
Hwnd: display the object handle
Int nscrwidth display width
Int nscrheight display height

Hresult cnewformdlg: displaypic (char * lpimagefile, hwnd, int nscrwidth, int nscrheight)
{
HDC hdc_temp =: getdc (hwnd );

Ipicture * PPIC;
Istream * PSTM;

Bool bresult;

Handle hfile = NULL;
DWORD dwfilesize, dwbyteread;

// Open the graphic file on the hard disk
Hfile = createfile (lpstrfile, generic_read,
File_share_read, null, open_existing, file_attribute_normal, null );

If (hfile! = Invalid_handle_value)
{
Dwfilesize = getfilesize (hfile, null); // get the number of file bytes

If (dwfilesize = 0 xffffffff)
Return e_fail;
}
Else
{
Return e_fail;
}

// Allocate a global Bucket
Hglobal = globalalloc (gmem_moveable, dwfilesize );
Lpvoid pvdata = NULL;

If (hglobal = NULL)
{
Afxmessagebox ("Image File Allocation Error .");
Return e_fail;
}

If (pvdata = globallock (hglobal) = NULL) // lock the allocated memory block
{
Afxmessagebox ("memory block locking problems ");
Return e_fail;
}

Readfile (hfile, pvdata, dwfilesize, & dwbyteread, null); // read the file into the memory buffer

Globalunlock (hglobal );

If (createstreamonhglobal (hglobal, true, & PSTM )! = S_ OK)
{
Afxmessagebox ("stream initialization failed ");
Return e_fail;
}

// Mount the Graphic File
Bresult = oleloadpicture (PSTM, dwfilesize, true, iid_ipicture, (lpvoid *) & PPIC );

If (failed (bresult ))
{
Afxmessagebox ("image file loading error .");
Return e_fail;
}

Ole_xsize_himetric hmwidth; // The actual width of the image.
Ole_ysize_himetric hmheight; // The actual height of the image.
PPIC-> get_width (& hmwidth );
PPIC-> get_height (& hmheight );

// Output the image to the screen
Bresult = PPIC-> render (hdc_temp, 0, 0, nscrwidth, nscrheight,
0, hmheight, hmwidth,-hmheight, null );

Closehandle (hfile); // close the opened file

PPIC-> release ();
// Free memory.
Globalfree (hglobal );

If (succeeded (bresult ))
{
Return s_ OK;
}
Else
{
Afxmessagebox ("image file loading error .");
Return e_fail;
}
}
The call is as follows:
Void cnewformdlg: onbutton2 ()
{
Hwnd m_hwnd;
Getdlgitem (idc_static_view, & m_hwnd );
Displaypic ("F: // image // crystal // crystal521.jpg", m_hwnd, 500,500 );
}

 

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.