"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Sunday, January 2, 2011

watermark image over image

hi friends,
in my previous post i shown watermark of text over image. 2day I'm showing image over image



using System;
using System.Drawing;
using System.Drawing.Imaging;

//Original Image that you want to watermark
Image image = System.Drawing.Image.FromFile(@"C:\way2sms.png");

//Watermark
Image logo = Image.FromFile(@"C:\Capture.png");

//Create graphics object of the Orignal image
Graphics g = System.Drawing.Graphics.FromImage(image);

//Create a blank bitmap object to which we will draw our transparent logo
Bitmap TransparentLogo = new Bitmap(logo.Width, logo.Height);

//Create a graphics object so that we can draw on the blank bitmap image object
Graphics TGraphics = Graphics.FromImage(TransparentLogo);

//An image is represenred as a 5X4 matrix
//the 3rd element of the 4th row represents the transparency
ColorMatrix ColorMatrix = new ColorMatrix();
ColorMatrix.Matrix33 = 0.50F ; //Transparency

//An ImageAttributes object is used to set all the alpha values.
//This is done by initializing a color matrix and setting the alpha scaling value in the matrix.
//The address of the color matrix is passed to the SetColorMatrix method
//The ImageAttributes object is passed to the DrawImage method
ImageAttributes ImgAttributes = new ImageAttributes();
ImgAttributes.SetColorMatrix(ColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
TGraphics.DrawImage(logo, new Rectangle(0, 0, TransparentLogo.Width, TransparentLogo.Height), 0, 0, TransparentLogo.Width, TransparentLogo.Height, GraphicsUnit.Pixel, ImgAttributes);

TGraphics.Dispose();

//Do Watermarking
g.DrawImage(TransparentLogo, (image.Width - logo.Width) / 2, (image.Height - logo.Height) / 2);

//Save Image
image.Save(@"C:\MyWatermakedImage.png", ImageFormat.Png);




Have a nice day... 'N happy Coding :)

No comments: