2012-02-16 13 views
2

java-scriptを使用して複数の画像をアップロードする必要があります。だから私は画像の品質の緩いことなくそれらの画像を圧縮する必要があります。asp.netで画像を圧縮するにはどうすればいいですか?

すべての画像をphyisicalフォルダ "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)); 
    } 
} 

物理フォルダに

+0

どのような画像ですか?それらがjpegの場合、それらはすでに圧縮されています。 – Candide

+0

あなたは画像をアップロードしています。いくつかのJQueryプラグインを介してクライアント側の画像を圧縮することができれば、より良いでしょう...クライアント側の圧縮についてはこれを参照してください(http://stackoverflow.com/questions/4579193/uploadify-and-image-compression) –

+3

ほとんどの画像フォーマットはすでに圧縮されています。それらをさらに圧縮し*品質を維持することはLaw&Order犯罪研究室でのみ可能です。 –

答えて

0

のアップロード中にので、私はPNGに画像を変換してZIP圧縮に建てを使用することをお勧め画質の緩いせずに画像を圧縮する必要があります。

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; 
} 

たり、あなたは少しunnoticable損失を気にしない場合、その後、あなたは、高品質でJPGに変換することができ、その後、それをZIP。 100%の品質では、ユーザーは違いが気付かれず、品質が低いほどイメージは小さくなりますが、「品質の低下なし」の条件を破ります。

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; 
} 
関連する問題