Click here to Skip to main content
15,868,340 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 471.6K   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 18:02
keyur_patel9-Feb-14 18:02 
GeneralThank you ... Pin
tao from Narbonne3-Jun-10 3:06
tao from Narbonne3-Jun-10 3:06 
GeneralMy vote of 1 Pin
Pascal Damman27-Mar-09 6:28
Pascal Damman27-Mar-09 6:28 
QuestionMemory Problem Pin
Mohammad Abedi17-Sep-08 9:22
Mohammad Abedi17-Sep-08 9:22 
GeneralI'm doing exactly what they write here and it doesn't work!!!! Pin
sharolsh12-Mar-08 0:40
sharolsh12-Mar-08 0:40 
Generalnew to this... Pin
Don_Sartain30-Sep-06 18:14
Don_Sartain30-Sep-06 18:14 
Generalword to tiff Pin
shwlin23-May-06 20:43
shwlin23-May-06 20:43 
Generalword to tiff Pin
shwlin23-May-06 20:38
shwlin23-May-06 20:38 
Questionhow to zoom in/out an image in c++ Builder? Pin
tprelipceanu17-Apr-06 4:56
tprelipceanu17-Apr-06 4:56 
AnswerRe: how to zoom in/out an image in c++ Builder? Pin
PeterHarrie17-Apr-06 20:42
PeterHarrie17-Apr-06 20:42 
QuestionHow to reverse GIF animation ? Pin
ferret-e23-Mar-06 4:43
ferret-e23-Mar-06 4:43 
AnswerRe: How to reverse GIF animation ? Pin
PeterHarrie17-Apr-06 20:44
PeterHarrie17-Apr-06 20:44 
QuestionHow to use on evc? Pin
Member 225629616-Sep-05 4:08
Member 225629616-Sep-05 4:08 
GeneralExcellent :) Pin
Averk14-Jul-05 0:50
Averk14-Jul-05 0:50 
QuestionHow to pu Draw in a Dialog Pin
RSE Thomas7-Feb-05 2:53
RSE Thomas7-Feb-05 2:53 
AnswerRe: How to pu Draw in a Dialog Pin
ztana28-Dec-13 15:54
ztana28-Dec-13 15:54 
GeneralUsing it to SaveToBuffer Pin
Alex Evans19-Dec-04 12:53
Alex Evans19-Dec-04 12:53 
QuestionNot supporting *.tif .......??????? Pin
mahatma_cis16-Jul-04 21:24
mahatma_cis16-Jul-04 21:24 
GeneralI want comments details... Pin
windmood25-Jun-04 3:26
windmood25-Jun-04 3:26 
Questioncan this class use in a basis dialog? Pin
zhangnan20-May-04 20:03
zhangnan20-May-04 20:03 
QuestionSource code Teris Game? Pin
buiquynghia16-May-04 3:29
buiquynghia16-May-04 3:29 
GeneralHelp - This draws the whole dialog box Pin
Member 65311230-Mar-04 11:09
Member 65311230-Mar-04 11:09 
Hi there,

This box draws the whole dialog box, regardless if the picture is that size(it fills the excess area with white space).

Can anyone help? I've looked into the IPicture class without avail.

Thanks,

Gene
GeneralBitmaps from toolbar button Pin
ronaldog15-Mar-04 16:54
ronaldog15-Mar-04 16:54 
GeneralProblem when reading a resource Pin
Bert Tuyt13-Sep-03 0:58
Bert Tuyt13-Sep-03 0:58 
Questiontiff? Pin
hyukdon13-Aug-03 3:49
hyukdon13-Aug-03 3: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.