Click here to Skip to main content
12,627,377 members (35,968 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ MFC VC9.0 VisualC++
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 1:22am
chaiein1.8K
Updated 3-May-12 1:23am
v2
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  
Comments
tischer8277 21-Jan-16 7:28am
   
This code helped me understand that original MFC does not operate natively with PNG files which contain transparency Alpha data, and that they have to be Detached and Attached to make them work. Fortunately, BCGSoft's modern MFC libraries have CBCGPToolBarImages that reads PNG files simply (Load) and directly with only a couple lines of code to implement. Transparent background is automatic.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

With CImage[^] class.
  Permalink  
Comments
chaiein 3-May-12 6:36am
   
how can i do without mouse or key event? that is using CRect
chaiein 3-May-12 6:40am
   
Error 3 error C2065: 'CImage' : undeclared identifier
CPallini 3-May-12 6:44am
   
From the documentation page:
"Requirements
Header: atlimage.h"
You have to include "atlimage.h"
chaiein 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 3-May-12 7:51am
   
I would ask the authors:
http://msdn.microsoft.com/en-us/library/ms177536(VS.80).aspx
chaiein 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 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 4-Mar-14 15:59pm
   
Post is nearly 2 years old and OP confirmed that solution 2 was the correct solution

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100