Click here to Skip to main content
15,919,245 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 475.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

 
GeneralRe: Again.. no GIF without a Unisys License Pin
20-Jan-01 20:24
suss20-Jan-01 20:24 
GeneralRe: Again.. no GIF without a Unisys License Pin
Chris Losinger21-Jan-01 7:55
professionalChris Losinger21-Jan-01 7:55 
GeneralRe: Again.. no GIF without a Unisys License Pin
David Wulff21-Jan-01 9:43
David Wulff21-Jan-01 9:43 
GeneralA few questions/remarks regarding this issue Pin
PeterHarrie19-Jan-01 23:16
PeterHarrie19-Jan-01 23:16 
GeneralRe: A few questions/remarks regarding this issue Pin
19-Jan-01 23:33
suss19-Jan-01 23:33 
GeneralRe: A few questions/remarks regarding this issue Pin
Chris Losinger20-Jan-01 7:56
professionalChris Losinger20-Jan-01 7:56 
GeneralRe: A few questions/remarks regarding this issue Pin
Colin J Davies21-Jan-01 8:52
Colin J Davies21-Jan-01 8:52 
GeneralRe: Again.. no GIF without a Unisys License Pin
20-Jan-01 23:31
suss20-Jan-01 23:31 
found this at:

http://prtr-13.ucsc.edu/~badger/software/libungif/

---
Libungif, a library for using uncompressed GIFs.

What is libungif?
Once upon a time, Gershon Elbor and Eric Raymond wrote a freely available library called giflib. This library provided a means for software writers to quickly add GIF file manipulation to their programs.

Unfortunately, in that time there was an entity known as Unisys which saw that the GIF image format used a bit of knowledge known as LZW compression which belonged to them. Eager to advance their rights, they let it be known that they would not have use of their knowledge be infringed. All those who used or distributed software with LZW (and therefore GIF) compression algorithms must apply for licenses or cease forthwith.

Many programmers did cease their efforts with GIF. Questing for a new image to succeed the old, they created the PNG image format, a superior format capable of many things that GIF could never do. However, the legacy of GIF remained; many images remained in that format, still needing to be read and written and converted. Some software remained that could make only gibberish of PNG and yet resorted to GIFs for their graphic needs.

So it came to pass that libungif was born, a library which could be called in the same manner as giflib, but created uncompressed gifs instead of ones encoded with LZW. This allowed programmers to write software that was capable of talking to either giflib or libungif during the transition from gif to png.
---


GeneralRe: Again.. no GIF without a Unisys License Pin
22-Jan-01 13:02
suss22-Jan-01 13:02 
GeneralRe: Again.. no GIF without a Unisys License Pin
22-Jan-01 13:13
suss22-Jan-01 13:13 
QuestionHow to creating Half size bitmap from any Bitmap?? Pin
19-Jan-01 2:10
suss19-Jan-01 2:10 
AnswerRe: How to creating Half size bitmap from any Bitmap?? Pin
PeterHarrie19-Jan-01 2:31
PeterHarrie19-Jan-01 2:31 
AnswerRe: How to creating Half size bitmap from any Bitmap?? Pin
Skarrin25-Jan-01 1:30
Skarrin25-Jan-01 1:30 
QuestionWhat about animated GIF's Pin
Remon Spekreijse18-Jan-01 22:30
Remon Spekreijse18-Jan-01 22:30 
AnswerRe: What about animated GIF's Pin
PeterHarrie18-Jan-01 22:38
PeterHarrie18-Jan-01 22:38 
GeneralThe source is here: Pin
Skarrin25-Jan-01 1:26
Skarrin25-Jan-01 1:26 

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.