以JPG文件为中心叠加水印PNG

时间:2012-12-07 20:36:22

标签: c# system.drawing

我有一个方法可以获取图像并调整大小并保存它,保留exif信息。我现在要做的是将透明的PNG图像覆盖在图像上方作为水印。 png的大小将始终大于我想要放置的任何图像。我想将它放在图像的顶部,保留水印的纵横比。这是我到目前为止的代码:

private static void ResizeImage(Image theImage, int newSize, string savePath, IEnumerable<PropertyItem> propertyItems)
{
    int width;
    int height;
    CalculateNewRatio(theImage.Width, theImage.Height, newSize, out width, out height);
    using (var b = new Bitmap(width, height))
    {
        using (var g = Graphics.FromImage(b))
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            using(var a = Image.FromFile("Watermark.png"))
            {
                g.DrawImage();  //What to do here?
            }
            g.DrawImage(theImage, new Rectangle(0, 0, width, height));

            var qualityParam = new EncoderParameter(Encoder.Quality, 80L);
            var codecs = ImageCodecInfo.GetImageEncoders();
            var jpegCodec = codecs.FirstOrDefault(t => t.MimeType == "image/jpeg");
            var encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;
            foreach(var item in propertyItems)
            {
                b.SetPropertyItem(item);
            }
            b.Save(savePath, jpegCodec, encoderParams);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

我找到了解决方案,代码如下。可能不是最优化的代码,但它很快并且做我需要做的事情,它将目录中的所有JPG图像重新调整为大小,并在图像上覆盖水印时将照片库的拇指图像重新调整为大小。 / p>

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace ImageResize
{
    internal class Program
    {
        private static readonly string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

        private static void Main()
        {
            var strFiles = Directory.GetFiles(directory, "*.jpg");

            //Using parallel processing for performance
            Parallel.ForEach(strFiles, strFile =>
                                           {
                                               using (var image = Image.FromFile(strFile, true))
                                               {
                                                   var exif = image.PropertyItems;
                                                   var b = directory + "\\" + Path.GetFileNameWithoutExtension(strFile);
                                                   ResizeImage(image, 800, b + "_FULL.jpg", exif);
                                                   ResizeImage(image, 200, b + "_THUMB.jpg", exif);
                                               }
                                               File.Delete(strFile);
                                           });
        }

        private static void ResizeImage(Image theImage, int newSize, string savePath, IEnumerable<PropertyItem> propertyItems)
        {
            try
            {
                int width;
                int height;
                CalculateNewRatio(theImage.Width, theImage.Height, newSize, out width, out height);
                using (var b = new Bitmap(width, height))
                {
                    using (var g = Graphics.FromImage(b))
                    {
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        g.DrawImage(theImage, new Rectangle(0, 0, width, height));

                        //Using FileStream to avoid lock issues because of the parallel processing
                        using (var stream = new FileStream(directory + "\\Watermark.png", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            using (var overLay = Image.FromStream(stream))
                            {
                                stream.Close();
                                int newWidth;
                                int newHeight;
                                CalculateNewRatio(overLay.Width, overLay.Height, height > width ? width : newSize, out newWidth, out newHeight);
                                var x = (b.Width - newWidth) / 2;
                                var y = (b.Height - newHeight) / 2;
                                g.DrawImage(overLay, new Rectangle(x, y, newWidth, newHeight));
                            }
                        }

                        var qualityParam = new EncoderParameter(Encoder.Quality, 80L);
                        var codecs = ImageCodecInfo.GetImageEncoders();
                        var jpegCodec = codecs.FirstOrDefault(t => t.MimeType == "image/jpeg");
                        var encoderParams = new EncoderParameters(1);
                        encoderParams.Param[0] = qualityParam;
                        foreach (var item in propertyItems)
                        {
                            b.SetPropertyItem(item);
                        }
                        b.Save(savePath, jpegCodec, encoderParams);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private static void CalculateNewRatio(int width, int height, int desiredSize, out int newWidth, out int newHeight)
        {
            if ((width >= height && width > desiredSize) || (width <= height && height > desiredSize))
            {
                if (width > height)
                {
                    newWidth = desiredSize;
                    newHeight = height*newWidth/width;
                }
                else if (width < height)
                {
                    newHeight = desiredSize;
                    newWidth = width*newHeight/height;
                }
                else
                {
                    newWidth = desiredSize;
                    newHeight = desiredSize;
                }
            }
            else
            {
                newWidth = width;
                newHeight = height;
            }
        }
    }
}

答案 1 :(得分:0)

要在保持纵横比的同时将图像缩放到画布上以获得最佳效果,过程非常简单:

double widthFactor = b.Width / a.Width;
double heightFactor = b.Height / a.Height;
double scaleFactor = Math.Min(widthFactor, heightFactor);
int newWidth = a.Width * scaleFactor;
int newHeight = a.Width * scaleFactor;

要计算图像居中的位置,只需要更多的数学运算:

int left = (b.Width - newWidth) / 2;
int top = (b.Height - newHeight) / 2;

然后使用适当形式的Graphics.DrawImage方法。