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.

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)

Share

About the Author

PeterHarrie
Web Developer
Netherlands Netherlands
No Biography provided

Comments and Discussions

 
Questionm_pPicture->get_Width(&hmWidth); not working Pinmemberkeyur_patel9-Feb-14 18:02 
GeneralThank you ... Pinmembertao from Narbonne3-Jun-10 3:06 
GeneralMy vote of 1 PinmemberPascalos27-Mar-09 6:28 
QuestionMemory Problem PinmemberAlexQII17-Sep-08 9:22 
GeneralI'm doing exactly what they write here and it doesn't work!!!! Pinmembersharolsh12-Mar-08 0:40 
Generalnew to this... PinmemberDon_Sartain30-Sep-06 18:14 
Generalword to tiff Pinmembershwlin23-May-06 20:43 
Generalword to tiff Pinmembershwlin23-May-06 20:38 
Questionhow to zoom in/out an image in c++ Builder? Pinmembertprelipceanu17-Apr-06 4:56 
AnswerRe: how to zoom in/out an image in c++ Builder? PinmemberPeter Hendrix17-Apr-06 20:42 
QuestionHow to reverse GIF animation ? Pinmemberferret-e23-Mar-06 4:43 
AnswerRe: How to reverse GIF animation ? PinmemberPeter Hendrix17-Apr-06 20:44 
QuestionHow to use on evc? Pinmemberrobert101416-Sep-05 4:08 
GeneralExcellent :) PinmemberAverk14-Jul-05 0:50 
QuestionHow to pu Draw in a Dialog PinmemberRSE Thomas7-Feb-05 2:53 
AnswerRe: How to pu Draw in a Dialog Pinmemberztana28-Dec-13 15:54 
GeneralUsing it to SaveToBuffer PinmemberAlex Evans19-Dec-04 12:53 
QuestionNot supporting *.tif .......??????? Pinmembermahatma_cis16-Jul-04 21:24 
GeneralI want comments details... Pinmemberwindmood25-Jun-04 3:26 
Questioncan this class use in a basis dialog? Pinmemberzhangnan20-May-04 20:03 
QuestionSource code Teris Game? Pinmemberbuiquynghia16-May-04 3:29 
GeneralHelp - This draws the whole dialog box PinmemberGene Chan30-Mar-04 11:09 
GeneralBitmaps from toolbar button Pinmemberronaldog15-Mar-04 16:54 
GeneralProblem when reading a resource PinmemberBert Tuyt13-Sep-03 0:58 
Questiontiff? Pinsussitrainl13-Aug-03 3:49 
AnswerRe: tiff? Pinmembergilbreth19-Jun-07 9:43 
GeneralThank you very much Pinmemberhobiwan24-Jun-03 19:19 
Generaltiff Pinmemberkais0813-Feb-03 21:13 
GeneralTIFF Pinsusskaeschowa27-Jan-03 3:47 
AnswerRe: TIFF Pinmember98701823714-Nov-08 3:03 
GeneralAccess Violation PinsussSubrahmanyam6-Nov-02 23:52 
GeneralImage quality on 16-bit color display PinmemberSubrahmanyam29-Oct-02 19:51 
GeneralAn improved version of this class PinmemberW2k26-Sep-02 9:56 
GeneralRe: An improved version of this class PinmemberEmcee Lam10-Nov-02 14:14 
GeneralRe: An improved version of this class PinmemberW2k13-Nov-02 11:21 
GeneralCan't load image from DLL PinmemberAnonymous13-Feb-02 5:25 
GeneralRe: Can't load image from DLL PinmemberAnonymous11-Apr-02 19:54 
Questionhow to make it run in activeX control Pinmemberhihiforever26-Dec-01 5:53 
GeneralDoesn't support PNG as claimed. PinmemberRobert Cao22-Oct-01 19:54 
GeneralRe: Doesn't support PNG as claimed. PinmemberW2k21-Sep-02 2:54 
GeneralRe: Doesn't support PNG as claimed. Pinmemberwz_zhou13-Mar-03 23:31 
General->Release() will fail Pinmembercp21-Oct-01 21:05 
GeneralDisplay & Edit Bitmap PinmemberAn Binh24-Sep-01 4:02 
GeneralAnimated Gif PinmemberILX13-Sep-01 23:25 
GeneralIf jpg file is damaged... PinmemberJordy_wei29-Aug-01 7:29 
GeneralRe: If jpg file is damaged... Pinmembersortfish28-Feb-04 21:33 
GeneralThis project wont compile Pinmemberjody hull4-Aug-01 16:40 
GeneralRe: This project wont compile PinmemberAnonymous4-Aug-01 17:09 
GeneralRe: This project wont compile Pinmemberjody hull5-Aug-01 13:29 
GeneralAnimated Gif Pinmembersrinivas vaithianathan16-Jul-01 14:09 

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 | Mobile
Web03 | 2.8.141029.1 | Last Updated 17 Jan 2001
Article Copyright 2001 by PeterHarrie
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid