Do I need to keep the Bitmap object when using Bitmap.GetHBITMAP()? - c++

When using Bitmap.GetHBITMAP(), do I need to keep (not delete) the Bitmap object, or does Bitmap.GetHBITMAP() creates a new HBITMAP and not just returns the one that the Bitmap object uses? (the documentation does not mention anything about this).

Related

Memory management when not storing handles returned by functions

Apologies if this has been answered before, I haven't been able to find an answer.
I'm doing some painting in my Win32 application, and have quite a few scenarios where I need to paint an object (e.g. a rectangle) only once. The way I'm currently creating brushes for this is as follows:
HBRUSH sampleBrush = CreateSolidBrush(RGB(1, 119, 158));
SelectObject(myDC, sampleBrush);
// Do some painting on DC using brush
DeleteObject(sampleBrush);
Create brush, store handle, select into DC, use brush, release memory.
However, if I were to do the following instead:
SelectObject(myDC, CreateSolidBrush(RGB(1, 119, 158)));
Would there be any memory management required since I'm not storing a handle to the brush I create? And if so, how would I release the memory?
The function CreateSolidBrush cannot know how you are using it. It can't know that you aren't storing the handle and therefore perform some automatic clean up. Since the documentation of CreateSolidBrush specifies that you should call DeleteObject with the returned handle, you should make sure you abide by those requirements.
If you don't store the handle, then you lose access to it and can't ensure that the object is destroyed.

CreateCompatibleBitmap Vs SelectObject

I'm wondering , if I want to create different bitmaps,
I use the CreateCompatibleBitmap function again and again, to associate it to the same memory CDC.
is it the same meaning that I CreateBitmap and SelectObject again and again??
I ask this question because I want to do something to the newly created bitmap by another CDC.
Without seeing the specific code it's hard to know the exact problem but CreateCompatibleBitmap is commonly used in double-buffering situations to avoid flickering. Rather than drawing directly to the Device Context (DC) you first draw to an off-screen, or memory, DC which is basically drawing to a bitmap. The bitmap is then copied directly to the screen DC using BitBlt, so it appears like all the drawing happens at the same time.
The usual steps are this (and will probably happen on every WM_PAINT):
Use the screen DC to create a bitmap, which is 'compatible' with it, using CreateCompatibleBitmap.
Create a memory DC
Select the bitmap into the memory DC (this is what you'll draw to)
When drawing is finished BitBlt the memory DC's bitmap onto the screen DC.
More information available here: Guide to Win32 Memory DC (Code Project)
Yes, CreateCompatibleBitmap() creates a new bitmap, a new memory allocation, a new handler each time you call it in a loop;

How to copy a CImage object?

I am trying to copy a CImage so that I can do some manipulation with it's pixels. I've tried this where source is a pointer to a loaded CImage object.
CImage* dest = new CImage(*source);
However this doesn't seem to work and I believe source and dest are pointing to the same memory.
How would I create a new copy totally detached from the previous CImage?
If this is a question about MFC/ATL CImage then you can create new instance and use Create to initialize it to the size of the original CImage. Then use BitBlt to copy contents.

Should I always replace default object after it has finished work with the new object when I Use DC

I just use a temp DC created with:
::CreateCompatibleDC(NULL);
Then I draw some image to this temp DC. I want to get the image in this DC, so I use CreateCompatibleBitmap() to create a temp bitmap and use SelectObject() to get the original HBITMAP in the DC.
I just want to DeleteDC() right now and after I'm done using the original bitmap, delete the original bitmap.
Is this wrong ?
Should I always replace original object after it has finished work with the new object when I Use DC ?
I think I am wrong.I can first create a bitmap and select it into the temp DC and after I'm done with the drawing. I can replace it with the original object.
I think this is the right way to solve the problem.

Create a CBitmap from Resources ID

I need to fill a CImageList with a number of bitmaps which are stored in separate bmp files (rather than as a single bmp with numerous parts). I assume I need to create a CBitmap so I can call the Add method of CImageList. So how might one create a CBitmap object using only MFC Resource IDs?
You just need to use the method CBitmap::LoadBitmap.
You can either load the bitmap from the file or from resources.