1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

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

12 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
  3. thank you so much. I am also looking for watermarking by writing text on the image.( write text over and over again until the end of image)

    ReplyDelete
  4. great!
    now just need to figure out how to position the image.

    ReplyDelete
  5. good..please help me to retrieve watermark text from image.

    ReplyDelete
  6. you are required to write a program that reads a an image file and a watermark file (which is simply another image file, but smaller in size) and adds the watermark to the image producing an output image file.
    The program must be implemented as a command-line application.

    The following command-line options are to be supported by the applicaion.

    -help.
    This option prints out to the console an option summary and causes the application to exit (ignoring any other options and/or arguments).
    -version.
    This option prints out to the console the application's version and causes the application to exit (ignoring any other options and/or arguments).
    -watermark.
    This value option specifies the name of the watermark file. The watermark file must be a valid image file, and it is expected that its size is considerably smaller than the size of the image that we watermark.
    -position.
    This value option specifies where the watermark should be placed on the image. The valid values for this option should be one of: TL, TR, BL, and BR. Here T stands for top, B for bottom, L for left, and R for right. The default value for this option (if unspecified) is TL.
    -strength.
    This value option specifies an integer in the range 0..100 that is used to denote what percentage of a watermark pixel is blended with a source pixel. You will take the colour value of a watermark pixel, multiply the value by the strength and add it the colour value of the corresponding image pixed multiplied by (100 - strength). This blends the watermark with the source image. Smaller strength value means the watermark is less visible and therefore less intrusive. The default value for this option is 10.
    -output.
    This value option specifies a directory where the output images are placed. The default name for the output directory is "Out".

    The command-line arguments after the options specify the input image files that are to be watermarked by the application.
    Examples

    Here are some example usages of the application. It is assumed that the application is called WamTool.

    WamTool -watermark Uni.bmp MaxxWalk.png DarkKnight.jpg
    The application embeds the watermark (specified in Uni.bmp) in the image files MaxxWalk.png and DarkKnight.jpg. The watermark will be placed at the top-left corner of the images. The output image files will be created in a folder called "Out" and will bear the same name as the input files.
    WamTool -watermark Uni.bmp -position BL -output WM MaxxWalk.png
    The application embeds the watermark (specified in Uni.bmp) in the image file MaxxWalk.png. The watermark will be placed at the bottom-left corner of the image. The output image file will be created in a folder called "WM" and will bear the same name as the input file.



    please anyone help me by sending sample code. its very urgent..my i.d. is sachin10033@yahoo.com

    ReplyDelete
  7. Greetings,

    I am currently working on a watermarking application. I am considering a perceptual and non-perceptual watermarks. The thing I am hesitating on is the choice of programming language. Is C# good for such application in general? I am not very familiar whether I can do low-level programming with C#, as compared with C++? What would you suggest?

    Regards!

    ReplyDelete
  8. Hi

    thanks a lot for the sharing. i used 2 images of same size i.e watermark_logo and the file to watermark, but the final result is not displaying the watermark_logo.

    any ideas ?

    ReplyDelete
  9. can u plz send me ur code in asp.net project file?? i mean as web page( complete) at ctasgroups@gmail.com

    ReplyDelete

Related Posts with Thumbnails