Use the COM interface of the ipicture Function

Source: Internet
Author: User
The ipicture interface is a COM interface supported by Windows 95 and later operating systems. It is used to operate on a variety of common image formats in windows. For example, many file formats such as BMP, JPEG, and GIF can be recognized. Easy to use. However, many may not be familiar with this interface, or do not know much about the operations on the COM interface. In my personal opinion, direct operations on the COM interface are indeed quite annoying. Therefore, we naturally thought of encapsulating these operations for future use. (I know that I am a lazy person, so I often do these things.) It is very convenient to use it. So let's share it with you.
Compressing large images into jpg or GIF files will greatly reduce the size of your software. Using this class is as convenient and fast as using standard BMP format images.
If you have any questions, suggestions, or better ideas in this article, please feel free to discuss them with me.
The most important thing about a class is, of course, how to use it. First of all, we will introduce how to use this class. The source code of the class will be appended to the end.

**************************************** ****************************************
Usage of the cpicture class
Class Name: cpicture
Purpose: encapsulate operations on files and resources in common formats (such as BMP, JPEG, and GIF) through the ipicture interface, and work with the support of MFC. Of course, if you are interested, you can simply change it to work without the support of MFC. I usually work in MFC, so it is convenient to write it into MFC.
Base Class: none.
Note: The coordinates and dimensions of an image are measured in pixels. The coordinate origin of the image is located at the top left corner of the image and is in the + y direction, and the coordinates are in the + x direction to the right.

Member function description:
Cpicture: cpicture ();
Description: constructor. Construct an empty cpicture object.

Bool cpicture: loadpicture (uint nresource, lpctstr lpszrestype );
Bool cpicture: loadpicture (maid, maid );
Note: This function loads an image from the resource. If the object already contains an image, the original image will be automatically released first, regardless of whether the loading is successful or not.
Return Value: if the load is successful, true is returned; otherwise, false is returned.
Parameters:
Nresource specifies the ID of a resource. This ID specifies a resource. The function will generate an Image Based on the Resource. You do not need to worry about the resource format, the object is automatically recognized (if you do not know the format of the resource, loading will fail ).
Lpszrestype specifies the type of the resource. This function can recognize a variety of non-standard image resources. Therefore, you should specify the type of the resource you load.
Lpszresource specifies a resource identifier, which specifies a resource. The function will generate an Image Based on the Resource. Do not worry about the resource format, the object is automatically recognized (if you do not know the format of the resource, loading will fail ).

Bool cpicture: loadpicturefromfile (lpctstr lpszfilename );
Note: This function loads an image from a file.
Return Value: if the load is successful, true is returned; otherwise, false is returned.
Parameters:
Lpszfilename is a pointer to a file name string.

Bool cpicture: isvalid ();
Determines whether an object is valid.
Return Value: if an object has a valid image, true is returned; otherwise, false is returned.
Parameter: None

Csize cpicture: getsize ();
Description: obtains the image size.
Return Value: A csize object that contains the image size. If you use this function for an invalid cpicture object, the return value is (0, 0 ).

Void cpicture: Draw (CDC * PDC, lpcrect lprcdest, lpcrect lprcsrc );
Void cpicture: Draw (CDC * PDC, int xdest, int ydest, int cxdest, int cydest,
Int xsrc, int ysrc, int cxsrc, int cysrc );
Description: draws images contained in an object on a DC surface. If the object does not contain any images, nothing is done.
Return Value: no return value.
Parameters:
A pointer to a DC object. The image is drawn on the surface of the device.
Lprcdest specifies a target rectangle. Partial images in the source rectangle are stretched and drawn to the image area on the device surface.
Lprcsrc specifies a source rectangular area. The part in the image inside the source rectangle is stretched and drawn to the target rectangle area on the device surface.
Xdest specifies the horizontal direction (x) coordinate in the upper left corner of the target rectangle. See the description of the parameter lprcdest.
Ydest specifies the vertical (y) coordinate in the upper left corner of the target rectangle. See the description of the parameter lprcdest.
Cxdest specifies the horizontal direction (width) of the target rectangle. See the description of the parameter lprcdest.
Cydest specifies the vertical direction (height) of the target rectangle. For details, see the description of the lprcdest parameter.
Xsrc specifies the horizontal direction (x) coordinate in the upper left corner of the source rectangle. See the description of the lprcsrc parameter.
Ysrc specifies the vertical (y) coordinate in the upper left corner of the source rectangle. See the description of the lprcsrc parameter.
Cxsrc specifies the horizontal direction (width) of the source rectangle. See the description of the lprcsrc parameter.
Cysrc specifies the vertical direction (height) of the source rectangle. See the description of the lprcsrc parameter.

Void cpicture: release ();
Note: remove the images contained in the object from the memory. Note that the object itself will not be removed from the memory. You can still use it to reload a new image. You do not need to remember to call release for images that are no longer in use at all times. When class destructor are performed, this function is naturally called. Of course, I am not opposed to calling this function explicitly to release images so as to leave a little memory resource empty.
Return Value: none.
Parameter: none.

**************************************** **************************************
Example: A simple Image Browsing Program (MFC program) implemented using this class. We need to add and modify a maximum of five lines, but the function is relatively (five lines of code) it is already "too powerful". It can browse a variety of images, including BMP, JPEG, GIF, icon, Tif, and other formats.
Let's first introduce picture. h: //////////////////////////////////////// //////////////////////////////
// Picture. h: interface for the cpicture class.
/Lounge stdio 2003
// Author: casoi (QQ: 714002785)
// E-mail: yuanweihuayan@126.com /////////////////////////////////////// /// // # ifndef picture_h
# Ifndef picture_h # define picture_h
# Define picture_h # If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000 class cpicture
{
Public:
Cpicture ();
Virtual ~ Cpicture (); public:
Bool loadpicture (uint nresource, lpctstr lpszrestype)
{Return loadpicture (makeintresource (nresource), lpszrestype );}
Bool loadpicturefromfile (lpctstr lpszfilename );
Bool loadpicture (maid );
Bool isvalid () {return m_ppic! = NULL ;}
Csize getsize () {return m_size ;}
Void draw (CDC * PDC, lpcrect lprcdest, lpcrect lprcsrc );
Void draw (CDC * PDC, int xdest, int ydest, int cxdest, int cydest,
Int xsrc, int ysrc, int cxsrc, int cysrc); void release (); protected:
Ipicture * m_ppic; ole_xsize_himetric _ w_him;
Ole_ysize_himetric _ h_him; csize m_size; protected:
Void calcsize ();
}; # Endif // define picture_h
# Endif // define picture_h and then yes. CPP file: //////////////////////////////////////// //////////////////////////////
// Picture. cpp: Implementation of the cpicture class.
/Lounge stdio 2003
// Author: casoi (QQ: 714002785)
// E-mail: yuanweihuayan@126.com /////////////////////////////////////// //// // # include "stdafx. h"
# Include "picture. H" # ifdef _ debug
# UNDEF this_file
Static char this_file [] =__ file __;
# Define new debug_new
# Endif ////////////////////////////////////// ////////////////////////////////
// Construction/destruction
//////////////////////////////////////// /// // Cpicture:: cpicture ()
: M_ppic (null), _ h_him (0), _ w_him (0), m_size (0, 0)
{
} Cpicture ::~ Cpicture ()
{
Release ();
} Void cpicture: release ()
{
If (m_ppic! = NULL)
{
M_ppic-> release ();
M_ppic = NULL;
_ H_him = _ w_him = 0;
M_size.cx = m_size.cy = 0;
}
} Bool cpicture: loadpicture (maid, maid)
{
Release (); hinstance hinst = afxfindresourcehandle (lpszresource, lpszrestype );
Hrsrc =: findresource (hinst, lpszresource, lpszrestype );
If (hrsrc = NULL) return false;
Hglobal = loadresource (hinst, hrsrc); If (hglobal = NULL) return false; DWORD dwsize = sizeofresource (hinst, hrsrc); hglobal hmem =: globalalloc (gmem_moveable, dwsize );
If (hmem = NULL) return false; lpvoid psrc =: lockresource (hglobal );
If (psrc = NULL ){
: Globalfree (hmem );
Return false;
} Lpvoid PDES =: globallock (hmem );
If (PDES = NULL ){
//: Globalunlock (hglobal );
: Globalfree (hmem );
Return false;
} Memcpy (PDES, psrc, dwsize); // globalunlock (hglobal );
Globalunlock (hmem);: freeresource (hglobal); istream * PSTM = NULL;
Createstreamonhglobal (hmem, true, & PSTM); If (! Succeeded (oleloadpicture (PSTM, dwsize, true, iid_ipicture, (lpvoid *) & m_ppic )))
{
PSTM-> release ();
: Globalfree (hmem );
PSTM = NULL;
Return false;
} PSTM-> release ();
: Globalfree (hmem); calcsize ();
Return true;} bool cpicture: loadpicturefromfile (lpctstr lpszfilename)
{
Release (); cfile file;
If (! File. Open (lpszfilename, cfile: moderead ))
Return false; DWORD dwsize = file. getlength (); hglobal hmem =: globalalloc (gmem_moveable, dwsize );
If (hmem = NULL) return false; lpvoid PDES =: globallock (hmem );
If (PDES = NULL ){
: Globalfree (hmem );
Return false;
} File. Read (PDES, dwsize );
File. Close (); globalunlock (hmem); istream * PSTM = NULL;
Createstreamonhglobal (hmem, true, & PSTM); If (! Succeeded (oleloadpicture (PSTM, dwsize, true, iid_ipicture, (lpvoid *) & m_ppic )))
{
PSTM-> release ();
: Globalfree (hmem );
PSTM = NULL;
Return false;
} PSTM-> release ();
: Globalfree (hmem); calcsize ();
Return true;} void cpicture: calcsize ()
{
If (m_ppic = NULL) return; m_ppic-> get_width (& _ w_him );
M_ppic-> get_height (& _ h_him); CDC * PDC = cwnd: get1_topwindow ()-> getdc ();
M_size.cx = _ w_him;
M_size.cy = _ h_him; PDC-> himetrictodp (& m_size); cwnd: get1_topwindow ()-> releasedc (PDC);} void cpicture: Draw (CDC * PDC, lpcrect lprcdest, lpcrect lprcsrc)
{
If (m_ppic)
{
Csize szorig (lprcsrc-> left, lprcsrc-> top );
Csize szsrc (lprcsrc-> right-lprcsrc-> left, lprcsrc-> bottom-lprcsrc-> top );
PDC-> dptohimetric (& szorig );
PDC-> dptohimetric (& szsrc); m_ppic-> render (* PDC, lprcdest-> left, lprcdest-> top, lprcdest-> right-lprcdest-> left,
Lprcdest-> bottom-lprcdest-> top, szorig. CX, _ h_him-szOrig.cy, szsrc. CX,
-Szsrc. Cy, null );
}
} Void cpicture: Draw (CDC * PDC, int xdest, int ydest, int cxdest, int cydest,
Int xsrc, int ysrc, int cxsrc, int cysrc)
{
Draw (PDC, crect (xdest, ydest, xdest + cxdest, ydest + cydest), crect (xsrc, ysrc, xsrc + cxsrc, ysrc + cysrc ));
} Note that there are many versions on the Internet, but I have tried many of them and cannot use them: m_pic.loadpicturefromfile ("C: // a.jpg "); (m_pic is an object of cpicture)
Csize SZ = m_pic.getsize ();
CDC * PDC = getdc ();
M_pic.draw (PDC, Sz. CX, Sz. Cy, Sz. CX, Sz. Cy );

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.