Combine two images with transparent

I have two bitmaps. One contains an image taken with a USB camera. The other will contain a shape like a rectal, but it could also be a trapezoid or, say, a random shape with one color in it. The rest of the image is now white.

The two images are not the same size, but the scaling algorithms are not the hardest part here, so let's assume they are the same size.

I want to show my shape on a USB camera image. The white part will be considered transparent for blending purposes. Right now I am about to edit image pixels by pixel, but I am looking for an API that will do it for me.

So, if I take a photo with a house in the middle and overlay a red rectangle, the resulting image will have the original image with a red rectangle around the house.

I am using .NET if it can help. I could also use the win32 API if it contains some useful functionality.

Edit: I accepted the answer because it put me on the right track. This is actually very easy to do.

Bitmap^ overlay_image = gcnew Bitmap("overlay.bmp");
Bitmap^ original_image = gcnew Bitmap("original.bmp");
overlay_image->MakeTransparent(Color::White);
Graphics^ g_original = Graphics::FromImage(original_image);
g_original->DrawImage(overlay_image, 0, 0);

      

Voilà original_image

now has a red rectangle above it. It's really fast enough for my 30ft USB camera so I can get it in real time.

There is no scaling now. It also assumes that the background of the overlay image is white so that it becomes transparent.

+1


a source to share


2 answers


If you are using the full .NET framework, System.Drawing.Imaging has functions for alpha channels and masking:



http://www.codeproject.com/KB/GDI-plus/alphafx.aspx

+1


a source


One fairly robust library to look out for is ImageMagick . They even have a .NET port of the library. It can do things related to transparency, shapes, and overlays.



I could use the command line versions (like command convert

or composite

) first to see if you can get them to do what you want. If that works, then you should be able to implement the same functionality using your libraries.

+1


a source







All Articles