CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2011
    Posts
    29

    How to change the height and width of a png image dynamically using vc++?

    Hi,

    I m trying to load a png image by giving the dimension of my own.

    CImage Image;
    Image.Load (L"D:\\Images\\PNG_Images\\Image7.png");

    But here what happens is that I m not able to give my own dimensions.My code requires constant changing of dimensions of the image.
    I tried using libpng.lib in my code.But I dono the keywords for loading the png image after including the lib.

    I m using Visual Studio 2010. I m developing in vc++.

    How do I load and change the height and width of a png image?

    Thankx a ton in advance....

    Regards,
    Sandhya.

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to change the height and width of a png image dynamically using vc++?

    No need of third-party library.
    Just use CImage::Draw method.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Apr 2011
    Posts
    29

    Re: How to change the height and width of a png image dynamically using vc++?

    I tried doing so..But no proper result.I tried both Draw and StretchBlt

    RECT bounds;
    bounds.top = 0;
    bounds.bottom = Height;
    bounds.left = 0;
    bounds.right = Width;

    CBitmap bitmap;
    CImage Image;
    Image.Load (L"D:\\Images\\PNG_Images\\Image7.png");
    HDC hdcDst;
    HWND VIEWWER = GetDlgItem(m_hWnd,DLG_VideoViewer);
    HDC V_Hdc = GetDC(VIEWWER);
    hdcDst = CreateCompatibleDC(V_Hdc) ;

    //Image.Draw(hdcDst,0,0,Width,Height);

    Image.StretchBlt(hdcDst,bounds,bounds,SRCCOPY);

    bitmap.Attach (Image);


    Only the size of the rectangle change.
    My original size of the png image is 1024x980.
    The value of height and width i m feeding onto CImage is varying ;for example 128x128.
    I tried even giving Source and Dest values for StretchBlt.

    int O_Height = Image.GetHeight();
    int O_Width = Image.GetWidth();
    Image.StretchBlt(hdcDst,0,0,Width,Height,0,0,O_Height ,O_Width ,SRCCOPY);


    I m getting the outer boundary with the dimension i ve mentioned. But the image is still the same Eg, 1024x980. So I m gettin only a small part of the image on the screen.

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to change the height and width of a png image dynamically using vc++?

    There is not very clear which is the destination of the scaled image.

    If you want to draw it in the window, then it's very simple.
    Here is a simple example that stretches the original image to window client area, by using CImage::Draw:
    Code:
    void CTestView::OnDraw(CDC* pDC)
    {
       CTestDoc* pDoc = GetDocument();
       ASSERT_VALID(pDoc);
       CImage& image = pDoc->GetImage(); // get reference to CTestDoc::m_image
       if(!image.IsNull())
       {
          // stretch image to client area
          CRect rcClient;
          GetClientRect(rcClient);
          CRect rcImage(0, 0, image.GetWidth(), image.GetHeight());
          pDC->SetStretchBltMode(HALFTONE);
          image.Draw(pDC->m_hDC, rcClient, rcImage);
       }
    }
    If you want to scale the original image and attach it to another CImage object, then the folowing code would work...
    Code:
       // ...
       const int nSrcWidth = imageSrc.GetWidth();
       const int nSrcHeight = imageSrc.GetHeight();
       const int nBPP = imageSrc.GetBPP();
       const int nDestWidth = nSrcWidth / 4; // NOTE: hard-coded values, just for demo purpose
       const int nDestHeight = nSrcHeight / 4;
    
       if(!imageDest.IsNull())
          imageDest.Destroy();
       imageDest.Create(nDestWidth, nDestHeight, nBPP);
       imageSrc.Draw(imageDest.GetDC(), 0, 0, nDestWidth, nDestHeight,
          0, 0, nSrcWidth, nSrcHeight);
       // ...
    ...but it doesn't. Probably, it's a bug in CImage impelmentation (at least in the version I have).
    After calling CImage::GetDC, there is no further possible to detach/destroy the CImage object.
    Or maybe I'm wrong...

    Anyway, the classic method of using a memory device context can fix the problem.
    Example:
    Code:
       CTestDoc* pDoc = GetDocument();
       ASSERT_VALID(pDoc);
       CImage& imageSrc = pDoc->GetImage();
       CImage& imageDest = pDoc->GetScaledImage();
       CBitmap& bitmap = pDoc->GetBitmap();
    
       const int nSrcWidth = imageSrc.GetWidth();
       const int nSrcHeight = imageSrc.GetHeight();
       const int nDestWidth = nSrcWidth / 4;
       const int nDestHeight = nSrcHeight / 4;
    
       if(NULL != bitmap.GetSafeHandle())
          bitmap.DeleteObject();
    
       if(!imageDest.IsNull())
          imageDest.Destroy();
    
       CDC dcMem;
       dcMem.CreateCompatibleDC(pDC);
       bitmap.CreateCompatibleBitmap(pDC, nDestWidth, nDestHeight);
       CBitmap* pOldBitmap = dcMem.SelectObject(&bitmap);
    
       dcMem.SetStretchBltMode(HALFTONE);
       imageSrc.Draw(dcMem.m_hDC, 0, 0, nDestWidth, nDestHeight,
          0, 0, nSrcWidth, nSrcHeight);
       dcMem.SelectObject(pOldBitmap);
    
       imageDest.Attach(bitmap);
       // ...
    Last edited by ovidiucucu; March 5th, 2013 at 07:48 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to change the height and width of a png image dynamically using vc++?

    Additional remarks.
    Having again a look in your code, I've noticed that you are trying to draw in a control from its parent window class.
    Generally, do not draw in a window (control or anything else) in other place than in its own WM_PAINT message handler.
    Otherwise, next question would be "why my control isn't repaint if...?".
    So, if let's say you are using a static control, derive from CStatic class then map WM_PAINT to perform drawing inside its handler.

    And do not load the image from file each time you have to paint it.

    And probably, an additional CImage object for keeping the scaled image isn't necessary. Just draw directly into the control's client area, using a code similar to the one I have shown in my first example. Keep the rest just for example...
    Last edited by ovidiucucu; March 5th, 2013 at 08:28 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured