如何在不知道传入图像的大小的情况下让我的矩形始终位于图像的中间

how to have my rectangle always on the middle of the image without knowing the size of the incomming image

尝试将水印图像添​​加到 png 图像,我已经能够做到,但我想去掉水印矩形的硬编码尺寸规则,并使其始终位于中心的图像。我怎样才能做到这一点。

public Form1()
    {
        InitializeComponent();
        picBox.Parent = this;
        picBox.Dock = DockStyle.Fill;
        picBox.SizeMode = PictureBoxSizeMode.Zoom;
        Bitmap Jpg = new Bitmap(@"C:\Users\tferreira\Desktop3123.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;
    }

现在图像是硬编码的,但将被删除。如果有人可以帮助我让这个水印始终居中,谢谢。

我用这种非常安静的代码解决了这个问题。

 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"));

使用图像的大小和水印的大小并将其除以 2,使图像始终居中。