简体   繁体   中英

Set watermark on an image and save it in an original quality

I upload a photo on web site and I want to set a watermark on it and save it in original quality. For testing I create C# application.

class Class1
    {
        public static string GetContentType(String path)
        {
            switch (Path.GetExtension(path))
            {
                case ".bmp": return "Image/bmp";
                case ".gif": return "Image/gif";
                case ".jpg": return "Image/jpeg";
                case ".png": return "Image/png";
                default: break;
            }
            return String.Empty;
        }

        public static ImageFormat GetImageFormat(String path)
        {
            switch (Path.GetExtension(path).ToLower())
            {
                case ".bmp": return ImageFormat.Bmp;
                case ".gif": return ImageFormat.Gif;
                case ".jpg": return ImageFormat.Jpeg;
                case ".png": return ImageFormat.Png;
                default: return null;
            }
        }

        public static void AddWaterMark(string sourceFile, string destinationPath)
        {

            // Normally you’d put this in a config file somewhere.
            string watermark = "http://mysite.com/";

            Image image = Image.FromFile(sourceFile);

            Graphics graphic;
            if (image.PixelFormat != PixelFormat.Indexed &&
                image.PixelFormat != PixelFormat.Format8bppIndexed &&
                image.PixelFormat != PixelFormat.Format4bppIndexed &&
                image.PixelFormat != PixelFormat.Format1bppIndexed)
            {

                graphic = Graphics.FromImage(image);
            }
            else
            {

                Bitmap indexedImage = new Bitmap(image);
                graphic = Graphics.FromImage(indexedImage);

                // Draw the contents of the original bitmap onto the new bitmap.
                graphic.DrawImage(image, 0, 0, image.Width, image.Height);
                image = indexedImage;
            }
            graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;

            Font myFont = new Font("Arial", 20);
            SolidBrush brush = new SolidBrush(Color.FromArgb(80, Color.White));

            //This gets the size of the graphic 

            SizeF textSize = graphic.MeasureString(watermark, myFont);

            // Code for writing text on the image. 

            PointF pointF = new PointF(430, 710);
            graphic.DrawString(watermark, myFont, brush, pointF);
            image.Save(destinationPath, GetImageFormat(sourceFile));
            graphic.Dispose();
        }

    }

//And using
class Program
    {

        private static string file1 = "C:\\1.JPG";
        private static string file1_withwatermark = "C:\\1_withwatermark.JPG";
        static void Main(string[] args)
        {
            Class1.AddWaterMark(file1, file1_withwatermark);
        }
}

The resolution of file1 is 3648x2736 , size - 4Mb .

The first thing I don't understand is why does the file1_withwatermark isn't with watermark?

And the second one is why does the size of the file1_withwatermark is 1Mb , however the resolution of it is 3648x2736 too! I want to save file1_withwatermark in original quality, that is the size of file1_withwatermark must be about 4Mb .

Partial answer; Note that JPEG is a lossy compression method. As a result, you will lose actual image quality in the saved image versus the original.

This applies even if you save in a higher quality level, resulting in a larger file than the original.

This is not a huge problem for one or two instances of resaving the file, but just keep this in mind when you consider "quality" and file size of your edited JPEG compressed images.

Two thoughts: 1) Try swapping the save and dispose lines; you might have a pending graphics operation that is not completed before you save. 2) If it's a jpeg img, you need to specify the quality level. See High quality JPEG compression with c#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM