Halloween Fun - Embedding Ghosts Watermark in images with C#
For those of you who are going to celebrate Halloween on the
31st , here is a nice cool thing to do with C# and
System.Drawing library.
In this post I'll show you how
to use C# to embed a ghost image with a background image.
First we'll pick out the ingredients:
A ghost image
And for background I've picked a times square
shot
taken by
barcoder96
ok, now that we have these, let's start getting our hands
code dirty.
open up a console project , and add the
System.Drawing reference by adding a
using System.Drawing;
Next, type in the
following code
Image backImg = Image.FromFile("e:/pictures/times.jpg");
Image ghostImg = Image.FromFile("e:/pictures/ghost.png");
Graphics g = Graphics.FromImage(backImg);
g.DrawImage(ghostImg, backImg.Width/2, 10);
backImg.Save("e:/pictures/halloween.jpg");
What's happening here is that we basically load up our
background image and foreground image, we acquire a graphics
object from the background image, and use it to paint over
the foreground image. Looks simple, right ? here is the
result
But to make it more ghost realistic (there's an
oxymoron
for you), we'll make the ghost more transparent
for that, I'm using an image color manipulation trick
I first saw
here
(and there's also a good one
here)
The Matrix33 value below sets the level of opacity or
transparency. 0 is full transparency , 1 is no transparency
at all.
The matrix multiply all of the colors with the new
opacity value which "fades" out the colors and thus gives it
the requires transparency.
Bitmap transparentGhost = new Bitmap(ghostImg.Width, ghostImg.Height); Graphics transGraphics = Graphics.FromImage(transparentGhost); ColorMatrix tranMatrix = new ColorMatrix(); tranMatrix.Matrix33 = 0.25F; //this is the transparency value, tweak this to fine tuned results. ImageAttributes transparentAtt = new ImageAttributes(); transparentAtt.SetColorMatrix(tranMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); transGraphics.DrawImage(ghostImg, new Rectangle(0, 0, transparentGhost.Width, transparentGhost.Height), 0, 0, transparentGhost.Width, transparentGhost.Height, GraphicsUnit.Pixel, transparentAtt); transGraphics.Dispose(); g.DrawImage(transparentGhost, backImg.Width / 2, 10); backImg.Save("e:/pictures/halloween2.jpg");Which gets us to this better version
And if you iterate it some more , and give it some
skewing you can get the following result.
For achieving this , you can use the
YAEL C# image manipulation library
which provides a watermark image filter
Or trying this
Halloween Ghosts Image
filter online at
http://www.imgtoys.com