Click here to Skip to main content
Licence CPOL
First Posted 17 Jan 2001
Views 345,697
Downloads 9,831
Bookmarked 107 times

Simple class for drawing pictures (JPG, TIFF, GIF, etc...)

By | 17 Jan 2001 | Article
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)

About the Author

PeterHarrie

Web Developer

Netherlands Netherlands

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThank you ... Pinmembertao from Narbonne3:06 3 Jun '10  
GeneralMy vote of 1 PinmemberPascalos6:28 27 Mar '09  
QuestionMemory Problem PinmemberAlexQII9:22 17 Sep '08  
GeneralI'm doing exactly what they write here and it doesn't work!!!! Pinmembersharolsh0:40 12 Mar '08  
Generalnew to this... PinmemberDon_Sartain18:14 30 Sep '06  
Generalword to tiff Pinmembershwlin20:43 23 May '06  
Generalword to tiff Pinmembershwlin20:38 23 May '06  
Questionhow to zoom in/out an image in c++ Builder? Pinmembertprelipceanu4:56 17 Apr '06  
AnswerRe: how to zoom in/out an image in c++ Builder? PinmemberPeter Hendrix20:42 17 Apr '06  
QuestionHow to reverse GIF animation ? Pinmemberferret-e4:43 23 Mar '06  
AnswerRe: How to reverse GIF animation ? PinmemberPeter Hendrix20:44 17 Apr '06  
QuestionHow to use on evc? Pinmemberrobert10144:08 16 Sep '05  
GeneralExcellent :) PinmemberAverk0:50 14 Jul '05  
QuestionHow to pu Draw in a Dialog PinmemberRSE Thomas2:53 7 Feb '05  
GeneralUsing it to SaveToBuffer PinmemberAlex Evans12:53 19 Dec '04  
QuestionNot supporting *.tif .......??????? Pinmembermahatma_cis21:24 16 Jul '04  
GeneralI want comments details... Pinmemberwindmood3:26 25 Jun '04  
Questioncan this class use in a basis dialog? Pinmemberzhangnan20:03 20 May '04  
QuestionSource code Teris Game? Pinmemberbuiquynghia3:29 16 May '04  
GeneralHelp - This draws the whole dialog box PinmemberGene Chan11:09 30 Mar '04  
GeneralBitmaps from toolbar button Pinmemberronaldog16:54 15 Mar '04  
GeneralProblem when reading a resource PinmemberBert Tuyt0:58 13 Sep '03  
Questiontiff? Pinsussitrainl3:49 13 Aug '03  
AnswerRe: tiff? Pinmembergilbreth9:43 19 Jun '07  
GeneralThank you very much Pinmemberhobiwan19:19 24 Jun '03  

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.

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