使用 c# 进行高质量 JPEG 压缩 [英] High quality JPEG compression with c#

查看:86
本文介绍了使用 c# 进行高质量 JPEG 压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 C# 并想使用 JPEG 格式保存图像.然而,.NET 降低了图像的质量,并以不够的压缩方式保存它们.

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.

推荐答案

您似乎将质量设置为 100%.这意味着不会有压缩.

It looks like you're setting the quality to 100%. That means that there will be no compression.

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

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.

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

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.

这篇关于使用 c# 进行高质量 JPEG 压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆