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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Load an image dynamically in win32 using VC++

Load an image dynamically in win32 using VC++

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionjsonlearning
6 Posts 4 Posters 0 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.
  • S Offline
    S Offline
    Sachin k Rajput
    wrote on last edited by
    #1

    I'm creating an application in which I'll have to load an image using open file dialog. there may be any image from system and will have to display that. I'll not have to save my image in resource because its not defined which image is going to be displayed. Application is win32 API not MFC. How can I do it? Thanks!

    _ L 2 Replies Last reply
    0
    • S Sachin k Rajput

      I'm creating an application in which I'll have to load an image using open file dialog. there may be any image from system and will have to display that. I'll not have to save my image in resource because its not defined which image is going to be displayed. Application is win32 API not MFC. How can I do it? Thanks!

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      This should help[^]

      You talk about Being HUMAN. I have it in my name AnsHUMAN

      1 Reply Last reply
      0
      • S Sachin k Rajput

        I'm creating an application in which I'll have to load an image using open file dialog. there may be any image from system and will have to display that. I'll not have to save my image in resource because its not defined which image is going to be displayed. Application is win32 API not MFC. How can I do it? Thanks!

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

        You can use the GDI+ Graphics.DrawImage methods[^] to draw most image types.

        S 1 Reply Last reply
        0
        • L Lost User

          You can use the GDI+ Graphics.DrawImage methods[^] to draw most image types.

          S Offline
          S Offline
          Sachin k Rajput
          wrote on last edited by
          #4

          It's completed. Now I'm trying to load image from memory buffer. Are you having any idea?

          L S 2 Replies Last reply
          0
          • S Sachin k Rajput

            It's completed. Now I'm trying to load image from memory buffer. Are you having any idea?

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

            You can copy the buffer to a global memory buffer, then use CreateStreamOnHGlobal[^] to get the image buffer as a stream, which can then be passed to Image.FromStream[^]. The following should (I hope) help to clarify it:

            // allocate a global memory block and copy the image data to it
            // dwFileSize is size of image buffer in memory (or file size if reading directly)
            HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
            PVOID pvData = GlobalLock(hGlobal);
            CopyMemory(pvData, pImageData, dwFileSize); // pImageData is source memory buffer
            GlobalUnlock(hGlobal);

            IStream* pStream;
            // create IStream* from global memory which will
            // be freed automatically when the stream is released
            HRESULT hResult = CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);
            Image* pImage = Image::FromStream(pStream);
            pStream->Release();

            RectF destRect; // set values to destination location and size
            Graphics graphics(hDC);
            graphics.DrawImage(pImage, destRect);
            delete pImage;

            1 Reply Last reply
            0
            • S Sachin k Rajput

              It's completed. Now I'm trying to load image from memory buffer. Are you having any idea?

              S Offline
              S Offline
              Shaheed Legion
              wrote on last edited by
              #6

              Hi Toms, you could use the CxImage library - which allows you to specify the image bits when constructing an image, or you could use CreateDIBSection to create a bitmap using the image data you already have, then display it using an HDC.

              For awesome websites or just to chat check out my blog for more info. . .

              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