Answers for "resize image and add watermark c#"

C#
0

resize image and add watermark c#

void waterMarkOnBottomRight(Image img, Image watermarkImage, string saveFileName)
    {
        double imageHeightBrand = Convert.ToDouble(watermarkImage.Height);
        double imageWidthBrand = Convert.ToDouble(watermarkImage.Width);
        double ratioBrand = imageWidthBrand / imageHeightBrand;

        double imageHeightBild = Convert.ToDouble(img.Height); //height of the image to watermark
        double imageWidthBild = Convert.ToDouble(img.Width);
        var imageWidthTmpBranding = imageWidthBild * 0.2; //the watermark width, but only 20% size of the image to watermark
        var imageHeightTmpBranding = imageWidthTmpBranding / ratioBrand; //height of watermark, preserve aspect ratio
        int imageWidthBranding = Convert.ToInt32(imageWidthTmpBranding); //convert in into int32 (see method below)
        int imageHeightBranding = Convert.ToInt32(imageHeightTmpBranding);

        int watermarkX = (int)(imageWidthBild - imageWidthBranding); // Bottom Right
        int watermarkY = (int)(imageHeightBild - imageHeightBranding);

        using (Graphics g = Graphics.FromImage(img)) 
            g.DrawImage(watermarkImage,
                new Rectangle(watermarkX, watermarkY, imageWidthBranding, imageHeightBranding),
                new Rectangle(0, 0, (int)imageWidthBrand, (int)imageHeightBrand),
                GraphicsUnit.Pixel);
        img.Save(saveFileName);
    }
Posted by: Guest on March-30-2021

C# Answers by Framework

Browse Popular Code Answers by Language