Click here to Skip to main content
15,920,956 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.7K   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

 
GeneralRendering problems Pin
23-May-01 0:47
suss23-May-01 0:47 
GeneralRe: Rendering problems Pin
23-May-01 4:22
suss23-May-01 4:22 
GeneralBeginner problem Pin
5-May-01 15:18
suss5-May-01 15:18 
GeneralLack of saving image Pin
4-Apr-01 3:40
suss4-Apr-01 3:40 
GeneralRe: Lack of saving image Pin
20-Apr-01 9:19
suss20-Apr-01 9:19 
GeneralIt will be more easyer Pin
6-Mar-01 3:10
suss6-Mar-01 3:10 
GeneralSuperb Pin
NormDroid23-Feb-01 4:23
professionalNormDroid23-Feb-01 4:23 
GeneralPicture that adjusts itself to the DC size Pin
21-Feb-01 19:53
suss21-Feb-01 19:53 
Hi Peter,

Excellent class. I was looking for something just like that! It made my life a lot easier.Cool | :cool:
I added some code to one of the Draw()-functions, so the clients of this class don't need to calculate the ratio if the picture is bigger than the size of the device context. Hope you like it.

In the header file, add a default parameter to the draw function:

//bool Draw(CDC* pDC);
bool Draw(CDC* pDC, BOOL bUseDCExtends = FALSE );


In the cpp file, add the following code (and also the default parameter):

bool CPicture::Draw(CDC* pDC, BOOL bUseDCExtends /* FALSE */)
{
   if (m_pPicture != NULL)
   {
      long hmWidth;
      long hmHeight;
      m_pPicture->get_Width(&hmWidth);
      m_pPicture->get_Height(&hmHeight);

      int nWidth = MulDiv(hmWidth, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH);
      int nHeight = MulDiv(hmHeight, pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH);


      // Added this code to adjust the picture size to te extends of the
      // device context (Only do so if picture is bigger than device context window!
      //
      if( bUseDCExtends != FALSE )
      {
         CRect    rect;
         double   dMinRatio;
         double   dWidthRatio;
         double   dHeightRatio;

         pDC->GetWindow()->GetWindowRect( &rect );

         dWidthRatio = (double)rect.Width() /  nWidth;
         dHeightRatio = (double)rect.Height() / nHeight;

         // get smallest ratio
         dMinRatio = ( dWidthRatio < dHeightRatio )? dWidthRatio: dHeightRatio;

         // if picture is bigger than device context, use ratio!
         if( dMinRatio < 1 )
         {
            return Draw( pDC, dMinRatio );
         }
      }

      return Draw(pDC, 0, 0, nWidth, nHeight);
  }
  return false;
}



Of course you can also add the same code in the function Draw( CDC *pDC, CPoint Pos),
but then you should adjust the calculation of dWidthRatio and dHeightRatio, by extracting the CPoint-values from the width and height of the WindowRect.

Have Fun!
QuestionTif files ? Other file formats ? Pin
22-Jan-01 23:28
suss22-Jan-01 23:28 
GeneralConvert to UNICODE Pin
20-Jan-01 9:02
suss20-Jan-01 9:02 
GeneralRe: Convert to UNICODE Pin
Masoud Samimi21-Jan-01 10:23
Masoud Samimi21-Jan-01 10:23 
GeneralRe: Convert to UNICODE Pin
Chris Losinger22-Jan-01 3:16
professionalChris Losinger22-Jan-01 3:16 
GeneralRe: Convert to UNICODE Pin
Masoud Samimi22-Jan-01 3:56
Masoud Samimi22-Jan-01 3:56 
GeneralRe: Convert to UNICODE Pin
23-Jan-01 0:24
suss23-Jan-01 0:24 
GeneralRe: Convert to UNICODE Pin
Masoud Samimi23-Jan-01 1:22
Masoud Samimi23-Jan-01 1:22 
GeneralRe: Convert to UNICODE Pin
Chris Losinger24-Jan-01 8:43
professionalChris Losinger24-Jan-01 8:43 
GeneralRe: Convert to UNICODE Pin
Chris Losinger24-Jan-01 8:42
professionalChris Losinger24-Jan-01 8:42 
GeneralRe: Convert to UNICODE Pin
4-Aug-01 17:20
suss4-Aug-01 17:20 
GeneralAgain.. no GIF without a Unisys License Pin
Chris Losinger19-Jan-01 9:56
professionalChris Losinger19-Jan-01 9:56 
GeneralRe: Again.. no GIF without a Unisys License Pin
19-Jan-01 10:07
suss19-Jan-01 10:07 
GeneralRe: Again.. no GIF without a Unisys License Pin
19-Jan-01 10:32
suss19-Jan-01 10:32 
GeneralRe: Again.. no GIF without a Unisys License Pin
19-Jan-01 10:39
suss19-Jan-01 10:39 
GeneralRe: Again.. no GIF without a Unisys License Pin
19-Jan-01 10:41
suss19-Jan-01 10:41 
GeneralRe: Again.. no GIF without a Unisys License Pin
19-Jan-01 10:45
suss19-Jan-01 10:45 
GeneralRe: Again.. no GIF without a Unisys License Pin
22-Jan-01 12:12
suss22-Jan-01 12:12 

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.