Converts a bitmap to a JPEG with a specific quality level - CSharp System.Drawing

CSharp examples for System.Drawing:JPEG

Description

Converts a bitmap to a JPEG with a specific quality level

Demo Code


using System.Net;
using System.IO;//from  www.jav  a 2  s . c o  m
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using System;

public class Main{
    /// <summary>Converts a bitmap to a JPEG with a specific quality level</summary>
      /// 
      /// <param name="inputBmp">Bitmap to convert</param>
      /// <param name="quality">Specifies a quality from 0 (lowest) to 100 (highest), or -1 to leave
      /// unspecified</param>
      /// 
      /// <returns>A new bitmap object containing the input bitmap converted.
      ///     If the destination format and the target format are the same, returns
      ///     a clone of the destination bitmap.</returns>
      public static Bitmap ConvertBitmapToJpeg(Bitmap inputBmp, int quality) {
         //If the dest format matches the source format and quality not changing, just clone
         if (inputBmp.RawFormat.Equals(ImageFormat.Jpeg) && quality == -1) {
            return(Bitmap)inputBmp.Clone();
         }

         //Create an in-memory stream which will be used to save
         //the converted image
         System.IO.Stream imgStream = new System.IO.MemoryStream();

         //Get the ImageCodecInfo for the desired target format
         ImageCodecInfo destCodec = FindCodecForType(MimeTypeFromImageFormat(ImageFormat.Jpeg));

         if (destCodec == null) {
            //No codec available for that format
            throw new ArgumentException("The requested format " + 
                                 MimeTypeFromImageFormat(ImageFormat.Jpeg) + 
                                 " does not have an available codec installed", 
                                 "destFormat");
         }

         //Create an EncoderParameters collection to contain the
         //parameters that control the dest format's encoder
         EncoderParameters destEncParams = new EncoderParameters(1);

         //Use quality parameter
         EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);
         destEncParams.Param[0] = qualityParam;

         //Save w/ the selected codec and encoder parameters
         inputBmp.Save(imgStream, destCodec, destEncParams);

         //At this point, imgStream contains the binary form of the
         //bitmap in the target format.  All that remains is to load it
         //into a new bitmap object
         Bitmap destBitmap = new Bitmap(imgStream);

         //Free the stream
         //imgStream.Close();
         //For some reason, the above causes unhandled GDI+ exceptions
         //when destBitmap.Save is called.  Perhaps the bitmap object reads
         //from the stream asynchronously?

         return destBitmap;
      }
    private static ImageCodecInfo FindCodecForType(String mimeType) {
         ImageCodecInfo[] imgEncoders = ImageCodecInfo.GetImageEncoders();

         for (int i = 0; i < imgEncoders.GetLength(0); i++) {
            if (imgEncoders[i].MimeType == mimeType) {
               //Found it
               return imgEncoders[i];
            }
         }

         //No encoders match
         return null;
      }
}

Related Tutorials