Helem Shoshi
Helem Shoshi

Reputation: 239

How can merge two images to be one image with the two images combined one transparent over the second one?

Bitmap bmp1 = new Bitmap(@"c:\coneimages\Cone_Images1.gif");
Bitmap bmp2 = new Bitmap(@"c:\coneimages\PictureBox_Images1.gif");
Bitmap bmp3 = new Bitmap(MergeTwoImages(bmp1,bmp2));
bmp3.Save(@"c:\coneimages\merged.bmp");

And

public static Bitmap MergeTwoImages(Image firstImage, Image secondImage)
        {
            if (firstImage == null)
            {
                throw new ArgumentNullException("firstImage");
            }

            if (secondImage == null)
            {
                throw new ArgumentNullException("secondImage");
            }

            int outputImageWidth = firstImage.Width > secondImage.Width ? firstImage.Width : secondImage.Width;

            int outputImageHeight = firstImage.Height + secondImage.Height + 1;

            Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            using (Graphics graphics = Graphics.FromImage(outputImage))
            {
                graphics.DrawImage(firstImage, new Rectangle(new Point(), firstImage.Size),
                    new Rectangle(new Point(), firstImage.Size), GraphicsUnit.Pixel);
                graphics.DrawImage(secondImage, new Rectangle(new Point(0, firstImage.Height + 1), secondImage.Size),
                    new Rectangle(new Point(), secondImage.Size), GraphicsUnit.Pixel);
            }

            return outputImage;
        }

But that's not what i wanted i'm not sure how and what to look for in google. What i want is that the bmp1 will over bmp2 like layer. The bmp1 is transparent and i want it be like layer over bmp2.

So in bmp3 i will see the whole regular bmp2 with bmp1 on it.

Upvotes: 1

Views: 174

Answers (2)

Ibki
Ibki

Reputation: 91

Bitmap m_back = new Bitmap(bmp2.Width, bmp2.Height);
for (int y = 0; y < bmp2.Height; y++)
{
     for (int x = 0; x < bmp2.Width; x++)
     {
          Color temp = Color.FromArgb(80, bmp2.GetPixel(x, y));
          m_back.SetPixel(x, y, temp);
     }
}

Bitmap bmp3 = new Bitmap(MergeTwoImages(bmp1, m_back));

Upvotes: 1

Steven Hansen
Steven Hansen

Reputation: 3239

Assuming bmp1 has alpha in it, draw bmp2 first, THEN with compositing mode set to SourceOver (the default) draw bmp1. This should accomplish the correct alpha blend order.

In other words...

Bitmap bmp3 = new Bitmap(MergeTwoImages(bmp2,bmp1)); // Swapped arguments.

If bmp1 does not contain alpha, you will need to use a color matrix to change the transparency.

Upvotes: 2

Related Questions