Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I am trying to watermark my image with a logo and for that I am using this function, but I get an image which is not watermarked.

I have not got any error msg, it is poulating data properly but the image which I am getting as output doesn't have the watermarked logo on it.

Here the code which I have posted, it is one which I got from the internet.
I don't understand actually where to use original image and logo, or how to use drawimage to draw logo on the image.


 public Bitmap AddWatermark(Bitmap bImg, Bitmap logo)
{
    Bitmap imgPhoto = bImg;
    int phWidth = imgPhoto.Width;
    int phHeight = imgPhoto.Height;
    Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
   bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
   Graphics grPhoto = Graphics.FromImage(bmPhoto);
   System.Drawing.Image imgWatermark = logo;
    int wmWidth = imgWatermark.Width;
    int wmHeight = imgWatermark.Height;
   Bitmap bmWatermark = new Bitmap(bmPhoto);
    bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
    Graphics grWatermark = Graphics.FromImage(bmWatermark);
    ImageAttributes imageAttributes = new ImageAttributes();
    ColorMap colorMap = new ColorMap();
    colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
    colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
    ColorMap[] remapTable = { colorMap };
    imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
    float[][] colorMatrixElements = {
                                            new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
                                            new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
                                            new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
                                            new float[] {0.0f,  0.0f,  0.0f,  1.0f, 0.0f},
                                            new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};
    ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
    imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
    int xPosOfWm = ((phWidth - wmWidth) - 10);
    int yPosOfWm = 10;

    grWatermark.DrawImage(imgWatermark,new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0,wmWidth,wmHeight,GraphicsUnit.Pixel,imageAttributes);   //ImageAttributes Object

    imgPhoto = bmWatermark;
    return imgPhoto;
}
Posted
Updated 23-Mar-11 6:24am
v4
Comments
J a a n s 23-Mar-11 11:48am    
Your code is not at all using the original image 'bImg'. Also it is creating a lot of unwanted variables. Please get code from some better source and use it.
Dalek Dave 23-Mar-11 12:25pm    
Edited for Grammar, Syntax and Readability.

The error lies in your color matrix element at position 3,3 (zero based) has to be the opacity. 1.0 means it is totally opaque so you'll see the water mark image over the other image. Try with your color matrix like this:

C#
float[][] colorMatrixElements = {
    new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
    new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
    new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
    new float[] {0.0f,  0.0f,  0.0f,  0.25f,0.0f},
    new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};


Matrix element 3,3 is now set to 25 percent opacity so some of the image that is being watermarked will show through.

Hope this helps you!

Look also at this article: Creating a Watermarked Photograph with GDI+ for .NET[^].

Happy coding!
 
Share this answer
 
Comments
Sandeep Mewara 23-Mar-11 11:35am    
My 5! Found the mistake too along with helpful link.
Dalek Dave 23-Mar-11 12:25pm    
Good Catch and Answer.
Sergey Alexandrovich Kryukov 23-Mar-11 20:26pm    
Very good, my 5.
--SA
RKT S 23-Mar-11 21:43pm    
thnaks manfred...
RKT S 24-Mar-11 0:09am    
hi manfred....i did the changes which you asked me to do... but still the watermark image is the one without logo on it... plz help and let me know if i am doing something wrong here.. the final function which i have used is

public Bitmap AddWatermark(Bitmap bImg, Bitmap logo)
{

Bitmap bmPhoto = bImg;
//bmPhoto.SetResolution(bImg.HorizontalResolution,bImg.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);

Bitmap imgWatermark=logo;
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;

//Create a Bitmap based on the previously modified photograph Bitmap
ImageAttributes imageAttributes = new ImageAttributes();
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
grPhoto.DrawImage(imgWatermark, new Rectangle(0, 0, wmWidth, wmHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes);

return bmPhoto;

}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Mar-11 20:27pm    
My 5,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900