0

I want to make image size smaller then its original size.I am using following code for compress the size images but it increased the image size from 1MB to 1.5MB
Any Other solution for compress large size images without change image original height,width.

    public static byte[] CompressImage(Image img) {

            int originalwidth = img.Width, originalheight = img.Height;

            Bitmap bmpimage = new Bitmap(originalwidth, originalheight);

            Graphics gf = Graphics.FromImage(bmpimage);
            gf.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            gf.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.AssumeLinear;
            gf.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

            Rectangle rect = new Rectangle(0, 0, originalwidth, originalheight);
            gf.DrawImage(img, rect, 0, 0, originalwidth, originalheight, GraphicsUnit.Pixel);

            byte[] imagearray;

            using (MemoryStream ms = new MemoryStream())
            {
                bmpimage.Save(ms, ImageFormat.Jpeg);
                imagearray= ms.ToArray();
            }

            return imagearray;
        }
Shoaib Ijaz
  • 5,347
  • 12
  • 56
  • 84
  • Can you reword your question? It sounds like you're using "compress" when you mean "downscale" at the same time you're also talking about issues with actual file compression issues with the JPEG file format. – Dai Sep 11 '12 at 12:33
  • I want to make image size smaller without losing quality.this is my question. – Shoaib Ijaz Sep 11 '12 at 12:39

2 Answers2

3

You can set the quality level when you save the file as JPEG, which mostly also directly will correlate with file size - the less quality the smaller your output file will be.

Also see How to: Set JPEG Compression Level , for an example see this SO answer.

Community
  • 1
  • 1
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • I have also try this and working but when i convert it to byte array for save in database it throws exception "Packets larger than max_allowed_packet are not allowed." when i use my code then it is working fine. any idea what will be the issue? – Shoaib Ijaz Sep 11 '12 at 13:10
  • @AskQuestion: That sounds like a MySql error unrelated to your bitmap question - make sure you set the compression to a value that actually *lowers* the output file size - I believe the default quality is 50 - but RTFM :-) – BrokenGlass Sep 11 '12 at 13:16
  • After conversion I got this exception so I ask about it.can you tell me what type of encoding it is i have no idea about this function :) – Shoaib Ijaz Sep 11 '12 at 13:35
0

As said by @BrokenGlass you can specify the compression level within the EncoderParameter. Here's a snippet if you want to give a try changing quality:

public static void SaveJpeg(string path, Image image, int quality)
{
    //ensure the quality is within the correct range
    if ((quality < 0) || (quality > 100))
    {
        //create the error message
        string error = string.Format("Jpeg image quality must be between 0 and 100, with 100 being the highest quality.  A value of {0} was specified.", quality);
        //throw a helpful exception
        throw new ArgumentOutOfRangeException(error);
    }

    //create an encoder parameter for the image quality
    EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
    //get the jpeg codec
    ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

    //create a collection of all parameters that we will pass to the encoder
    EncoderParameters encoderParams = new EncoderParameters(1);
    //set the quality parameter for the codec
    encoderParams.Param[0] = qualityParam;
    //save the image using the codec and the parameters
    image.Save(path, jpegCodec, encoderParams);
}
Giorgio Minardi
  • 2,765
  • 1
  • 15
  • 11