Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to load a png image in PictureControl?

How to load a png image in PictureControl?

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearning
6 Posts 3 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VCProgrammer
    wrote on last edited by
    #1

    hi all, i want to load a png image from resource in picture control. how can i do this. thanks.

    L Y 2 Replies Last reply
    0
    • V VCProgrammer

      hi all, i want to load a png image from resource in picture control. how can i do this. thanks.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Do you mean via the LoadResource function[^]?

      1 Reply Last reply
      0
      • V VCProgrammer

        hi all, i want to load a png image from resource in picture control. how can i do this. thanks.

        Y Offline
        Y Offline
        yu jian
        wrote on last edited by
        #3

        #include "atlimage.h"

        Image* m_pImage;

        char *pStr = strTempIMGPath.GetBuffer(0);
        int size = MultiByteToWideChar(GetACP(), 0, pStr, -1, NULL, 0);
        MultiByteToWideChar(GetACP(), 0, pStr, -1, szFile, size);
        strTempIMGPath.ReleaseBuffer();
        if (m_pImage != NULL)
        {
        delete m_pImage;
        m_pImage = NULL;
        }
        m_pImage = new Image(szFile, FALSE);

        void CPNGButton::OnPaint()
        {
        CPaintDC dc(this);
        Graphics graphics(dc.m_hDC);
        if(m_pImage != NULL && (m_pImage->GetLastStatus() == Gdiplus::Status::Ok))
        {
        graphics.DrawImage(m_pImage, 0, 0, m_pImage->GetWidth(), m_pImage->GetHeight());
        }
        }

        V 1 Reply Last reply
        0
        • Y yu jian

          #include "atlimage.h"

          Image* m_pImage;

          char *pStr = strTempIMGPath.GetBuffer(0);
          int size = MultiByteToWideChar(GetACP(), 0, pStr, -1, NULL, 0);
          MultiByteToWideChar(GetACP(), 0, pStr, -1, szFile, size);
          strTempIMGPath.ReleaseBuffer();
          if (m_pImage != NULL)
          {
          delete m_pImage;
          m_pImage = NULL;
          }
          m_pImage = new Image(szFile, FALSE);

          void CPNGButton::OnPaint()
          {
          CPaintDC dc(this);
          Graphics graphics(dc.m_hDC);
          if(m_pImage != NULL && (m_pImage->GetLastStatus() == Gdiplus::Status::Ok))
          {
          graphics.DrawImage(m_pImage, 0, 0, m_pImage->GetWidth(), m_pImage->GetHeight());
          }
          }

          V Offline
          V Offline
          VCProgrammer
          wrote on last edited by
          #4

          i want to load png file from Resources not with using file path of any selected file location.

          Y 1 Reply Last reply
          0
          • V VCProgrammer

            i want to load png file from Resources not with using file path of any selected file location.

            Y Offline
            Y Offline
            yu jian
            wrote on last edited by
            #5

            I event tried to load the PNG file from the resource. But I failed. If you find the way, please tell me.

            V 1 Reply Last reply
            0
            • Y yu jian

              I event tried to load the PNG file from the resource. But I failed. If you find the way, please tell me.

              V Offline
              V Offline
              VCProgrammer
              wrote on last edited by
              #6

              LPCTSTR szResourceName =MAKEINTRESOURCE(IDB_PNG1);
              LPCTSTR szResourceType=_T("PNG");
              HMODULE hresModule=AfxGetResourceHandle();

              HRSRC hPicture = FindResource(hresModule,szResourceName,szResourceType);
              HGLOBAL hResData;
              
              // Load the dialog box into global memory.
              if (!hPicture || !(hResData = LoadResource(hresModule,hPicture)))
              {
              	TRACE(\_T("Load (resource): Error loading resource %s\\n"),szResourceName);
              	return ;
              };
              DWORD dwSize = SizeofResource(hresModule,hPicture);	
              
              // hResData is not the real HGLOBAL (we can't lock it)
              // let's make it real
              
              HGLOBAL hGlobal = GlobalAlloc(GMEM\_MOVEABLE | GMEM\_NODISCARD,dwSize);
              if (!hGlobal)
              {
              	TRACE(\_T("Load (resource): Error allocating memory\\n"));
              	FreeResource(hResData);
              	return ;
              };
              
              char \*pDest = reinterpret\_cast (GlobalLock(hGlobal));
              char \*pSrc = reinterpret\_cast (LockResource(hResData));
              if (!pSrc || !pDest)
              {
              	TRACE(\_T("Load (resource): Error locking memory\\n"));
              	GlobalFree(hGlobal);
              	FreeResource(hResData);
              	return  ;
              };
              
              //IStream\* pStream;
              ULONG ulWritten=0;
              ulWritten = 0;
              
              CopyMemory(pDest,pSrc,dwSize);
              
              HRESULT hr = ::CreateStreamOnHGlobal(hGlobal,FALSE,&pStream);
              pStream->Write(pDest, dwSize, &ulWritten);
              
              FreeResource(hResData);
              GlobalUnlock(hGlobal);
              
              Bitmap oBmp(pStream,0);
              
              m\_Image\_ics.Load(pStream);
              HDC memdc;
              CRect rect;
              this->GetClientRect(&rect);     
              //=====
              HBITMAP hBmp;
              oBmp.GetHBITMAP(0, &hBmp);
              ::SendMessage(png\_bnr->GetSafeHwnd(),STM\_SETIMAGE,IMAGE\_BITMAP,(LPARAM)hBmp);
              
              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups