views:

1567

answers:

4

I want to save images with c# but while using .net jpeg format it reduces quality and compression is not enough. It must save the file like its orjinal quality and size. I am using this code but It has not enough quality and compression. I am archiving studio photos and quality and compression is very important. Thanks.

Bitmap bm = (Bitmap)Image.FromFile(FilePath); 
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 
        ImageCodecInfo ici = null; 
        foreach (ImageCodecInfo codec in codecs)
        { 
            if (codec.MimeType == "image/jpeg") 
                ici = codec; 
        } 

        EncoderParameters ep = new EncoderParameters(); 
        ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100); 
            bm.Save("C:\\quality" + x.ToString() + ".jpg", ici, ep);
+3  A: 

Compression and quality are always a trade off.

JPEGs are always going to be lossy.

You may want to consider using PNG and minifying the files using PNGCrush or PNGauntlet

John Gietzen
A: 

It must save the file like its orjinal quality and size

That doesn't make a lot of sense. When you are using lossy compression you are going to lose some information by definition. The point of compressing an image is to reduce the file size. If you need high quality and jpeg isn't doing it for you you may have to go with some type of lossless compression, but your file sizes will not be reduced by much. You could always try using the 'standard' library for compressing to jpeg (libjpeg) and see if that gives you any different results (I doubt it, but I don't know what .NET is using under the hood.)

Ed Swangren
A: 

Compressing the jpeg format by its very nature reduces quality. Perhaps you should look into file compression, such as #ziplib. You may be able to get a reasonable compression over a group of files.

C. Ross
+2  A: 

It looks like you're setting the quality to 100%. That means that there will be no compression.

If you change the compression level (80, 50, etc.) and you're unsatisifed with the quality, you may want to try a different image library. LEADTools has a good (non-free) engine.

UPDATE: As a commenter mentioned, 100% quality still does not mean lossless compression when using JPEG. Loading the image, doing something to it, and then saving it again will ultimately result in image degradation. If you need to alter and save an image without losing any of the data you need to use a lossless format such as TIFF, PNG or BMP. I'd go with compressed TIFF (since it's still lossless even though it's compressed) or PNG.

Michael Todd
Setting quality to 100% does not mean it's lossless, when it comes to .NET JPG encoding. I personally use the above mentioned LEADTools (FREE!) JPG encoder from my .NET code. It's an executable to which I provide an image source, launch the executable, and then read the result back into memory for further manipulation.
Andy