Note that there are some explanatory texts on larger screens.

plurals
  1. POSet watermark on an image and save it in an original quality
    primarykey
    data
    text
    <p>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.</p> <pre><code>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 &amp;&amp; image.PixelFormat != PixelFormat.Format8bppIndexed &amp;&amp; image.PixelFormat != PixelFormat.Format4bppIndexed &amp;&amp; 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 &amp; 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); } } </code></pre> <p>The resolution of file1 is <code>3648x2736</code>, size - <code>4Mb</code>.</p> <p>The first thing I don't understand is why does the <code>file1_withwatermark</code> isn't with watermark?</p> <p>And the second one is why does the size of the <code>file1_withwatermark</code> is <code>1Mb</code>, however the resolution of it is <code>3648x2736</code> too! I want to save <code>file1_withwatermark</code> in original quality, that is the size of <code>file1_withwatermark</code> must be about <code>4Mb</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload