C# 如何用asp.net压缩图像而不丢失图像质量

C# 如何用asp.net压缩图像而不丢失图像质量,c#,C#,我必须使用java脚本上传多个图像。因此,我需要压缩这些图像,而不影响图像质量 我必须在Physical文件夹“uploads”中存储所有图像 HttpFileCollection hfc=Request.Files; 对于(int i=0;i0){ SaveAs(Server.MapPath(“~/uploads/”)+System.IO.Path.GetFileName(hpf.FileName)); } } 因此,我需要在上传到物理文件夹时压缩图像而不降低图像质量我建议将图像转换为PNG

我必须使用java脚本上传多个图像。因此,我需要压缩这些图像,而不影响图像质量

我必须在Physical文件夹“uploads”中存储所有图像

HttpFileCollection hfc=Request.Files;
对于(int i=0;i0){
SaveAs(Server.MapPath(“~/uploads/”)+System.IO.Path.GetFileName(hpf.FileName));
}
}

因此,我需要在上传到物理文件夹时压缩图像而不降低图像质量

我建议将图像转换为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;
}
或者,如果你不介意一些不明显的损失,那么你可以转换成高质量的JPG,然后压缩它。在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;
}

我建议将图像转换为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;
}
或者,如果你不介意一些不明显的损失,那么你可以转换成高质量的JPG,然后压缩它。在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;
}

什么样的图像?如果它们是JPEG,它们已经被压缩。因为,您正在上载图像。如果您可以通过JQuery插件在客户端压缩图像,那就更好了。。。有关客户端压缩()的信息,请参见本文。大多数图像格式都已压缩。只有在治安犯罪实验室才能进一步压缩图像并保持质量。什么样的图像?如果它们是JPEG,它们已经被压缩。因为,您正在上载图像。如果您可以通过JQuery插件在客户端压缩图像,那就更好了。。。有关客户端压缩()的信息,请参见本文。大多数图像格式都已压缩。只有在治安犯罪实验室才能进一步压缩和保持质量。