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. Help with Resizing Bitmap loaded with LoadImage() and Attach to CImagelist object

Help with Resizing Bitmap loaded with LoadImage() and Attach to CImagelist object

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsperformancehelpquestion
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.
  • E Offline
    E Offline
    EternalWyrm
    wrote on last edited by
    #1

    I've been working on this for a few days and I can't seem to get it. I just want to load a bitmap from file, resize it, then attach it to a CImageList obj via the Add() Member function. I'm using LoadImage() to load the bitmap into memory from disk. HBITMAP hbmp = (HBITMAP) ::LoadImage(..) ; I can display the bitmap just fine using pDC->DrawState(…, , hbmp); I've tried creating a memoryDC and a Compatible Bitmap like this: HDC hClientDC = GetDC()->m_hDC; HDC hMemDC = NULL; HBITMAP hMemBitmap = NULL; //create compatible memory area hMemDC = CreateCompatibleDC(hClientDC); hMemBitmap = CreateCompatibleBitmap(hMemDC, cx, cy); But, then I can't seem to copy anything into it. I'm a noob, so I'm just lost here. Can anyone help? EW.

    V L L 3 Replies Last reply
    0
    • E EternalWyrm

      I've been working on this for a few days and I can't seem to get it. I just want to load a bitmap from file, resize it, then attach it to a CImageList obj via the Add() Member function. I'm using LoadImage() to load the bitmap into memory from disk. HBITMAP hbmp = (HBITMAP) ::LoadImage(..) ; I can display the bitmap just fine using pDC->DrawState(…, , hbmp); I've tried creating a memoryDC and a Compatible Bitmap like this: HDC hClientDC = GetDC()->m_hDC; HDC hMemDC = NULL; HBITMAP hMemBitmap = NULL; //create compatible memory area hMemDC = CreateCompatibleDC(hClientDC); hMemBitmap = CreateCompatibleBitmap(hMemDC, cx, cy); But, then I can't seem to copy anything into it. I'm a noob, so I'm just lost here. Can anyone help? EW.

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Could it help you? [how can i update the HBITMAP before retu - C++ Forum](http://www.cplusplus.com/forum/windows/179907/)

      1 Reply Last reply
      0
      • E EternalWyrm

        I've been working on this for a few days and I can't seem to get it. I just want to load a bitmap from file, resize it, then attach it to a CImageList obj via the Add() Member function. I'm using LoadImage() to load the bitmap into memory from disk. HBITMAP hbmp = (HBITMAP) ::LoadImage(..) ; I can display the bitmap just fine using pDC->DrawState(…, , hbmp); I've tried creating a memoryDC and a Compatible Bitmap like this: HDC hClientDC = GetDC()->m_hDC; HDC hMemDC = NULL; HBITMAP hMemBitmap = NULL; //create compatible memory area hMemDC = CreateCompatibleDC(hClientDC); hMemBitmap = CreateCompatibleBitmap(hMemDC, cx, cy); But, then I can't seem to copy anything into it. I'm a noob, so I'm just lost here. Can anyone help? EW.

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

        You can copy a bitmap into a DC using one of the 'blit' methods: BitBlt function (wingdi.h) - Win32 apps | Microsoft Docs[^] does a straight copy. StretchBlt function (wingdi.h) - Win32 apps | Microsoft Docs[^] copies, and compresses or expands the image in the target DC.

        1 Reply Last reply
        0
        • E EternalWyrm

          I've been working on this for a few days and I can't seem to get it. I just want to load a bitmap from file, resize it, then attach it to a CImageList obj via the Add() Member function. I'm using LoadImage() to load the bitmap into memory from disk. HBITMAP hbmp = (HBITMAP) ::LoadImage(..) ; I can display the bitmap just fine using pDC->DrawState(…, , hbmp); I've tried creating a memoryDC and a Compatible Bitmap like this: HDC hClientDC = GetDC()->m_hDC; HDC hMemDC = NULL; HBITMAP hMemBitmap = NULL; //create compatible memory area hMemDC = CreateCompatibleDC(hClientDC); hMemBitmap = CreateCompatibleBitmap(hMemDC, cx, cy); But, then I can't seem to copy anything into it. I'm a noob, so I'm just lost here. Can anyone help? EW.

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          At no point at you actually selecting the bitmap onto the DC, or bitblt or anything that would actually move the bitmap onto the DC This is usual code for doing it

          /*---------------------------------------------------------------------------
          This will create and Memory Device context and copy the bitmap given onto
          that memory context. Fail will return NULL as a memory context.
          Caller takes responsibility for disposal of the device context.
          --------------------------------------------------------------------------*/
          HDC BitmapToMemDc (HBITMAP hBmp)
          {
          HDC hMemDC = 0; // Preset memory context fail
          if (hBmp != 0) // Check bitmap handle valid
          {
          hMemDC = CreateCompatibleDC(NULL); // Create memory context
          if (hMemDC) // Check memory context success
          {
          SelectObject(hMemDC, hBmp); // Select the bitmap to the context
          }
          }
          return hMemDC; // Return the memory context
          }

          For a bitmap file you have it right LoadImage and then use the above

          /*---------------------------------------------------------------------------
          This will create and Memory Device context of the the bitmap filename given
          Caller must take responsibility for disposal of the device context.
          --------------------------------------------------------------------------*/
          HDC MemDCFromBMPFile(char* Filename)
          {
          HDC MemDC = 0; // Preset return value to fail
          HBITMAP bmp = LoadBitmapFromFile(Filename); // Load the bitmap
          if (bmp != 0) // Load bitmap was successful
          {
          MemDC = BitmapToMemDc(bmp); // Create a memory context of a valid bitmap
          DeleteObject(bmp); // Release the bitmap no longer needed (image on memDC)
          }
          return MemDC; // Return result
          }

          For completeness GIF, JPG's etc are a little more tricky you need to use Ipic Ipic will also load bmp and png and you can rescale any file with the Ipic->Render line and it isn't to bad It's resize is much better than stretchblt :-)

          #include "olectl.h" // OLE is used for JPG image loading

          /*---------------------------------------------------------------------------
          Creates a Memory Device context of the the JPEG/GIF filename given.
          Due to the way OLE works filename must be fully qualified (ie c:\folder\a.jpg)
          You can not assume the loading directory is the current directory.
          Caller must take responsibility for disposal of the devi

          E 1 Reply Last reply
          0
          • L leon de boer

            At no point at you actually selecting the bitmap onto the DC, or bitblt or anything that would actually move the bitmap onto the DC This is usual code for doing it

            /*---------------------------------------------------------------------------
            This will create and Memory Device context and copy the bitmap given onto
            that memory context. Fail will return NULL as a memory context.
            Caller takes responsibility for disposal of the device context.
            --------------------------------------------------------------------------*/
            HDC BitmapToMemDc (HBITMAP hBmp)
            {
            HDC hMemDC = 0; // Preset memory context fail
            if (hBmp != 0) // Check bitmap handle valid
            {
            hMemDC = CreateCompatibleDC(NULL); // Create memory context
            if (hMemDC) // Check memory context success
            {
            SelectObject(hMemDC, hBmp); // Select the bitmap to the context
            }
            }
            return hMemDC; // Return the memory context
            }

            For a bitmap file you have it right LoadImage and then use the above

            /*---------------------------------------------------------------------------
            This will create and Memory Device context of the the bitmap filename given
            Caller must take responsibility for disposal of the device context.
            --------------------------------------------------------------------------*/
            HDC MemDCFromBMPFile(char* Filename)
            {
            HDC MemDC = 0; // Preset return value to fail
            HBITMAP bmp = LoadBitmapFromFile(Filename); // Load the bitmap
            if (bmp != 0) // Load bitmap was successful
            {
            MemDC = BitmapToMemDc(bmp); // Create a memory context of a valid bitmap
            DeleteObject(bmp); // Release the bitmap no longer needed (image on memDC)
            }
            return MemDC; // Return result
            }

            For completeness GIF, JPG's etc are a little more tricky you need to use Ipic Ipic will also load bmp and png and you can rescale any file with the Ipic->Render line and it isn't to bad It's resize is much better than stretchblt :-)

            #include "olectl.h" // OLE is used for JPG image loading

            /*---------------------------------------------------------------------------
            Creates a Memory Device context of the the JPEG/GIF filename given.
            Due to the way OLE works filename must be fully qualified (ie c:\folder\a.jpg)
            You can not assume the loading directory is the current directory.
            Caller must take responsibility for disposal of the devi

            E Offline
            E Offline
            EternalWyrm
            wrote on last edited by
            #5

            Thanks for the help Leon!! I did manage to get the bmp code working good, however, I noticed as I compressed the bmp further and further with StretchBlt() it got ugly... When I tried using your JPG/GIF loading code, was it supposed to proc a Dlgbox? or no? I had some trouble with the LPOLESTR converting from my program... but I think I got that fixed... here's the code: HDC MemDCFromJPG_GIF_File(CString filename, int iWidth, int iHeight) { const int HIMETRIC_PER_INCH = 2540; SIZE sizeInHiMetric; IPicture* Ipic = NULL; HDC MemDC = 0; // Preset return value to fail BSTR b = filename.AllocSysString(); LPOLESTR pOLE_stringpath = b; HRESULT hr = OleLoadPicturePath(pOLE_stringpath, NULL, 0, 0, IID_IPicture, (LPVOID*)&Ipic); // Load the picture if ((hr == S_OK) & (Ipic != 0)) // Picture loaded okay { ... However, the Ipic pointer is returning NULL every time? I don't know how to fix that?? Thanks again for your help! Ryan.

            L 1 Reply Last reply
            0
            • E EternalWyrm

              Thanks for the help Leon!! I did manage to get the bmp code working good, however, I noticed as I compressed the bmp further and further with StretchBlt() it got ugly... When I tried using your JPG/GIF loading code, was it supposed to proc a Dlgbox? or no? I had some trouble with the LPOLESTR converting from my program... but I think I got that fixed... here's the code: HDC MemDCFromJPG_GIF_File(CString filename, int iWidth, int iHeight) { const int HIMETRIC_PER_INCH = 2540; SIZE sizeInHiMetric; IPicture* Ipic = NULL; HDC MemDC = 0; // Preset return value to fail BSTR b = filename.AllocSysString(); LPOLESTR pOLE_stringpath = b; HRESULT hr = OleLoadPicturePath(pOLE_stringpath, NULL, 0, 0, IID_IPicture, (LPVOID*)&Ipic); // Load the picture if ((hr == S_OK) & (Ipic != 0)) // Picture loaded okay { ... However, the Ipic pointer is returning NULL every time? I don't know how to fix that?? Thanks again for your help! Ryan.

              L Offline
              L Offline
              leon de boer
              wrote on last edited by
              #6

              Did you take note of the comment

              Due to the way OLE works filename must be fully qualified (ie c:\folder\a.jpg)

              That is usually the first port of call to why OleLoadPicturePath fails.

              In vino veritas

              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