28

while searching for a solution to automatically put a watermark to an image in internet, i found a best solution in stackoverflow. Link for the question is C# - Add watermark to the photo by special way. My special thanks to Alex Maslakov and adrift.

I implemented that solution with some modifications, i want to put watermark in center of the image. I modified the solution provided by adrift as follows

   private void button1_Click(object sender, EventArgs e)
    {
        using (Image image = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"))
        using (Image watermarkImage = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\watermark.png"))
        using (Graphics imageGraphics = Graphics.FromImage(image))
        using (Brush watermarkBrush = new TextureBrush(watermarkImage))
        {
            int x = (image.Width - watermarkImage.Width)/2;
            int y = (image.Height - watermarkImage.Height)/2;
            imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), watermarkImage.Size));
            image.Save(@"C:\Users\Public\Pictures\Sample Pictures\Desert_watermark.jpg");
        }

    }

but watermark is not properly placed in the center of image (see below image).

enter image description here

please correct me...

thanks

Community
  • 1
  • 1
Riskhan
  • 4,434
  • 12
  • 50
  • 76

4 Answers4

25

Finally i find the solution to my question...

The corrected code answer is following

    private void button1_Click(object sender, EventArgs e)
    {
        using (Image image = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"))
        using (Image watermarkImage = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\watermark.png"))
        using (Graphics imageGraphics = Graphics.FromImage(image))
        using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
        {
            int x = (image.Width / 2 - watermarkImage.Width / 2);
            int y = (image.Height / 2 - watermarkImage.Height / 2);
            watermarkBrush.TranslateTransform(x, y);
            imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width+1, watermarkImage.Height)));
            image.Save(@"C:\Users\Public\Pictures\Sample Pictures\Desert_watermark.jpg");
        }

    }

my thanks to Furqan Safdar and Abdias Software The link Problem in tiling image starting at different height using TextureBrush in C# also helped me to solve this problem

and thanks all

finally result

enter image description here

Community
  • 1
  • 1
Riskhan
  • 4,434
  • 12
  • 50
  • 76
8

Your original formula is fine btw.,

are you making sure the resolution of the two are the same? Set DPI resolution of your watermark image equal to image:

watermarkImage.SetResolution(imageGraphics.DpiX, imageGraphics.DpiY);

(from the top of my head, but i think the property names are correct).

UPDATE:

Change from Image to Bitmap in order to use SetResolution() - See if this change works for you - I've changed the way the watermark is drawn onto the image:

private void button1_Click(object sender, EventArgs e) {

    using (Bitmap image = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"))
    using (Bitmap watermarkImage = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\watermark.png"))
    using (Graphics imageGraphics = Graphics.FromImage(image))
    {
        watermarkImage.SetResolution(imageGraphics.DpiX, imageGraphics.DpiY);

        int x = ((image.Width - watermarkImage.Width) / 2);
        int y = ((image.Height - watermarkImage.Height) / 2);

        imageGraphics.DrawImage(watermarkImage, x, y, watermarkImage.Width, watermarkImage.Height);

        image.Save("C:\Users\Public\Pictures\Sample Pictures\Desert_watermark.jpg");
    }
}
  • image's resolution is 72dpi and watermarkimage's resolution is 96dpi, i can't find the function specified above, so changed watermarkimage's resolution to 72dpi manually, but no change in result – Riskhan Oct 13 '12 at 06:20
  • I have changed your routine a little in the way it draws the waterwark. See if that works. –  Oct 13 '12 at 06:44
  • Finally i found the solution to solve problem, please see my own answer. many many thanks for you for your support – Riskhan Oct 13 '12 at 07:09
  • Impossible to converto from Image to Bitmap – Emanuele Sep 19 '16 at 10:31
2

Try this code for center alignment:

int x = (image.Width / 2 - watermarkImage.Width / 2);
int y = (image.Height / 2 - watermarkImage.Height / 2);
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
1

Working sample:

public static Bitmap WatermarkImage(Bitmap image, Bitmap watermark)
{
    using (Graphics imageGraphics = Graphics.FromImage(image))
    {
        watermark.SetResolution(imageGraphics.DpiX, imageGraphics.DpiY);

        int x = (image.Width - watermark.Width) / 2;
        int y = (image.Height - watermark.Height) / 2;

        imageGraphics.DrawImage(watermark, x, y, watermark.Width, watermark.Height);
    }

    return image;
}

Usage:

Bitmap watermark = new Bitmap(@"c:/watermark.png");
Bitmap bitmap = new Bitmap(@"c:/image.png");
bitmap = WatermarkImage(bitmap, watermark);
bitmap.Save(@"C:/watermarkedImage.png");
Erçin Dedeoğlu
  • 4,950
  • 4
  • 49
  • 69