1

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.

Alexandre
  • 13,030
  • 35
  • 114
  • 173

1 Answers1

2

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.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • <> but the original image is JPEG too! – Alexandre Oct 16 '11 at 16:51
  • Yes. Each time you save a file with JPEG compression, some quality is lost. It makes zero difference what the original file was, it has to do with what you are saving as. Like I said; the quality loss is probably not a big deal in your application's case, but keep in mind that's why there will appear to be differences in 'quality' and 'image size'. Even with a huge image with JPEG, you will be losing some quality from the original. – Andrew Barber Oct 17 '11 at 15:18