使用 c# 进行高质量 JPEG 压缩
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1669850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
High quality JPEG compression with c#
提问by Baran
I am using C# and want to save images using JPEG format. However .NET reduces quality of the images and saves them with compression that is not enough.
我正在使用 C# 并希望使用 JPEG 格式保存图像。然而,.NET 会降低图像的质量,并以不够的压缩方式保存它们。
I want to save files with their original quality and size. I am using the following code but compression and quality are not like the original ones.
我想以原始质量和大小保存文件。我正在使用以下代码,但压缩和质量与原始代码不同。
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);
I am archiving studio photos and quality and compression is very important. Thanks.
我正在存档工作室照片,质量和压缩非常重要。谢谢。
采纳答案by Michael Todd
It looks like you're setting the quality to 100%. That means that there will be no compression.
看起来您将质量设置为 100%。这意味着不会有压缩。
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. LEADToolshas a good (non-free) engine.
如果您更改压缩级别(80、50 等)并且对质量不满意,您可能需要尝试不同的图像库。LEADTools有一个很好的(非免费)引擎。
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.
更新:正如评论者所提到的,使用 JPEG 时,100% 质量仍然不意味着无损压缩。加载图像,对其进行处理,然后再次保存,最终会导致图像质量下降。如果您需要在不丢失任何数据的情况下更改和保存图像,则需要使用无损格式,例如 TIFF、PNG 或 BMP。我会使用压缩的 TIFF(因为即使它被压缩仍然是无损的)或 PNG。
回答by John Gietzen
Compression and quality are always a trade off.
压缩和质量始终是一种权衡。
JPEGs are always going to be lossy.
JPEG 总是有损的。
You may want to consider using PNG and minifying the files using PNGCrush or PNGauntlet
您可能需要考虑使用 PNG 并使用 PNGCrush 或PNGauntlet缩小文件
回答by Ed S.
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.)
这没有多大意义。当您使用有损压缩时,根据定义,您将丢失一些信息。压缩图像的目的是减小文件大小。如果您需要高质量并且 jpeg 不适合您,您可能需要使用某种类型的无损压缩,但您的文件大小不会减少太多。您总是可以尝试使用“标准”库来压缩为 jpeg ( libjpeg)并查看是否会给您带来任何不同的结果(我对此表示怀疑,但我不知道 .NET 在后台使用的是什么。)
回答by C. Ross
回答by johngrinder
Regarding the setup of the compression level in .NET, please check this link (everything included): http://msdn.microsoft.com/en-us/library/bb882583.aspx
关于 .NET 中压缩级别的设置,请查看此链接(包括所有内容):http: //msdn.microsoft.com/en-us/library/bb882583.aspx
Rearding your question: Usually you will save the uploaded image from users as PNG, then you use this PNG as base to generate your JPGs with different sizes (and you put a watermark ONLY on the JPGs, never on the original PNG!) Advantage of this is: if you change your images-dimensions later on for your platform, you have the original PNG saved and based on this you can re-compute any new image sizes.
回答你的问题:通常你会把用户上传的图片保存为 PNG,然后你使用这个 PNG 作为基础来生成不同大小的 JPG(你只在 JPG 上加水印,不要在原始 PNG 上加水印!)这是:如果您稍后为您的平台更改图像尺寸,您将保存原始 PNG,并基于此重新计算任何新的图像尺寸。
回答by Chris Moschini
The .Net encoder built-in to the library (at least the default Windows library provided by Microsoft) is pretty bad:
库中内置的 .Net 编码器(至少是 Microsoft 提供的默认 Windows 库)非常糟糕:
http://b9dev.blogspot.com/2013/06/nets-built-in-jpeg-encoder-convenient.html
http://b9dev.blogspot.com/2013/06/nets-built-in-jpeg-encoder-convenient.html
Partial Update
部分更新
I'm now using an approach outlined here, that uses ImageMagick for the resize then jpegoptim for the final compression, with far better results. I realize that's a partial answer but I'll expand on this once time allows.
我现在使用这里概述的方法,它使用 ImageMagick 进行调整大小,然后使用 jpegoptim 进行最终压缩,结果要好得多。我意识到这是部分答案,但一旦时间允许,我会对此进行扩展。
Older Answer
较旧的答案
ImageMagick is the best choice I've found so far. It performs relatively solid jpeg compression.
ImageMagick 是迄今为止我发现的最佳选择。它执行相对可靠的 jpeg 压缩。
It has a couple downsides:
它有几个缺点:
It's better but not perfect. In particular, its Chroma subsampling is set to high detail at 90% or above, then jumps down to a lower detail level - one that can introduce a lot of artifacts. If you want to ignore subsampling, this is actually pretty convenient. But if you wanted high-detail subsampling at say, 50%, you have a larger challenge ahead. It also still won't quite hit quality/compression levels of Photoshop or Google PageSpeed.
It has a special deployment burden on the server that's very easy to miss. It requires a Visual Studio 2008 SDK lib installed. This lib is available on any dev machine with Visual Studio on it, but then you hit the server for the first time and it implodes with an obscure error. It's one of those lurking gotchas most people won't have scripted/automated, and you'll trip over it during some future server migration.
它更好但并不完美。特别是,它的色度子采样设置为 90% 或更高的高细节,然后跳到较低的细节级别 - 一个可以引入很多伪影的级别。如果你想忽略子采样,这实际上很方便。但是,如果您想要 50% 的高细节子采样,那么您将面临更大的挑战。它也不会完全达到 Photoshop 或 Google PageSpeed 的质量/压缩级别。
它对服务器有特殊的部署负担,很容易被忽略。它需要安装 Visual Studio 2008 SDK 库。这个库可以在任何带有 Visual Studio 的开发机器上使用,但是当你第一次访问服务器时,它会因一个模糊的错误而崩溃。这是大多数人不会编写脚本/自动化的潜伏问题之一,您将在未来的服务器迁移过程中被它绊倒。
Oldest Answer
最旧的答案
I dug around and came across a project to implement a C# JPEG encoder by translating a C project over:
我四处寻找并发现了一个通过翻译 C 项目来实现 C# JPEG 编码器的项目:
http://www.codeproject.com/Articles/83225/A-Simple-JPEG-Encoder-in-C
http://www.codeproject.com/Articles/83225/A-Simple-JPEG-Encoder-in-C
which I've simplified slightly:
我稍微简化了:
https://github.com/b9chris/ArpanJpegEncoder
https://github.com/b9chris/ArpanJpegEncoder
It produces much higher quality JPEGs than the .Net built-in, but still is not as good as Gimp's or Photoshop's. Filesizes also tend to be larger.
它生成的 JPEG 质量比内置的 .Net 高得多,但仍然不如 Gimp 或 Photoshop 的好。文件大小也往往更大。
BitMiracle's implementationis practically identical to the .Net built-in - same quality problems.
BitMiracle 的实现实际上与 .Net 内置的相同 - 相同的质量问题。
It's likely that just wrapping an existing open source implementation, like Google's jpeg_optimizer in PageSpeed Tools- seemingly libjpegunderneath, would be the most efficient option.
很可能只是包装一个现有的开源实现,比如Google 在 PageSpeed Tools 中的 jpeg_optimizer- 看似下面的libjpeg,将是最有效的选择。
Update
更新
ArpanJpegEncoder appears to have issues once it's deployed - maybe I need to increase the trust level of the code, or perhaps something else is going on. Locally it writes images fine, but once deployed I get a blank black image from it every time. I'll update if I determine the cause. Just a warning to others considering it.
ArpanJpegEncoder 在部署后似乎会出现问题 - 也许我需要提高代码的信任级别,或者其他事情正在发生。在本地,它可以很好地写入图像,但是一旦部署,我每次都会从中获得一个空白的黑色图像。如果我确定原因,我会更新。只是对其他考虑它的人的警告。