📜  调整图像大小并添加水印c#代码示例

📅  最后修改于: 2022-03-11 14:49:15.380000             🧑  作者: Mango

代码示例1
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);
    }