C/C ++: how to load JPG images in a program?

Source: Internet
Author: User
 

Only the interfaces for loading BMP files by default are available for cwnd provided by MFC, which is not supported for jpg and other images. In reality, non-BMP images are often used and COM technology is required for loading them. First, write the following functions:

Bool loadmydomainfile (cstring fname, lppicture * lppi)
{
Handle hfile = createfile (fname, generic_read, 0, null, open_existing, 0, null );

If (hfile = invalid_handle_value)
{
Cstring STR;
Str. Format (_ T ("% s cannot be opened"), fname );
MessageBox (STR );
Return false;
}
// Obtain the file size
DWORD dwfilesize = getfilesize (hfile, null );

If (DWORD)-1 = dwfilesize)
{
Closehandle (hfile );
MessageBox (_ T ("image file is empty "));
Return false;
}
// Read image files
Lpvoid pvdata;

// Allocate memory by file size
Hglobal = globalalloc (gmem_moveable, dwfilesize );

If (null = hglobal)
{
Closehandle (hfile );
MessageBox (_ T ("insufficient memory, insufficient memory allocation "));
Return false;
}

Pvdata = globallock (hglobal );
If (null = pvdata)
{
Globalunlock (hglobal );
Closehandle (hfile );
MessageBox (_ T ("memory cannot be locked "));
Return false;
}

DWORD dwfileread = 0;
Bool bread = readfile (hfile, pvdata, dwfilesize, & dwfileread, null );
Globalunlock (hglobal );
Closehandle (hfile );
If (false = bread)
{
MessageBox (_ T ("file read error "));
Return false;
}

Lpstream PSTM = NULL;
// Generate an istream stream from the allocated memory
Hresult hR = createstreamonhglobal (hglobal, true, & PSTM );

If (! Succeeded (HR ))
{
MessageBox (_ T ("failed to generate stream operation "));
If (PSTM! = NULL)
PSTM-> release ();
Return false;
}
Else if (PSTM = NULL)
{
MessageBox (_ T ("failed to generate stream operation "));
Return false;
}

If (! * Lppi)
(* Lppi)-> release ();
HR = oleloadpicture (PSTM, dwfilesize, false, iid_ipicture, (lpvoid *) & (* lppi ));
PSTM-> release ();
If (! Succeeded (HR ))
{
MessageBox (_ T ("loading failed "));
Return false;
}
Else if (* lppi = NULL)
{
MessageBox (_ T ("loading failed "));
Return false;
}
Return true;
}

Then add the variable declaration and function declaration to the header file:

Bool loadmydomainfile (cstring fname, lppicture * lppi );
Lppicture m_lppi; // load the stream of the image file
Bool m_bhadload; // The background image has been loaded.
Then add the following to the onpaint function:

If (m_bhadload)
{
CDC * PDC = getdc ();
Crect RC;
Long hmwidth = 0;
Long hmheight = 0;
M_lppi-> get_height (& hmheight );
M_lppi-> get_width (& hmwidth );
Getclientrect (& rc );
Int nwidth, nheight;
Nwidth = RC. Width ();
Nheight = RC. Height ();
Hresult hR = m_lppi-> render (PDC-> m_hdc, nwidth, 0,-nwidth, nheight,

Hmwidth, hmheight,-hmwidth,-hmheight, & rc );
}
In the oninitdialog function, call the above loading function as follows:

Tchar strpath [max_path];
Memset (strpath, 0, max_path );
Getcurrentdirectory (max_path, strpath );
Wcscat_s (strpath, max_path, _ T ("// a_bear.jpg "));
M_bhadload = loadmydomainfile (strpath, & m_lppi );
The jpg image can be displayed. Remember to add the following to the ondestroy function:

M_lppi-> release ();
To release objects.

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.