Click here to Skip to main content
12,951,983 members (68,350 online)
Click here to Skip to main content
Add your own
alternative version


118 bookmarked
Posted 17 Jan 2001

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

QuestionMessage Removed Pin
Member 367588517-Mar-17 20:39
memberMember 367588517-Mar-17 20:39 
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 
QuestionHow to reverse GIF animation ? Pin
ferret-e23-Mar-06 4:43
memberferret-e23-Mar-06 4:43 
AnswerRe: How to reverse GIF animation ? Pin
Peter Hendrix17-Apr-06 20:44
memberPeter Hendrix17-Apr-06 20:44 
QuestionHow to use on evc? Pin
robert101416-Sep-05 4:08
memberrobert101416-Sep-05 4:08 
GeneralExcellent :) Pin
Averk14-Jul-05 0:50
memberAverk14-Jul-05 0:50 
QuestionHow to pu Draw in a Dialog Pin
RSE Thomas7-Feb-05 2:53
memberRSE Thomas7-Feb-05 2:53 
AnswerRe: How to pu Draw in a Dialog Pin
ztana28-Dec-13 15:54
memberztana28-Dec-13 15:54 
GeneralUsing it to SaveToBuffer Pin
Alex Evans19-Dec-04 12:53
memberAlex Evans19-Dec-04 12:53 
QuestionNot supporting *.tif .......??????? Pin
mahatma_cis16-Jul-04 21:24
membermahatma_cis16-Jul-04 21:24 
GeneralI want comments details... Pin
windmood25-Jun-04 3:26
memberwindmood25-Jun-04 3:26 
Questioncan this class use in a basis dialog? Pin
zhangnan20-May-04 20:03
memberzhangnan20-May-04 20:03 
QuestionSource code Teris Game? Pin
buiquynghia16-May-04 3:29
memberbuiquynghia16-May-04 3:29 
GeneralHelp - This draws the whole dialog box Pin
Gene Chan30-Mar-04 11:09
memberGene Chan30-Mar-04 11:09 
GeneralBitmaps from toolbar button Pin
ronaldog15-Mar-04 16:54
memberronaldog15-Mar-04 16:54 
GeneralProblem when reading a resource Pin
Bert Tuyt13-Sep-03 0:58
memberBert Tuyt13-Sep-03 0:58 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 17 Jan 2001
Article Copyright 2001 by PeterHarrie
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid