Click here to Skip to main content
Licence CPOL
First Posted 17 Jan 2001
Views 339,758
Downloads 9,287
Bookmarked 106 times

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

By PeterHarrie | 17 Jan 2001
This article describes a class called CPicture that uses the IPicture class to draw pictures of almost any type to a CDC.
4 votes, 12.5%
1

2
2 votes, 6.3%
3
3 votes, 9.4%
4
23 votes, 71.9%
5
4.83/5 - 69 votes
4 removed
μ 4.28, σa 2.40 [?]

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 Narbonne4:06 3 Jun '10  
GeneralMy vote of 1 PinmemberPascalos7:28 27 Mar '09  
QuestionMemory Problem PinmemberAlexQII10:22 17 Sep '08  
GeneralI'm doing exactly what they write here and it doesn't work!!!! Pinmembersharolsh1:40 12 Mar '08  
Generalnew to this... PinmemberDon_Sartain19:14 30 Sep '06  
Generalword to tiff Pinmembershwlin21:43 23 May '06  
Generalword to tiff Pinmembershwlin21:38 23 May '06  
Questionhow to zoom in/out an image in c++ Builder? Pinmembertprelipceanu5:56 17 Apr '06  
AnswerRe: how to zoom in/out an image in c++ Builder? PinmemberPeter Hendrix21:42 17 Apr '06  
QuestionHow to reverse GIF animation ? Pinmemberferret-e5:43 23 Mar '06  
AnswerRe: How to reverse GIF animation ? PinmemberPeter Hendrix21:44 17 Apr '06  
QuestionHow to use on evc? Pinmemberrobert10145:08 16 Sep '05  
GeneralExcellent :) PinmemberAverk1:50 14 Jul '05  
QuestionHow to pu Draw in a Dialog PinmemberRSE Thomas3:53 7 Feb '05  
GeneralUsing it to SaveToBuffer PinmemberAlex Evans13:53 19 Dec '04  
QuestionNot supporting *.tif .......??????? Pinmembermahatma_cis22:24 16 Jul '04  
GeneralI want comments details... Pinmemberwindmood4:26 25 Jun '04  
Questioncan this class use in a basis dialog? Pinmemberzhangnan21:03 20 May '04  
QuestionSource code Teris Game? Pinmemberbuiquynghia4:29 16 May '04  
GeneralHelp - This draws the whole dialog box PinmemberGene Chan12:09 30 Mar '04  
GeneralBitmaps from toolbar button Pinmemberronaldog17:54 15 Mar '04  
GeneralProblem when reading a resource PinmemberBert Tuyt1:58 13 Sep '03  
Questiontiff? Pinsussitrainl4:49 13 Aug '03  
AnswerRe: tiff? Pinmembergilbreth10:43 19 Jun '07  
GeneralThank you very much Pinmemberhobiwan20: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
Web02 | 2.5.120210.1 | Last Updated 17 Jan 2001
Article Copyright 2001 by PeterHarrie
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid