473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

blending images

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;
}
Nov 16 '05 #1
2 6697
you need to use gdi+ m8... like this article describes:

http://www.codeproject.com/cs/media/KVImageProcess.asp

"Duke" <du****@gmail.com> wrote in message
news:8f**************************@posting.google.c om...
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;
}

Nov 16 '05 #2
you need to use gdi+ m8... like this article describes:

http://www.codeproject.com/cs/media/KVImageProcess.asp

"Duke" <du****@gmail.com> wrote in message
news:8f**************************@posting.google.c om...
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;
}

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Philippe Meunier | last post by:
Hi, I am using the .NET toolbar with VB.NET 2003. My toolbar uses icons images that are actually PNG files and uses alpha blending on it. So to make alpha blending work correctly with the...
7
by: Wayne | last post by:
I have a script that uses filesystemobject that reads files from a given path, in my case images. It is running on a server that is 2000 adv svr w/ all current patches. The script prior to some...
6
by: Renato | last post by:
Hi, I have seen the board described in this link. http://www.seventech.it/english/merlinoboard/2.php I think that its a good solution to realize an embedded system with VGA output, in that cases...
0
by: Duke | last post by:
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...
3
by: Simon | last post by:
This problem has been driving me mad for months.... Seen a few posts on forums about it but no answers... No mention on MSDN etc. XP Pro SP1, VS.NET (c#) .Net framework 1.1, IIS 5.1. In a...
4
toxicpaint
by: toxicpaint | last post by:
Hi, can anyone give me a hand. I'm currently displaying 4 random images at the top of a page. I did this using an array of 35 pictures and then writing them to page. The problem I have is that in...
5
by: remon87 | last post by:
I need some help. I have javasript that creates the submenu but it works if I have a text with css. I need it to do the same with a roll over images. so when I click on the image the submenu...
5
by: HxRLxY | last post by:
I am creating a custom JPanel that displays an image in the background on top of a custom color. What I want to do is to have the edges of the top image blend into the background color. Only the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.