Friday, January 23, 2009

Watermarking an image in C#.net

CodeGlobeImagewatermarkingFirst we will select our background image.


Then we select our watermark image.

Now open a new project and add the reference System.Drawing.Imaging;

Next type the following code. Each line in the following code is sufficiently commented.

Image image = System.Drawing.Image.FromFile(@"C:\Ascent.jpg");//This is the background image
Image
logo = Image.FromFile(@"C:\SUNFLOWR.JPG"); //This is your watermark
Graphics
g = System.Drawing.Graphics.FromImage(image); //Create graphics object of the background image //So that you can draw your logo on it
Bitmap TransparentLogo = new Bitmap(logo.Width, logo.Height); //Create a blank bitmap object //to which we //draw our transparent logo
Graphics TGraphics = Graphics.FromImage(TransparentLogo);//Create a graphics object so that //we can draw //on the blank bitmap image object
ColorMatrix ColorMatrix = new ColorMatrix(); //An image is represenred as a 5X4 matrix(i.e 4 //columns and 5 //rows)
ColorMatrix
.Matrix33 = 0.25F;//the 3rd element of the 4th row represents the transparency
ImageAttributes
ImgAttributes = new ImageAttributes();//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 of the //ImageAttributes object, and the //ImageAttributes object is passed to the DrawImage method of the Graphics object.
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();
g.DrawImage(TransparentLogo, image.Width / 2, 30);
image.
Save(@"C:\Test.jpeg", ImageFormat.Jpeg);

This is how the resulting image will look like.


WatermarkedImage
kerala webdevelopers-webhosting providers

3 comments:

  1. sir pls help me to extract the original image
    from the watermarked image
    my id is saravanakumar87@gmail.com

    ReplyDelete
  2. you can't remove watermark from images.It's possible to edit image using any of the good image editing software.

    ReplyDelete

LinkWithin

Related Posts with Thumbnails