Click here to Skip to main content
15,897,273 members
Articles / Desktop Programming / MFC
Article

Simple class for drawing pictures (JPG, TIFF, GIF, etc...)

Rate me:
Please Sign up or sign in to vote.
4.82/5 (36 votes)
17 Jan 2001CPOL 473.5K   14.3K   120   111
This article describes a class called CPicture that uses the IPicture class to draw pictures of almost any type to a CDC.

CPicture.JPG

This article describes a very easy to use class that enables you to draw an image of almost any type to the screen.

Another feature it demonstrates is using the resource to store the images. By using the static function GetResource in another context, you can very easily store Wave-files or document templates in the resource of your application. This example has three images stored in memory: Image 1 is a JPEG image, image 2 is a GIF image and image 3 is a bitmap.

By examining the example, it will be very clear how the class should be used. Below, the most important code is shown.

This example also implements the CMemDC written by Keith Rule to enhance the drawing. (FlickerFree Drawing).

// The function 'LoadFromBuffer' prepares the IPicture class.
// It requires a buffer filled with the contents of an image.

bool CPicture::LoadFromBuffer(BYTE* pBuff, int nSize)
{
    bool bResult = false;

    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
    void* pData = GlobalLock(hGlobal);
    memcpy(pData, pBuff, nSize);
    GlobalUnlock(hGlobal);

    IStream* pStream = NULL;

    if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
    {
        HRESULT hr;
        if ((hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture,
                                 (LPVOID *)&m_pPicture)) == S_OK)
            bResult = true;

        pStream->Release();
    }

    return bResult;
}
 
// This function draws the picture on the device context

bool CPicture::Draw(CDC* pDC, int x, int y, int cx, int cy)
{
    long hmWidth;
    long hmHeight;
    m_pPicture->get_Width(&hmWidth);
    m_pPicture->get_Height(&hmHeight);

    if (m_pPicture->Render(pDC->m_hDC, x, y, cx, cy, 0, 
                              hmHeight, hmWidth, -hmHeight, NULL) == S_OK)
        return true;

    return false;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAn improved version of this class Pin
W2k26-Sep-02 9:56
W2k26-Sep-02 9:56 
GeneralRe: An improved version of this class Pin
Emcee Lam10-Nov-02 14:14
Emcee Lam10-Nov-02 14:14 
GeneralRe: An improved version of this class Pin
W2k13-Nov-02 11:21
W2k13-Nov-02 11:21 
GeneralCan't load image from DLL Pin
13-Feb-02 5:25
suss13-Feb-02 5:25 
GeneralRe: Can't load image from DLL Pin
11-Apr-02 19:54
suss11-Apr-02 19:54 
Questionhow to make it run in activeX control Pin
26-Dec-01 5:53
suss26-Dec-01 5:53 
GeneralDoesn't support PNG as claimed. Pin
Robert Cao22-Oct-01 19:54
Robert Cao22-Oct-01 19:54 
GeneralRe: Doesn't support PNG as claimed. Pin
W2k21-Sep-02 2:54
W2k21-Sep-02 2:54 
If I'm not mistaken, PNG support was added in Microsoft Internet Explorer version 5, and the IPicture interface is part of MSIE. If you are running an older version of MSIE than 5 (or Windows than 2000/XP), you might need to upgrade in order to get PNG support.

Also, in a typical Windows app, there will be a number of #defines telling the compiler what Windows version-specific features are allowed, for example:
<br />
#ifndef WINVER						// Allow use of features specific to Windows 98 and Windows 2000 or later.<br />
#define WINVER 0x0500<br />
#endif<br />
<br />
#ifndef _WIN32_WINNT				// Allow use of features specific to Windows 98 and Windows 2000 or later.<br />
#define _WIN32_WINNT 0x0500	<br />
#endif						<br />
<br />
#ifndef _WIN32_WINDOWS				// Allow use of features specific to Windows 98 or later.<br />
#define _WIN32_WINDOWS 0x0410<br />
#endif<br />
<br />
#ifndef _WIN32_IE					// Allow use of features specific to IE 5.0 or later.<br />
#define _WIN32_IE 0x0500			<br />
#endif<br />

Other than that, I'm not sure what could be wrong. I hope this helps.

[ PlanetCPP ][ home of the n00blist ]
GeneralRe: Doesn't support PNG as claimed. Pin
zhou_wz13-Mar-03 23:31
zhou_wz13-Mar-03 23:31 
General->Release() will fail Pin
21-Oct-01 21:05
suss21-Oct-01 21:05 
GeneralDisplay & Edit Bitmap Pin
An Binh24-Sep-01 4:02
An Binh24-Sep-01 4:02 
GeneralAnimated Gif Pin
13-Sep-01 23:25
suss13-Sep-01 23:25 
GeneralIf jpg file is damaged... Pin
29-Aug-01 7:29
suss29-Aug-01 7:29 
GeneralRe: If jpg file is damaged... Pin
sortfish28-Feb-04 21:33
sortfish28-Feb-04 21:33 
GeneralThis project wont compile Pin
jody hull4-Aug-01 16:40
jody hull4-Aug-01 16:40 
GeneralRe: This project wont compile Pin
4-Aug-01 17:09
suss4-Aug-01 17:09 
GeneralRe: This project wont compile Pin
5-Aug-01 13:29
suss5-Aug-01 13:29 
GeneralAnimated Gif Pin
16-Jul-01 14:09
suss16-Jul-01 14:09 
GeneralRe: Animated Gif Pin
Hazem Nasereddin11-Jan-03 2:59
Hazem Nasereddin11-Jan-03 2:59 
GeneralRe: Animated Gif Pin
chenhuasheng18-Jun-03 1:55
chenhuasheng18-Jun-03 1:55 
GeneralRe: Animated Gif Pin
chenhuasheng18-Jun-03 1:55
chenhuasheng18-Jun-03 1:55 
QuestionIn MFC Dialogs??? Pin
5-Jul-01 18:31
suss5-Jul-01 18:31 
AnswerRe: In MFC Dialogs??? Pin
8-Aug-01 16:16
suss8-Aug-01 16:16 
AnswerRe: In MFC Dialogs??? Pin
9-Aug-01 0:34
suss9-Aug-01 0:34 
GeneralProblem saving images Pin
29-Jun-01 3:38
suss29-Jun-01 3:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.