Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFC VC9.0 VC VC++
I want to load PNG images. because png file may have transparent background. And I want to be replacing it with different png picture on any event.How can I display png images in a desired position on the dialog?
Posted 3-May-12 0:22am
chaiein1.8K
Edited 3-May-12 0:23am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

If you want to load from resource use below function.
bool Utils::LoadBitmapFromPNG(UINT uResourceID, CBitmap& BitmapOut)
{
    bool bRet = false;
    
    HINSTANCE hModuleInstance = AfxGetInstanceHandle();
    HRSRC hResourceHandle = ::FindResource(hModuleInstance, MAKEINTRESOURCE(uResourceID), L"PNG");
    if (0 == hResourceHandle)
    {
        return bRet;
    }
 
    DWORD nImageSize = ::SizeofResource(hModuleInstance, hResourceHandle);
    if (0 == nImageSize)
    {
        return bRet;
    }
 
    HGLOBAL hResourceInstance = ::LoadResource(hModuleInstance, hResourceHandle);
    if (0 == hResourceInstance)
    {
        return bRet;
    }
 
    const void* pResourceData = ::LockResource(hResourceInstance);
    if (0 == pResourceData)
    {
        FreeResource(hResourceInstance);
        return bRet;
    }
 
    HGLOBAL hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, nImageSize);
    if (0 == hBuffer)
    {
        FreeResource(hResourceInstance);
        return bRet;
    }
 
    void* pBuffer = ::GlobalLock(hBuffer);
    if (0 != pBuffer)
    {
        CopyMemory(pBuffer, pResourceData, nImageSize);
        IStream* pStream = 0;
        if (S_OK == ::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream))
        {
            CImage ImageFromResource;
            ImageFromResource.Load(pStream);
            pStream->Release();
            BitmapOut.Attach(ImageFromResource.Detach());
            bRet = true;
        }
        ::GlobalUnlock(hBuffer);
    }
    ::GlobalFree(hBuffer);
 
    UnlockResource(hResourceInstance);
    FreeResource(hResourceInstance);
 
    return bRet;
}
  Permalink  
Comments
CHill60 at 4-Mar-14 15:59pm
   
Post is nearly 2 years old and OP confirmed that solution 2 was the correct solution
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

With CImage[^] class.
  Permalink  
Comments
chaiein at 3-May-12 6:36am
   
how can i do without mouse or key event? that is using CRect
chaiein at 3-May-12 6:40am
   
Error 3 error C2065: 'CImage' : undeclared identifier
CPallini at 3-May-12 6:44am
   
From the documentation page:
"Requirements
Header: atlimage.h"
You have to include "atlimage.h"
chaiein at 3-May-12 6:49am
   
image.Load(_T("./res/acoustic grand piano_small.png"));
CDC* pDC = CDC::FromHandle(image.GetDC());
CBitmap bitmap;
bitmap.Attach(image.Detach());
pDC->SelectObject(bitmap);
 

image.ReleaseDC();
 
Please correct me there is some mistake and I need to give position to place the pic
CPallini at 3-May-12 7:51am
   
I would ask the authors:
http://msdn.microsoft.com/en-us/library/ms177536(VS.80).aspx
chaiein at 4-May-12 2:09am
   
Thank you all I solved the problem as in solution 2 :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
 
Try this code.
 
void CTestMFCDlg::OnBnClickedButtonLoadPng()
{
	CString pngPath=L"c:\\test.png";
    	CImage pngImage;
	CBitmap pngBmp;	
	CDC bmDC;
	CBitmap *pOldbmp;
	BITMAP  bi;
	UINT xPos=450,yPos=300;
 
	CClientDC dc(this);
	
    	pngImage.Load(pngPath);
	// new code
	
	pngBmp.Attach(pngImage.Detach());
	
	bmDC.CreateCompatibleDC(&dc);
 
	 pOldbmp= bmDC.SelectObject(&pngBmp);
	 pngBmp.GetBitmap(&bi);
	 dc.BitBlt(xPos,yPos,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);	 
	 bmDC.SelectObject(pOldbmp);	 
}
 
-Ganesh Jeevaa
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CHill60 360
1 OriginalGriff 343
2 Sarvesh Kumar Gupta 168
3 RyanDev 145
4 Sergey Alexandrovich Kryukov 128


Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 4 Mar 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid