3

The Plan

I need to reduce image sizes, while keeping some quality. However, I am accepting GIF, JPEG, JPG and PNG.


The Current Situation

The uploading of the image is compleetly functional, and I have been able to scale its width and/or height with sucess.

I found an awesome post here on Stackoverflow, that gave me a very good sample of code:

Image myImage = //... load the image somehow 

// Save the image with a quality of 50% SaveJpeg (destImagePath, myImage, 50); //add this! using System.Drawing.Imaging;

                      /// <summary> 
    /// Saves an image as a jpeg image, with the given quality 
    /// </summary> 
    /// <param name="path">Path to which the image would be saved.</param> 
    // <param name="quality">An integer from 0 to 100, with 100 being the 
    /// highest quality</param> 
    public static void SaveJpeg (string path, Image img, int quality) 
    { 
        if (quality<0  ||  quality>100) 
            throw new ArgumentOutOfRangeException("quality must be between 0 and 100."); 


        // Encoder parameter for image quality 
        EncoderParameter qualityParam = 
            new EncoderParameter (Encoder.Quality, quality); 
        // Jpeg image codec 
        ImageCodecInfo   jpegCodec = GetEncoderInfo("image/jpeg"); 

        EncoderParameters encoderParams = new EncoderParameters(1); 
        encoderParams.Param[0] = qualityParam; 

        img.Save (path, jpegCodec, encoderParams); 
    } 

    /// <summary> 
    /// Returns the image codec with the given mime type 
    /// </summary> 
    private static ImageCodecInfo GetEncoderInfo(string mimeType) 
    { 
        // Get image codecs for all image formats 
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 

        // Find the correct image codec 
        for(int i=0; i<codecs.Length; i++) 
            if(codecs[i].MimeType == mimeType) 
                return codecs[i]; 
        return null; 
    } 

However, this code seems to work with JPEG images only.


The Questions

  1. Is JPEG format lighter than PNG or GIF? And if so, should I convert and save all images in JPEG?
    • In this case, the sample code should work just fine right?
    • If not, given the sample code, what can we do use other formats?
  2. What tools can we use in C# to reduce the image weight? (besides reducing the dimensions)
  3. What is the recommended "quality loss" for an image, in order to preserve a reasonable status of readability, color, sharpness and definition?
  4. Is image processing "heavy" on the server?
Community
  • 1
  • 1
Marco
  • 1,272
  • 6
  • 22
  • 33

4 Answers4

3

First of all, JPEG uses a "lossy" compression algorithm, which is why you can tune the quality. Lower quality will result better compression at the cost of higher data loss. Also realize that the JPEG format is designed for photograph-like images, and applying JPEG to other types of images can be disappointing (google "jpeg artifacts").

PNG and GIF are lossless, so they can't support a quality parameter. This isn't to say that you can't get better compression out of them - there's a variety of tools to reduce the size of your PNG's (pngcrush, punypng, etc), but these tools (mostly) work in a fundamentally different way than JPEG compressors do. One thing to note is that GIF supports a smaller palette than PNG - only up to 256 colors.

So to answer your questions in order:

  1. Can't really compare, since compression in JPEG comes with a tradeoff while compression in PNG and GIF doesn't, and how tolerable the tradeoff is depends on the type of image. That is, JPEG will often give better compression, but if the image isn't a photograph the result will be noticeably worse.

  2. Your code for JPEG is fine. Google for C#-compatible tools for PNG.

  3. You'll need to experiment with the types of images you expect.

  4. Yes.

bmm6o
  • 6,187
  • 3
  • 28
  • 55
  • GIF and PNG compress by reducing colors. The loss of color information can be very noticeable for photos – nunespascal Sep 14 '12 at 04:11
  • Yes, I didn't want to get into reducing the pixel depth, which can be effective but can be noticeable. I was thinking more about using different gzip compression levels. – bmm6o Sep 14 '12 at 15:38
  • But you're right, that technique will either be very effective or not acceptable, depending on the input. – bmm6o Sep 14 '12 at 15:40
0

You might want to read my article 20 Image Resizing pitfalls. It covers 3 of your 4 questions.

Lilith River
  • 16,204
  • 2
  • 44
  • 76
0

We can do this by different ways,

one of the best way is convert all images into jpeg,because it will give better clarity with less size,in this we don't need to change any height or width of the particular image

method 1 : convert all images into jpeg(no additional compression needed)

  private static void VaryQualityLevel(Image imgToResize,string imageName)
      {
        // Get a bitmap.
        Bitmap bmp1 = new Bitmap(imgToResize);
        ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

        // Create an Encoder object based on the GUID
        // for the Quality parameter category.
        System.Drawing.Imaging.Encoder myEncoder =
            System.Drawing.Imaging.Encoder.Quality;

        // Create an EncoderParameters object.
        // An EncoderParameters object has an array of EncoderParameter
        // objects. In this case, there is only one
        // EncoderParameter object in the array.
        EncoderParameters myEncoderParameters = new EncoderParameters(1);

        EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,
            50L);
        myEncoderParameters.Param[0] = myEncoderParameter;

        var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Download/"), imageName+".jpeg");
        bmp1.Save(fileSavePath, jgpEncoder,
            myEncoderParameters);

        //myEncoderParameter = new EncoderParameter(myEncoder, 100L);
        //myEncoderParameters.Param[0] = myEncoderParameter;
        //fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Download/"), "TestPhotoQuality100.jpeg");
        //bmp1.Save(fileSavePath, jgpEncoder,
        //    myEncoderParameters);

        // Save the bitmap as a JPG file with 75 quality level compression.
        myEncoderParameter = new EncoderParameter(myEncoder, 75L);
        //myEncoderParameters.Param[0] = myEncoderParameter;
        //fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Download/"), "TestPhotoQuality75.jpeg");
        //bmp1.Save(fileSavePath, jgpEncoder,
        //    myEncoderParameters);

    }

    private static ImageCodecInfo GetEncoder(ImageFormat format)
    {

        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
        }

method 2 :by changing the height and width of the image(without jpeg conversion)

CommonConstant.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EmptyDemo.compression
{
#region[Directive]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion[Directive]

/// <summary>
/// This class is used to get the constants
/// </summary>
public class CommonConstant
{
    public const string JPEG = ".jpeg";
    public const string PNG = ".png";
    public const string JPG = ".jpg";
    public const string BTM = ".btm";
}
}

ImageCompress.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EmptyDemo.compression
{
#region[Directive]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
#endregion[Directive]

/// <summary>
/// This class is used to compress the image to
/// provided size
/// </summary>
public class ImageCompress
{
    #region[PrivateData]
    private static volatile ImageCompress imageCompress;
    private Bitmap bitmap;
    private int width;
    private int height;
    private Image img;
    #endregion[Privatedata]

    #region[Constructor]
    /// <summary>
    /// It is used to restrict to create the instance of the      ImageCompress
    /// </summary>
    private ImageCompress()
    {
    }
    #endregion[Constructor]

    #region[Poperties]
    /// <summary>
    /// Gets ImageCompress object
    /// </summary>
    public static ImageCompress GetImageCompressObject
    {
        get
        {
            if (imageCompress == null)
            {
                imageCompress = new ImageCompress();
            }
            return imageCompress;
        }
    }

    /// <summary>
    /// Gets or sets Width
    /// </summary>
    public int Height
    {
        get { return height; }
        set { height = value; }
    }

    /// <summary>
    /// Gets or sets Width
    /// </summary>
    public int Width
    {
        get { return width; }
        set { width = value; }
    }

    /// <summary>
    /// Gets or sets Image
    /// </summary>
    public Bitmap GetImage
    {
        get { return bitmap; }
        set { bitmap = value; }
    }
    #endregion[Poperties]

    #region[PublicFunction]
    /// <summary>
    /// This function is used to save the image
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="path"></param>
    public void Save(string fileName, string path)
    {
        if (ISValidFileType(fileName))
        {
            string pathaname = path + @"\" + fileName;
            save(pathaname, 60);
        }
    }
    #endregion[PublicFunction]

    #region[PrivateData]
    /// <summary>
    /// This function is use to compress the image to
    /// predefine size
    /// </summary>
    /// <returns>return bitmap in compress size</returns>
    private Image CompressImage()
    {
        if (GetImage != null)
        {
            Width = (Width == 0) ? GetImage.Width : Width;
            Height = (Height == 0) ? GetImage.Height : Height;
            Bitmap newBitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
            newBitmap = bitmap;
            newBitmap.SetResolution(80, 80);
            return newBitmap.GetThumbnailImage(Width, Height, null, IntPtr.Zero);
        }
        else
        {
            throw new Exception("Please provide bitmap");
        }
    }

    /// <summary>
    /// This function is used to check the file Type
    /// </summary>
    /// <param name="fileName">String data type:contain the file name</param>
    /// <returns>true or false on the file extention</returns>
    private bool ISValidFileType(string fileName)
    {
        bool isValidExt = false;
        string fileExt = Path.GetExtension(fileName);
        switch (fileExt.ToLower())
        {
            case CommonConstant.JPEG:
            case CommonConstant.BTM:
            case CommonConstant.JPG:
            case CommonConstant.PNG:
                isValidExt = true;
                break;
        }
        return isValidExt;
    }

    /// <summary>
    /// This function is used to get the imageCode info
    /// on the basis of mimeType
    /// </summary>
    /// <param name="mimeType">string data type</param>
    /// <returns>ImageCodecInfo data type</returns>
    private ImageCodecInfo GetImageCoeInfo(string mimeType)
    {
        ImageCodecInfo[] codes = ImageCodecInfo.GetImageEncoders();
        for (int i = 0; i < codes.Length; i++)
        {
            if (codes[i].MimeType == mimeType)
            {
                return codes[i];
            }
        }
        return null;
    }
    /// <summary>
    /// this function is used to save the image into a
    /// given path
    /// </summary>
    /// <param name="path">string data type</param>
    /// <param name="quality">int data type</param>
    private void save(string path, int quality)
    {
        img = CompressImage();
        ////Setting the quality of the picture
        EncoderParameter qualityParam =
            new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        ////Seting the format to save
        ImageCodecInfo imageCodec = GetImageCoeInfo("image/jpeg");
        ////Used to contain the poarameters of the quality
        EncoderParameters parameters = new EncoderParameters(1);
        parameters.Param[0] = qualityParam;
        ////Used to save the image to a  given path
        img.Save(path, imageCodec, parameters);
    }
    #endregion[PrivateData]
}
}

Here I am uploading image with the help of jquery and web service and i am passing the file as formdata

    [WebMethod]
    public void UploadFile()
     {
         if (HttpContext.Current.Request.Files.AllKeys.Any())
         {
             // Get the uploaded image from the Files collection
             var httpPostedFile = HttpContext.Current.Request.Files["UploadedImage"];
             if (httpPostedFile != null)
             {
                 ImageCompress imgCompress = ImageCompress.GetImageCompressObject;
                 imgCompress.GetImage = new System.Drawing.Bitmap(httpPostedFile.InputStream);
                 imgCompress.Height = 260;
                 imgCompress.Width = 358;
                 //imgCompress.Save(httpPostedFile.FileName, @"C:\Documents and Settings\Rasmi\My Documents\Visual Studio2008\WebSites\compressImageFile\Logo");
                 imgCompress.Save(httpPostedFile.FileName, HttpContext.Current.Server.MapPath("~/Download/"));

             }
         }
     }
user3501613
  • 596
  • 7
  • 28
0

I am using same concept to resize the image without losing quality just by changing scaling factor. In social networking sites after upload images they will reduce the size of the photo but the quality will be same after seen that I tried to write this post

using System.Drawing.Imaging;
using System.Drawing;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        VaryQualityLevel();
    }
    private void VaryQualityLevel()
    {
        Bitmap bmp1 = new Bitmap(Server.MapPath("test2.png"));
        ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
        System.Drawing.Imaging.Encoder myEncoder =System.Drawing.Imaging.Encoder.Quality;
        EncoderParameters myEncoderParameters = new EncoderParameters(1);
        EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        bmp1.Save(Server.MapPath("~/Images/1.jpg"), jgpEncoder, myEncoderParameters);
        //myEncoderParameter = new EncoderParameter(myEncoder, 75L);
        //myEncoderParameters.Param[0] = myEncoderParameter;
        //bmp1.Save(Server.MapPath("~/Images/2.jpg"), jgpEncoder, myEncoderParameters);

    }
    private ImageCodecInfo GetEncoder(ImageFormat format)
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }
}
Papun Sahoo
  • 407
  • 5
  • 13