Click here to Skip to main content
Click here to Skip to main content

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

Comments and Discussions

Questionm_pPicture->get_Width(&hmWidth); not working Pinmemberkeyur_patel9-Feb-14 19:02 
GeneralThank you ... Pinmembertao from Narbonne3-Jun-10 4:06 
GeneralMy vote of 1 PinmemberPascalos27-Mar-09 7:28 
QuestionMemory Problem PinmemberAlexQII17-Sep-08 10:22 
GeneralI'm doing exactly what they write here and it doesn't work!!!! Pinmembersharolsh12-Mar-08 1:40 
Generalnew to this... PinmemberDon_Sartain30-Sep-06 19:14 
Generalword to tiff Pinmembershwlin23-May-06 21:43 
Generalword to tiff Pinmembershwlin23-May-06 21:38 
Questionhow to zoom in/out an image in c++ Builder? Pinmembertprelipceanu17-Apr-06 5:56 
AnswerRe: how to zoom in/out an image in c++ Builder? PinmemberPeter Hendrix17-Apr-06 21:42 
QuestionHow to reverse GIF animation ? Pinmemberferret-e23-Mar-06 5:43 
AnswerRe: How to reverse GIF animation ? PinmemberPeter Hendrix17-Apr-06 21:44 
QuestionHow to use on evc? Pinmemberrobert101416-Sep-05 5:08 
GeneralExcellent :) PinmemberAverk14-Jul-05 1:50 
QuestionHow to pu Draw in a Dialog PinmemberRSE Thomas7-Feb-05 3:53 
AnswerRe: How to pu Draw in a Dialog Pinmemberztana28-Dec-13 16:54 
GeneralUsing it to SaveToBuffer PinmemberAlex Evans19-Dec-04 13:53 
QuestionNot supporting *.tif .......??????? Pinmembermahatma_cis16-Jul-04 22:24 
GeneralI want comments details... Pinmemberwindmood25-Jun-04 4:26 
Questioncan this class use in a basis dialog? Pinmemberzhangnan20-May-04 21:03 
QuestionSource code Teris Game? Pinmemberbuiquynghia16-May-04 4:29 
GeneralHelp - This draws the whole dialog box PinmemberGene Chan30-Mar-04 12:09 
GeneralBitmaps from toolbar button Pinmemberronaldog15-Mar-04 17:54 
GeneralProblem when reading a resource PinmemberBert Tuyt13-Sep-03 1:58 
Questiontiff? Pinsussitrainl13-Aug-03 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.141220.1 | Last Updated 17 Jan 2001
Article Copyright 2001 by PeterHarrie
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid