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

    Question [C++] Win32 GDI+ Update Bitmap value?

    I'm creating a program, and I need to load a PNG image from the resource files. The goal is simple, pass a Bitmap object by reference to a function, which assigns the PNG from the stream to it, and returns. Because it's by reference, the origial should be affected.

    Using the tutorial over on CodeProject, I came up with the following code:

    Code:
    int loadResource(Bitmap& image,LPCTSTR name,LPCTSTR type,HINSTANCE hinst=GetModuleHandle(0)){
    
      HRSRC hRes=FindResource(hinst, name, type);
      if(!hRes) return -1;
    
      unsigned long imgSize=SizeofResource(hinst, hRes);
      if(!imgSize) return -1;
    
      const void* pResData=LockResource(LoadResource(hinst, hRes));
      if(!pResData) return -1;
    
      HGLOBAL buffer=GlobalAlloc(GMEM_MOVEABLE, imgSize);
      if(buffer){
        void* pBuffer=GlobalLock(buffer);
    
        if(pBuffer){
          CopyMemory(pBuffer, pResData, imgSize);
    
          IStream* pStream=NULL;
          if(CreateStreamOnHGlobal(buffer, false, &pStream)==S_OK){
            image.FromStream(pStream);
            int status=image.GetLastStatus();
            if(status==Ok){
              pStream->Release();
              GlobalUnlock(buffer);
              GlobalFree(buffer);
              return status;
            }
            else{
              pStream->Release();
              GlobalUnlock(buffer);
              GlobalFree(buffer);
              return status;
            }
          }
        }
      }
      return -1;
    }
    And in my WM_PAINT:

    Code:
    /*Standard stuff*/
    HINSTANCE hinst=GetModuleHandle(0);
    			
    //This one works, but in the release, I don't want the image outside of the EXE
    //Bitmap image(L"temp.png"); 
    Bitmap image(30,44);
    loadResource(image, MAKEINTRESOURCE(IDB_TEMP), RT_RCDATA);
    Graphics graphics(hdc);
    TextureBrush brush(&image,WrapModeTile);
    graphics.FillRectangle(&brush, Rect(0, 0, 60, 88));
    
    /*Standard stuff*/
    The problem is that the original Bitmap isn't updated properly. It's initialized, and keeps that value, even after loadResource() runs.

    Can anybody help me out with this?

  2. #2
    Join Date
    Nov 2009
    Posts
    45

    Re: [C++] Win32 GDI+ Update Bitmap value?

    Can you place your PNG file here for checking? I will try to help you check that.
    Jack
    --------------------------------------------------------------------------------
    HMI/SCADA/CAD/GIS VC++ Source Codes: http://www.code-home.com

  3. #3
    Join Date
    Feb 2011
    Posts
    20

    Re: [C++] Win32 GDI+ Update Bitmap value?

    The image is attached. Thanks for the help.
    Attached Images Attached Images  

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