blending images

D

Duke

I've been trying to find a way to combine two images such that they
are "blended" i.e. that the top image is a semi-transparent overlay on
the bottom image. Many code examples show how to do this on the
screen (using the Graphics class), but not how to create an actual
image with this feature.

The key reason that I wanted to do this was graphics resources. You
can have only so many transparent windows on a multiple monitor system
before the graphics card starts to freak out.

Anyway, here is some code on how to do that, provided to this
newsgroup as a contribution of an idea. If you have a better way to
accomplish the same thing, I welcome your comments (although, I may
not read this group nor this email account very frequently). The code
is in the context of a regular old form with two changes: the form
border style is "none" and the start position is "manual" both of
which I changed through thr form designed in visual studio.


Bitmap b, bPrime;
int w, h;

// load the file and use short names for width and height
b = new Bitmap(@"c:\folder\file.bmp");
w = b.Width; h = b.Height;

// I'll juxtapose the initial image (b) and darkened image (bPrime)
Left = 1024; Top = 0; Width = 2 * w; Height = h;

// create a new bitmap of the same size as the initial image
// this image will be the "overlay image"
bPrime = new Bitmap(w, h, PixelFormat.Format32bppArgb);

// by default, each RGB pixel of bPrime will be black, but you can
// change it to some other color if you'd prefer to do so.
// I'll keep it black.
bPrime = SolidColor(bPrime, Color.Black);

// now we need to set the alpha value of each pixel of the overlay
// I use 0xC0 here to create a "darker" overlay;
// lesser values will create a "lighter" overlay;
// 0xC0 is 75% of 0xFF, i.e. 75% opaque
bPrime = ChangeAlpha(bPrime, 0xC0);

// now draw the overlay on top of the initial image
bPrime = DrawOnTop(bPrime, b);

// finally, set the background image as the comparison of the
// juxtaposition of the initial image and the new image
BackgroundImage = Juxtapose(b, bPrime);


// Here are the supporting functions... which should be easy enough
// to understand without comments

public Bitmap SolidColor(Bitmap pic, Color c)
{
Bitmap b;
int w, h;

b = new Bitmap(pic);

for (w = 0; w < b.Width; w++)
{
for (h = 0; h < b.Height; h++)
{
b.SetPixel(w, h, c);
}
}

return b;
}


public Bitmap ChangeAlpha(Bitmap pic, byte alpha)
{
Bitmap b;
int w, h;

b = new Bitmap(pic);

for (w = 0; w < b.Width; w++)
{
for (h = 0; h < b.Height; h++)
{
b.SetPixel(w, h, Color.FromArgb(alpha, b.GetPixel(w, h)));
}
}

return b;
}


public Bitmap DrawOnTop(Bitmap top, Bitmap bottom)
{
Bitmap j;
Graphics jG;

j = new Bitmap(top.Width, top.Height, PixelFormat.Format32bppArgb);
jG = Graphics.FromImage(j);

jG.DrawImage(bottom, 0, 0, bottom.Width, bottom.Height);
jG.DrawImage(top, 0, 0, top.Width, top.Height);

return j;
}


public Bitmap Juxtapose(Bitmap l, Bitmap r)
{
Bitmap j;

Graphics jG;
int lw, rw, jw;
int lh, rh, jh;

lw = l.Width;
rw = r.Width;
jw = lw + rw;

lh = l.Height;
rh = r.Height;
jh = (lh > rh) ? lh : rh;

j = new Bitmap(jw, jh, PixelFormat.Format32bppArgb);
jG = Graphics.FromImage(j);

jG.DrawImage(l, 0, 0, lw, lh);
jG.DrawImage(r, lw, 0, rw, rh);

return j;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top