-1

Trying to add a watermark image to a png image, i´ve been able to do it, but i want to take out the hardCoded size regulation for the rectangle of the waterMark, and make it always stay in the center of the image. How can i achieve this.

public Form1()
    {
        InitializeComponent();
        picBox.Parent = this;
        picBox.Dock = DockStyle.Fill;
        picBox.SizeMode = PictureBoxSizeMode.Zoom;
        Bitmap Jpg = new Bitmap(@"C:\Users\tferreira\Desktop\213123.PNG");
        using (Bitmap Bmp = new Bitmap(@"C:\Users\tferreira\Desktop\logo.png"))
        {
            using (Bitmap WatermarkBmp = new Bitmap(Bmp, Bmp.Width / 1, Bmp.Height / 1))
            {
                picBox.Image = WatermarkImage(Jpg, WatermarkBmp, new Point(400, 100), 0.40F);
            }
        }
    }

    public Bitmap WatermarkImage(Bitmap ImageToWatermark, Bitmap Watermark, Point WatermarkPosition, float Opacity)
    {
        using (Graphics G = Graphics.FromImage(ImageToWatermark))
        {
            using (ImageAttributes IA = new ImageAttributes())
            {
                ColorMatrix CM = new ColorMatrix();
                CM.Matrix33 = Opacity;
                IA.SetColorMatrix(CM);
                G.DrawImage(Watermark, new Rectangle(WatermarkPosition, Watermark.Size), 0, 0, Watermark.Width, Watermark.Height, GraphicsUnit.Pixel, IA);
            }
        }
        return ImageToWatermark;
    }

Right now the images are hardCoded but that will be taken out. If anyone can help me make this watermark allways stay centered i thank you.

Tiago Silva
  • 254
  • 2
  • 13
  • [Resizing a Bitmap used as watermark the result shows dark borders](https://stackoverflow.com/a/61977870/7444103), partially linked to [How to apply a fade transition effect to PictureBox Images using a Timer?](https://stackoverflow.com/a/61293719/7444103) (because of a the *fading* effect shown there). – Jimi Aug 10 '21 at 16:38
  • 2
    Why would you pass out the rectangle position when it should always be centered? With bitmap and size the position is easily calculated, no? – TaW Aug 10 '21 at 16:54
  • @TaW i guess your right. – Tiago Silva Aug 10 '21 at 16:54
  • @TaW the how do i do it? – Tiago Silva Aug 10 '21 at 16:57
  • 2
    Position (x,y): x=(W-w)/2; y=(H-h) / 2 with Bitmapsize (W,H) and rect size = (w,h). – TaW Aug 10 '21 at 17:29
  • See the `CenterRectangleOnRectangle()` method in the post I linked in the first comment. The fading effect shown in the second link can add opacity level and gamma correction. – Jimi Aug 10 '21 at 18:38

1 Answers1

0

I fixed this problem with this nice peace of code.

 System.Drawing.Image img = System.Drawing.Image.FromFile(JpgFilePath);
                Bitmap jpg = new Bitmap(img);
                filePath = JpgFilePath;
                int Width = jpg.Width;
                int Height = jpg.Height;

                jpg.SetResolution(300, 300);
                WaterMarked = WatermarkImage(jpg, WaterMarkBit, new Point((Width - WaterMarkBit.Width) / 2, (Height - WaterMarkBit.Height) / 2), 0.4F);

                WaterMarked.Save(filePath.Replace(".jpg", "") + ".tif", ImageFormat.Tiff);

                filesJpgForTif.Add(JpgFilePath.Replace("jpg", "tif"));

Using the the sizes of the image and sizes of the watermark and divide it by 2 it makes the image always stay centered.

Tiago Silva
  • 254
  • 2
  • 13