使用 C# 进行高质量 JPEG 压缩

发布于 2024-08-09 16:11:15 字数 611 浏览 4 评论 0 原文

我正在使用 C# 并希望使用 JPEG 格式保存图像。然而,.NET 降低了图像的质量,并以不够压缩的方式保存图像。

我想以原始质量和大小保存文件。我正在使用以下代码,但压缩和质量与原始代码不同。

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 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

去了角落 2024-08-16 16:11:15

库中内置的 .Net 编码器(至少是 Microsoft 提供的默认 Windows 库)非常糟糕:

http://b9dev.blogspot.com/2013/06/nets-built-in-jpeg-encoder-convenient.html

部分更新

我现在使用此处概述的方法,该方法使用 ImageMagick 进行调整大小,然后使用 jpegoptim 进行最终压缩,效果要好得多。我意识到这只是部分答案,但一旦时间允许,我会对此进行扩展。

较旧的答案

ImageMagick 是迄今为止我发现的最佳选择。它执行相对可靠的 jpeg 压缩。

http://magick.codeplex.com/

它有一些缺点:

  1. 它更好但并不完美。特别是,其色度子采样设置为 90% 或以上的高细节,然后跳至较低的细节级别 - 这可能会引入大量伪影。如果你想忽略子采样,这实际上非常方便。但如果您想要高细节二次采样(例如 50%),那么您将面临更大的挑战。它仍然无法完全达到 Photoshop 或 Google PageSpeed 的质量/压缩水平。

  2. 它对服务器有特殊的部署负担,很容易被忽视。它需要安装 Visual Studio 2008 SDK 库。这个库在任何装有 Visual Studio 的开发机器上都可用,但是当你第一次访问服务器时,它就会崩溃并出现一个模糊的错误。这是大多数人不会编写脚本/自动化的潜伏陷阱之一,在未来的服务器迁移过程中您会被它绊倒。

最古老的答案

我四处寻找并发现了一个通过翻译 C 项目来实现 C# JPEG 编码器的项目:

http://www.codeproject.com/Articles/83225/A-Simple-JPEG-Encoder-in-C

我稍微简化了:

https://github.com/b9chris/ArpanJpegEncoder

它生成的 JPEG 质量比 .Net 内置的要高得多,但仍然如此不如 Gimp 或 Photoshop 的好。文件大小也往往更大。

BitMiracle 的实现实际上与 .Net 内置的相同 - 相同的质量问题。

很可能只是包装现有的开源实现,例如 PageSpeed Tools 中的 Google jpeg_optimizer - 看起来libjpeg 下面,将是最有效的选择。

更新

ArpanJpegEncoder 在部署后似乎出现问题 - 也许我需要提高代码的信任级别,或者可能发生了其他问题。在本地,它可以很好地写入图像,但是一旦部署,我每次都会从中得到一个空白的黑色图像。如果确定原因,我会更新。只是对其他正在考虑这一点的人的警告。

The .Net encoder built-in to the library (at least the default Windows library provided by Microsoft) is pretty bad:

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.

Older Answer

ImageMagick is the best choice I've found so far. It performs relatively solid jpeg compression.

http://magick.codeplex.com/

It has a couple downsides:

  1. 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.

  2. 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.

Oldest Answer

I dug around and came across a project to implement a C# JPEG encoder by translating a C project over:

http://www.codeproject.com/Articles/83225/A-Simple-JPEG-Encoder-in-C

which I've simplified slightly:

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.

BitMiracle's implementation is practically identical to the .Net built-in - same quality problems.

It's likely that just wrapping an existing open source implementation, like Google's jpeg_optimizer in PageSpeed Tools - seemingly libjpeg underneath, would be the most efficient option.

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.

杀お生予夺 2024-08-16 16:11:15

看来您将质量设置为 100%。这意味着不会有压缩。

如果您更改压缩级别(80、50 等)并且您对质量不满意,您可能需要尝试不同的图像库。 LEADTools 拥有一个良好的(非免费)引擎。

更新:正如评论者提到的,使用 JPEG 时 100% 质量仍然并不意味着无损压缩。加载图像,对其进行处理,然后再次保存,最终会导致图像质量下降。如果您需要更改和保存图像而不丢失任何数据,则需要使用无损格式,例如 TIFF、PNG 或 BMP。我会选择压缩的 TIFF(因为即使经过压缩,它仍然是无损的)或 PNG。

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.

∝单色的世界 2024-08-16 16:11:15

压缩和质量始终是一个权衡。

JPEG 总是有损的。

您可能需要考虑使用 PNG 并使用 PNGCrush 或 PNGauntlet 缩小文件

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

当爱已成负担 2024-08-16 16:11:15

关于 .NET 中压缩级别的设置,请检查此链接(包含所有内容):http://msdn.microsoft.com/en-us/library/bb882583.aspx

阅读您的问题:
通常你会将用户上传的图像保存为PNG,然后使用这个PNG作为基础来生成不同尺寸的JPG(并且你只在JPG上添加水印,而不是在原始PNG上添加水印!)
这样做的优点是:如果您稍后更改平台的图像尺寸,您将保存原始 PNG,并基于此您可以重新计算任何新的图像尺寸。

Regarding the setup of the compression level in .NET, please check this link (everything included): 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.

梦幻的心爱 2024-08-16 16:11:15

它必须像其原始质量和大小一样保存文件

这没有多大意义。当您使用有损压缩时,根据定义您将丢失一些信息。压缩图像的目的是减小文件大小。如果您需要高质量,而 jpeg 无法满足您的要求,您可能必须使用某种类型的无损压缩,但您的文件大小不会减少太多。您始终可以尝试使用“标准”库压缩为 jpeg (libjpeg) 并查看是否这会给你任何不同的结果(我对此表示怀疑,但我不知道 .NET 在幕后使用什么。)

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.)

守不住的情 2024-08-16 16:11:15

压缩 jpeg 格式本质上会降低质量。也许您应该研究一下文件压缩,例如 #ziplib。您也许能够对一组文件进行合理的压缩。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文