How to copy a CImage object? - c++

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.

Related

How can I rotate and make the image semitransparent at the same time by using ATL::CImage?

I just started using ATL::CImage in my MFC project, and this is very basic question about it.
I know ATL::CImage members support AlphaBlend() for controlling the transparency, and plgblt() for rotating. But they are all the independent functions for only "Displaying" on DC as I understand.
How Can I apply both transparency and rotation of the image and display/save it?
I know GDI+ supports everything what I want, but I wanna know how to realize them with CImage class members too.
Thank you.
This isn't possible in the same way. You can use a temporary DC for each operation.
Create a DC with a bitmap of the needed size.
Perform the operation of the CImage into the DC.
Get the Bitmap from the DC and form a new CImage or simply work on with the DC.
The better way is always to use GDI+ to perform such operations. CImage is only needed when you need to store the interim result, or need to reuse it.

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

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).

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.

dereferencing SDL_Surfaces?

is there any way to copy a SDL surface to another, like creating a backup copy, without modifying the original when the copy is modified? *surface = *original_surface dosnt work. SDL_Surface does not have any constructors, so i cant do anything like surface = new SDL_Surface (original_surface). currently, i am opening the original image constantly, but it takes longer to open the image than for one loop to finish. this causes a lot of lagging, and eventually errors, causing my program to end
You can create a new compatible surface:
copy = SDL_CreateRGBSurface(flags, width, height, original->format.BitsPerPixel,
original->format.Rmask, original->format.Gmask,
original->format.Bmask, original->format.Amask);
And then blit the original into the copy:
SDL_BlitSurface(original, NULL, copy, NULL);

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.