如何将图像保存为具有给定输出文件大小的PNG或JPG文件? (首先在C#中)

时间:2021-01-14 21:23:17

I'd like to save an existing image as a PNG or JPG at a given file size, eg, 100KB.

我想将现有图像保存为给定文件大小的PNG或JPG,例如100KB。

3 个解决方案

#1


PNG uses lossless compression so you can not compress it below a certain level.

PNG使用无损压缩,因此您无法将其压缩到一定水平以下。

In .NET you can save a JPG with compression, and guess how big the file will be when completed.

在.NET中,您可以使用压缩保存JPG,并猜测完成后文件的大小。

http://msdn.microsoft.com/en-us/library/system.drawing.image.save(VS.80).aspx - See the "Save JPEG image with compression value" section.

http://msdn.microsoft.com/en-us/library/system.drawing.image.save(VS.80).aspx - 请参阅“使用压缩值保存JPEG图像”部分。

Also, you could resize the image dimensions to make it smaller.

此外,您可以调整图像尺寸以使其更小。

#2


Only if using JPG 2000 you could set the file size to a specific value. Using JPG, you'll have to try different quality values and using PNG will get you one size for a given image and a compression level - you can only try to resize the image which will give you a smaller size.

仅在使用JPG 2000时,您才能将文件大小设置为特定值。使用JPG,您将不得不尝试不同的质量值,使用PNG将为您提供给定图像和压缩级别的一个尺寸 - 您只能尝试调整图像大小,这样可以缩小尺寸。

You could also try to resize the image so that an uncompressed image would have the size you want, but then PNG and especially JPG will often have a much lower file size.

您还可以尝试调整图像大小,以便未压缩的图像具有您想要的大小,但是PNG,尤其是JPG通常具有更低的文件大小。

#3


For PNG there isn't really a quality setting, so you can't really control the file size.

对于PNG,没有真正的质量设置,因此您无法真正控制文件大小。

Jpg has a quality setting that determines how good a quality the image will be. lower quality settings result in smaller files. However, there is normally no option for "give the quality needed for a file of size x".

Jpg的质量设置决定了图像的质量。较低的质量设置会导致较小的文件。但是,通常没有“提供x大小的文件所需的质量”的选项。

You can achieve the same result using a rather inefficient approach of converting to jpg in memory, seeing how big the output is, adjusting the quality up or down and repeating until you get close enough. It might sound terrible, but if your images aren't too big you may find no one notices the short delay while you do this.

您可以使用一种相当低效的方法在内存中转换为jpg,查看输出的大小,向上或向下调整质量并重复直到您足够接近,从而获得相同的结果。这可能听起来很糟糕,但如果你的图像不是太大,你可能会发现没有人注意到你这么做时的短暂延迟。

#1


PNG uses lossless compression so you can not compress it below a certain level.

PNG使用无损压缩,因此您无法将其压缩到一定水平以下。

In .NET you can save a JPG with compression, and guess how big the file will be when completed.

在.NET中,您可以使用压缩保存JPG,并猜测完成后文件的大小。

http://msdn.microsoft.com/en-us/library/system.drawing.image.save(VS.80).aspx - See the "Save JPEG image with compression value" section.

http://msdn.microsoft.com/en-us/library/system.drawing.image.save(VS.80).aspx - 请参阅“使用压缩值保存JPEG图像”部分。

Also, you could resize the image dimensions to make it smaller.

此外,您可以调整图像尺寸以使其更小。

#2


Only if using JPG 2000 you could set the file size to a specific value. Using JPG, you'll have to try different quality values and using PNG will get you one size for a given image and a compression level - you can only try to resize the image which will give you a smaller size.

仅在使用JPG 2000时,您才能将文件大小设置为特定值。使用JPG,您将不得不尝试不同的质量值,使用PNG将为您提供给定图像和压缩级别的一个尺寸 - 您只能尝试调整图像大小,这样可以缩小尺寸。

You could also try to resize the image so that an uncompressed image would have the size you want, but then PNG and especially JPG will often have a much lower file size.

您还可以尝试调整图像大小,以便未压缩的图像具有您想要的大小,但是PNG,尤其是JPG通常具有更低的文件大小。

#3


For PNG there isn't really a quality setting, so you can't really control the file size.

对于PNG,没有真正的质量设置,因此您无法真正控制文件大小。

Jpg has a quality setting that determines how good a quality the image will be. lower quality settings result in smaller files. However, there is normally no option for "give the quality needed for a file of size x".

Jpg的质量设置决定了图像的质量。较低的质量设置会导致较小的文件。但是,通常没有“提供x大小的文件所需的质量”的选项。

You can achieve the same result using a rather inefficient approach of converting to jpg in memory, seeing how big the output is, adjusting the quality up or down and repeating until you get close enough. It might sound terrible, but if your images aren't too big you may find no one notices the short delay while you do this.

您可以使用一种相当低效的方法在内存中转换为jpg,查看输出的大小,向上或向下调整质量并重复直到您足够接近,从而获得相同的结果。这可能听起来很糟糕,但如果你的图像不是太大,你可能会发现没有人注意到你这么做时的短暂延迟。