Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>System.Drawing.Imaging</code> does give you additional control over the image compression if you pass in the JPEG codec and set the encoder parameter for Quality, which is essentially percent retention.</p> <p>Here's a function I use with the parameter <code>"image/jpeg"</code> to get the JPEG codec. (This isn't related to compression <em>per se</em>, but the <code>Image.Save</code> overloads that accept <code>EncoderParameters</code> require <code>ImageCodecInfo</code> instead of <code>ImageFormat</code>.)</p> <pre><code>// assumes an encoder for "image/jpeg" will be available. public static ImageCodecInfo GetCodec( string mimeType ) { ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders(); for( int i = 0; i &lt; encoders.Length; i++ ) if( encoders[i].MimeType == mimeType ) return encoders[i]; return null; } </code></pre> <p>Then you can set the encoder parameters before saving the image.</p> <pre><code>EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter( Encoder.Quality, percentRetention ); // 1-100 ep.Param[1] = new EncoderParameter( Encoder.ColorDepth, colorDepth ); // e.g. 24L </code></pre> <p>(There are other encoder parameters &#8212; see the documentation.</p> <p>So, put it all together, and you can say</p> <pre><code>image.Save( outFile, GetCodec("image/jpeg"), ep ); </code></pre> <p>(I store the codec and parameters in static values, as they are used over and over again, but I wanted to simplify the example here.)</p> <p>Hope this helps!</p> <p><strong>EDIT:</strong> If you are scaling images, you also have some control over the quality. Yes it is very "black box", but I find that it works well. Here are the "good &amp; slow" settings (which you need to set before you call DrawImage, but you can look up the "quick &amp; dirty" versions.</p> <pre><code>// good &amp; slow graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; </code></pre>
 

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