 Code Optimization

Interesting things about software development and code optimization

Hello my dear friends,

in this post I will show you how to overlay one image over another image like if you would print an image over wood.

In this example we need two images, one is a wood image and another one is just a photo or whatever image do you like.

To make such effect we will do a few simple steps, here you are:

- create an empty image that will be a result image;

- draw your wood texture over that empty image;

- combine colors of the wood image and a photo, using the following formula c1 * c2 / 255.0;

The main thing here is to multiply each component of colors and divide it by 255.0. You do not need to do anything with alpha,

here is the short code:

for (int y = 0; y < surfWood.Bounds.Height; y++)

{

for (int x = 0; x < surfWood.Bounds.Width; x++)

{

color = surfWood.GetPixel(x, y);

color2 = surfSrc.GetPixel(x, y);

surfWood.SetPixelAsIs((byte)((double)color.R*(double)color2.R/255.0)

,(byte)((double)color.G*(double)color2.G/255.0)

,(byte)((double)color.B*(double)color2.B/255.0)

,color.A

, x, y);

}

}

the result you will get is like the following: so looks like you have printed out on a wood plate.

Thank you