2

I have to upload multiple images using java-script. so i need to compress those images without loose of image Quality.

I have to store all images in phyisical folder"uploads".

HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++) {
    HttpPostedFile hpf = hfc[i];
    if (hpf.ContentLength > 0) {
        hpf.SaveAs(Server.MapPath("~/uploads/") +System.IO.Path.GetFileName(hpf.FileName));
    }
}

So i need to compress the images without loose of image Quality while uploading in physical folder

Naveed Butt
  • 2,861
  • 6
  • 32
  • 55
  • What kind of images? If they are jpegs they are already compressed. – Candide Feb 16 '12 at 11:35
  • Since, you are uploading the images. It would be better, if you could compress the images on the client side via some JQuery plugin... See this for client side compression (http://stackoverflow.com/questions/4579193/uploadify-and-image-compression) – Naveed Butt Feb 16 '12 at 11:37
  • 3
    Most image formats are already compressed. Compressing them further *and* maintaining quality is only possible in the Law & Order crime labs. – Hans Passant Feb 16 '12 at 12:02

1 Answers1

0

I suggest converting the images to PNG and then using the built in ZIP compression.

public static void SaveToPNG(Bitmap SourceImage, string DestinationPath)
{
    SourceImage.Save(DestinationPath, ImageFormat.Png);
    CompressFile(DestinationPath, true);
}

private static string CompressFile(string SourceFile, bool DeleteSourceFile)
{
    string TargetZipFileName = Path.ChangeExtension(SourceFile, ".zip");

    using (ZipArchive archive = ZipFile.Open(TargetZipFileName, ZipArchiveMode.Create))
    {
        archive.CreateEntryFromFile(SourceFile, Path.GetFileName(SourceFile),CompressionLevel.Optimal);
    }

    if(DeleteSourceFile == true)
    {
        File.Delete(SourceFile);
    }
    return TargetZipFileName;
}

OR your if you do not mind a little unnoticable loss, then you could convert to a JPG at a high quality and then ZIP that. At 100% quality, your users will likely not notice any difference and the lower quality you go, the smaller the images will get, but that defeats your "without loss of quality" condition.

private static ImageCodecInfo __JPEGCodecInfo = null;
private static ImageCodecInfo _JPEGCodecInfo
{
    get
    {
        if (__JPEGCodecInfo == null)
        {
            __JPEGCodecInfo = ImageCodecInfo.GetImageEncoders().ToList().Find(delegate (ImageCodecInfo codec) { return codec.FormatID == ImageFormat.Jpeg.Guid; });
        }
        return __JPEGCodecInfo;
    }
}
public static void SaveToJPEG(Bitmap SourceImage, string DestinationPath, long Quality)
{
    EncoderParameters parameters = new EncoderParameters(1);

    parameters.Param[0] = new EncoderParameter(Encoder.Quality, Quality);

    SourceImage.Save(DestinationPath, _JPEGCodecInfo, parameters);

    CompressFile(DestinationPath, true);
}

private static string CompressFile(string SourceFile, bool DeleteSourceFile)
{
    string TargetZipFileName = Path.ChangeExtension(SourceFile, ".zip");

    using (ZipArchive archive = ZipFile.Open(TargetZipFileName, ZipArchiveMode.Create))
    {
        archive.CreateEntryFromFile(SourceFile, Path.GetFileName(SourceFile),CompressionLevel.Optimal);
    }

    if(DeleteSourceFile == true)
    {
        File.Delete(SourceFile);
    }
    return TargetZipFileName;
}
Moon
  • 1,141
  • 2
  • 11
  • 25