Click here to Skip to main content
6,292,426 members and growing! (10,649 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Bitmaps     Intermediate License: The Code Project Open License (CPOL)

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

By Peter Hendrix

This article describes a class called CPicture that uses the IPicture class to draw pictures of almost any type to a CDC.
VC6Win2K, Visual Studio, MFC, Dev
Posted:17 Jan 2001
Views:286,948
Bookmarked:84 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
68 votes for this article.
Popularity: 7.52 Rating: 4.10 out of 5
5 votes, 16.1%
1

2
2 votes, 6.5%
3
3 votes, 9.7%
4
21 votes, 67.7%
5

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

Peter Hendrix


Member
Our company makes hardware and software for telecommunication solutions.
We maintain a very large public telephony network in Germany.
My task is to create fast and new revolutionary software to keep our network scalable and robust.
Occupation: Web Developer
Location: Netherlands Netherlands

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 108 (Total in Forum: 108) (Refresh)FirstPrevNext
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  
Generalhow to zoom in/out an image in c++ Builder? Pinmembertprelipceanu5:56 17 Apr '06  
GeneralRe: how to zoom in/out an image in c++ Builder? PinmemberPeter Hendrix21:42 17 Apr '06  
GeneralHow to reverse GIF animation ? Pinmemberferret-e5:43 23 Mar '06  
GeneralRe: 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  
GeneralHow to pu Draw in a Dialog PinmemberRSE Thomas3:53 7 Feb '05  
GeneralUsing it to SaveToBuffer PinmemberAlex Evans13:53 19 Dec '04  
GeneralNot supporting *.tif .......??????? Pinmembermahatma_cis22:24 16 Jul '04  
GeneralI want comments details... Pinmemberwindmood4:26 25 Jun '04  
Generalcan this class use in a basis dialog? Pinmemberzhangnan21:03 20 May '04  
GeneralSource 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  
Generaltiff? Pinsussitrainl4:49 13 Aug '03  
AnswerRe: tiff? Pinmembergilbreth10:43 19 Jun '07  
GeneralThank you very much Pinmemberhobiwan20:19 24 Jun '03  
Generaltiff Pinmemberkais0822:13 13 Feb '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Jan 2001
Editor: Sean Ewington
Copyright 2001 by Peter Hendrix
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project