在 C# 中调整大小和水印图像

Resizing and Watermark Image in C#

我想在一个文件夹中给不同尺寸的图片加水印。水印有 3891 x 4118 像素。我要加水印的图片大小几乎相同或更小。

但是,水印图像在图像上的大小应始终相同。因此,我采用要加水印的图像宽度,将其乘以 0.2 (20%),然后水印图像的高度按比例计算。 (见下面的代码)。

之后,我调整水印图像的大小并将其放在我想要加水印的图像上。到目前为止一切顺利,但问题是图像比应有的小得多。计算工作正常,即使我说按原样放置图像(3891 x 4118 像素),它也可以正确计算,但水印不会变大。它保持在我没有计算的尺寸上。

这是加载文件夹的按钮。

 private void DateiLaden_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult result = fbd.ShowDialog();


            try
            {
                string[] files = GetFiles(fbd.SelectedPath.ToString(), "*.jpg|*.jpeg"); //Only select JPG pictures, method below

                int anzahlBilder = files.Length;
                if (anzahlBilder <= 0)
                {
                    System.Windows.Forms.MessageBox.Show("No images!");
                } // if there are no images, stop processing...
                else
                {
                    var res = checkImageSize(files); //check if they have a minmal size, method below
                    if (res == "") //if everythings fine, continue
                    {
                        Directory.CreateDirectory(fbd.SelectedPath.ToString() + "\watermarked"); //create new directory of the selected folder, folder so save the watermarked images


                        System.Drawing.Image brand = System.Drawing.Image.FromFile("../../watermark.png"); //load watermark
                        double imageHeightBrand = Convert.ToDouble(brand.Height); //get height (4118px)
                        double imageWidthBrand = Convert.ToDouble(brand.Width); //get width (3891px)
                        double ratioBrand = imageWidthBrand / imageHeightBrand; //get ratio (0.94487615347)

                        foreach (var file in files)
                        {
                            System.Drawing.Image img = System.Drawing.Image.FromFile(file); //load image to watermerk to get width and height

                            double imageHeightBild = Convert.ToDouble(img.Height); //height of the image to watermark
                            double imageWidthBild = Convert.ToDouble(img.Width);  //width of the image to watermark                 

                             //Landscape
                             if (imageWidthBild > 640.0 && imageHeightBild > 425.0)
                             {
                                 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); //convert in into int32 (see method below)


                                 var watermark = ResizeImage(brand, imageWidthBranding, imageHeightBranding); //resize temporally the watermark image, method below

                                 System.Drawing.Image image = System.Drawing.Image.FromFile(file); //get image to watermark
                                 Graphics g = Graphics.FromImage(image); //load is as graphic
                                 g.DrawImage(watermark, new System.Drawing.Point(50, 50)); //draw the watermark on it
                                 image.Save(fbd.SelectedPath.ToString() + "\watermarked\" + returnOnlyImageName(file)); //save it in the folder with the same file name, see method below.
                             }

                            //Portrait
                           /* if(imageWidthBild > 350 && imageHeightBild > 520) {

                            }*/

                        }
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show(res);
                    }

                }

            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }


        }

"methods below"

 private static string[] GetFiles(string sourceFolder, string filters)
        {
            return filters.Split('|').SelectMany(filter => System.IO.Directory.GetFiles(sourceFolder, filter)).ToArray();
        }

        private static string checkImageSize(string[] files)
        {

            string message = "The following pictures are too small:\n";


            foreach (var file in files)
            {
                Bitmap img = new Bitmap(file);

                var imageHeight = img.Height;
                var imageWidth = img.Width;

                if ((imageWidth < 640 && imageHeight < 425) || (imageWidth < 350 && imageHeight < 520))
                {
                    message += returnOnlyImageName(file) + "\n";
                }
            }

            if (message == "The following pictures are too small:\n")
            {
                return "";
            }
            else
            {
                message += "\nPlease change those pictures!";
                return message;
            }

        }

        private static string returnOnlyImageName(string file)
        {
            return file.Substring(file.LastIndexOf("\")+1);
        }

        public static Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
        {
            var destRect = new System.Drawing.Rectangle(0, 0, width, height);
            var destImage = new Bitmap(width, height);

            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                using (var wrapMode = new ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }

            return destImage;
        }

右下角的水印是不是也可以保存下来?

感谢问候

给你...

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

这个正在为横向(宽度 > 高度)工作。

好吧.. 在您的代码中,您不必为图像创建单独的实例来计算、创建图形、添加新位图和使用图形绘制等等。你对图像做任何事情,它在内存中。而且它不会影响磁盘上的水印图像。