Click here to Skip to main content
12,395,577 members (58,580 online)
Click here to Skip to main content
Add your own
alternative version


117 bookmarked

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

, 17 Jan 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes a class called CPicture that uses the IPicture class to draw pictures of almost any type to a CDC.


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);

    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;


    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;

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

    return false;


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


About the Author

Web Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

Questionm_pPicture->get_Width(&hmWidth); not working Pin
keyur_patel9-Feb-14 18:02
memberkeyur_patel9-Feb-14 18:02 
GeneralThank you ... Pin
tao from Narbonne3-Jun-10 3:06
membertao from Narbonne3-Jun-10 3:06 
GeneralMy vote of 1 Pin
Pascalos27-Mar-09 6:28
memberPascalos27-Mar-09 6:28 
QuestionMemory Problem Pin
AlexQII17-Sep-08 9:22
memberAlexQII17-Sep-08 9:22 
GeneralI'm doing exactly what they write here and it doesn't work!!!! Pin
sharolsh12-Mar-08 0:40
membersharolsh12-Mar-08 0:40 
Generalnew to this... Pin
Don_Sartain30-Sep-06 18:14
memberDon_Sartain30-Sep-06 18:14 
Generalword to tiff Pin
shwlin23-May-06 20:43
membershwlin23-May-06 20:43 
Generalword to tiff Pin
shwlin23-May-06 20:38
membershwlin23-May-06 20:38 
Questionhow to zoom in/out an image in c++ Builder? Pin
tprelipceanu17-Apr-06 4:56
membertprelipceanu17-Apr-06 4:56 
AnswerRe: how to zoom in/out an image in c++ Builder? Pin
Peter Hendrix17-Apr-06 20:42
memberPeter Hendrix17-Apr-06 20:42 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 17 Jan 2001
Article Copyright 2001 by PeterHarrie
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid