如何在 C# 中将图像大小调整为特定的硬盘大小?
如何将 C# 中的图像大小调整为特定的硬盘大小,例如 2MiB? 有没有比反复试验更好的方法(当然,即使它是近似的)。
尝试在网络上查找解决方案时需要搜索哪些特定关键字?
How to resize an image an image in C# to a certain hard-disk size, like 2MiB? Is there a better way than trial and error (even if it's approximate, of course).
Any particular keywords to search for when trying to find the solution on the web?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过原始图像大小除以像素数来计算图像的近似信息级别:
我有一个 369636 字节和 1200x800 像素的图像,因此每个像素使用约 0.385 字节。
我有一个较小的版本,为 101111 字节和 600x400 像素,因此每个像素使用约 0.4213 字节。
当缩小图像时,您会发现它通常每个像素包含的信息会稍微多一点,在本例中大约多 9%。 根据您的图像类型以及缩小图像的程度,您应该能够计算出信息/像素比增加的平均值 (c),以便您可以计算近似的文件大小:
从中您可以提取必须制作多大图像才能达到特定文件大小的公式:
这将使您非常接近所需的文件大小。 如果您想更接近,您可以将图像大小调整为计算的大小,压缩它并根据您获得的文件大小计算新的每像素字节值。
You can calculate an approximate information level for the image by taking the original image size divided by the number of pixels:
I have an image that is 369636 bytes and 1200x800 pixels, so it uses ~0.385 bytes per pixel.
I have a smaller version that is 101111 bytes and 600x400 pixels, so it uses ~0.4213 bytes per pixel.
When you shrink an image you will see that it generally will contain slightly more information per pixel, in this case about 9% more. Depending on your type of images and how much you shrink them, you should be able to calculate an average for how much the information/pixel ration increases (c), so that you can calculate an approximate file size:
From this you can extract a formula for how large you have to make an image to reach a specific file size:
This will get you pretty close to the desired file size. If you want to get closer you can resize the image to the calculated size, compress it and calculate a new bytes per pixel value from the file size that you got.
我通过降低质量直到达到我想要的尺寸来实现这一目标。
注意:需要您添加 System.Drawing 引用。
I achieved this by reducing the quality until I reached my desired size.
NB: Requires you to add the System.Drawing reference.
这取决于您愿意更改的内容
很难猜测最终磁盘大小是多少,但如果您知道起点,则可以得到很好的估计。 减小尺寸可能成比例,减少每像素位数也可能成比例。
如果您更改格式、压缩或质量,这实际上只是一种猜测——很大程度上取决于图像内容。 您可以通过在与您认为会看到的内容相匹配的图像语料库上进行尝试来获得良好的范围。
It depends on what you are willing to change
It's hard to guess what the final disk size will be, but if you know a starting point you can get a pretty good estimate. Reducing the size will probably be proportional, reducing the bits per pixel will also likely be proportional.
If you change the format, compression or quality, it's really just a guess -- depends highly on the image content. You could probably get a good range by trying it on a corpus of images that matches what you think you'll be seeing.
如果它是 24 位 BMP,我认为您需要执行以下操作:
如果它是不同的 BMP 类型(例如 8 位 BMP),也在标头部分中会有更多数据指定从 0 到 255 的每个值的实际颜色,因此您在二分搜索之前需要从总文件大小中减去更多。
If it's a 24bit BMP i think you would need to do something like this:
If it is a different BMP type like 8 bit BMP also in the header section there will be more data specifying the actual color of each value from 0 to 255 so you will need to subtract more from the total file size before the binary search.
转换、减少(迭代、在内存中)和 下载(MVC)
Convert, Reduce (Iterative, In Memory) & Download (MVC)