Click here to Skip to main content
15,845,565 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 470.9K   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

 
Questionm_pPicture->get_Width(&hmWidth); not working Pin
keyur_patel9-Feb-14 19:02
keyur_patel9-Feb-14 19:02 
GeneralThank you ... Pin
tao from Narbonne3-Jun-10 4:06
tao from Narbonne3-Jun-10 4:06 
GeneralMy vote of 1 Pin
Pascal Damman27-Mar-09 7:28
Pascal Damman27-Mar-09 7:28 
QuestionMemory Problem Pin
Mohammad Abedi17-Sep-08 10:22
Mohammad Abedi17-Sep-08 10:22 
GeneralI'm doing exactly what they write here and it doesn't work!!!! Pin
sharolsh12-Mar-08 1:40
sharolsh12-Mar-08 1:40 
Generalnew to this... Pin
Don_Sartain30-Sep-06 19:14
Don_Sartain30-Sep-06 19:14 
Generalword to tiff Pin
shwlin23-May-06 21:43
shwlin23-May-06 21:43 
Generalword to tiff Pin
shwlin23-May-06 21:38
shwlin23-May-06 21:38 
Questionhow to zoom in/out an image in c++ Builder? Pin
tprelipceanu17-Apr-06 5:56
tprelipceanu17-Apr-06 5:56 
AnswerRe: how to zoom in/out an image in c++ Builder? Pin
PeterHarrie17-Apr-06 21:42
PeterHarrie17-Apr-06 21:42 
QuestionHow to reverse GIF animation ? Pin
ferret-e23-Mar-06 5:43
ferret-e23-Mar-06 5:43 
AnswerRe: How to reverse GIF animation ? Pin
PeterHarrie17-Apr-06 21:44
PeterHarrie17-Apr-06 21:44 
QuestionHow to use on evc? Pin
Member 225629616-Sep-05 5:08
Member 225629616-Sep-05 5:08 
GeneralExcellent :) Pin
Averk14-Jul-05 1:50
Averk14-Jul-05 1:50 
QuestionHow to pu Draw in a Dialog Pin
RSE Thomas7-Feb-05 3:53
RSE Thomas7-Feb-05 3:53 
AnswerRe: How to pu Draw in a Dialog Pin
ztana28-Dec-13 16:54
ztana28-Dec-13 16:54 
GeneralUsing it to SaveToBuffer Pin
Alex Evans19-Dec-04 13:53
Alex Evans19-Dec-04 13:53 
QuestionNot supporting *.tif .......??????? Pin
mahatma_cis16-Jul-04 22:24
mahatma_cis16-Jul-04 22:24 
GeneralI want comments details... Pin
windmood25-Jun-04 4:26
windmood25-Jun-04 4:26 
Questioncan this class use in a basis dialog? Pin
zhangnan20-May-04 21:03
zhangnan20-May-04 21:03 
QuestionSource code Teris Game? Pin
buiquynghia16-May-04 4:29
buiquynghia16-May-04 4:29 
GeneralHelp - This draws the whole dialog box Pin
Member 65311230-Mar-04 12:09
Member 65311230-Mar-04 12:09 
GeneralBitmaps from toolbar button Pin
ronaldog15-Mar-04 17:54
ronaldog15-Mar-04 17:54 
GeneralProblem when reading a resource Pin
Bert Tuyt13-Sep-03 1:58
Bert Tuyt13-Sep-03 1:58 
Questiontiff? Pin
hyukdon13-Aug-03 4:49
hyukdon13-Aug-03 4:49 

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.