Help with Resizing Bitmap loaded with LoadImage() and Attach to CImagelist object
-
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.
-
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.
Could it help you? [how can i update the HBITMAP before retu - C++ Forum](http://www.cplusplus.com/forum/windows/179907/)
-
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.
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.
-
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.
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 -
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 deviThanks 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.
-
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.
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