C# 中的图像处理
我正在将 JPG 图像从硬盘加载到 byte[] 中。有没有办法调整图像大小(降低分辨率)而不需要将其放入 Bitmap 对象中?
谢谢
I am loading a JPG image from hard disk into a byte[]. Is there a way to resize the image (reduce resolution) without the need to put it in a Bitmap object?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我正在做的事情。
不,我认为如果不先在内存中处理图像(即在某种位图中),就无法调整图像的大小。
良好的质量调整大小涉及使用插值/外推算法;它不能只是“挑选出每n个像素”,除非你可以与最近的邻居解决。
这里有一些解释:http://www.cambridgeincolour.com/tutorials/image-interpolation。嗯
Here's what I'm doing.
And no, I don't think you can resize an image without first processing it in-memory (i.e. in a Bitmap of some kind).
Decent quality resizing involves using an interpolation/extrapolation algorithm; it can't just be "pick out every n pixels", unless you can settle with nearest neighbor.
Here's some explanation: http://www.cambridgeincolour.com/tutorials/image-interpolation.htm
方法总是有的,但是否更好... JPG 是一种压缩图像格式,这意味着要对其进行任何图像处理,您需要一些东西来解释该数据。 bimap 对象将为您执行此操作,但如果您想走另一条路,您需要了解 jpeg 规范、创建某种解析器等。可能有一些快捷方式可以使用,而无需对原始 jpg 进行完整解释,但我认为这是一个坏主意。
哦,不要忘记 JPG 显然有不同的文件格式(JFIF 和 EXIF),您将需要理解它们......
在避免专门为您想要的东西设计的对象之前,我会非常努力地考虑做。
There are always ways but whether they are better... a JPG is a compressed image format which means that to do any image manipulation on it you need something to interpret that data. The bimap object will do this for you but if you want to go another route you'll need to look into understanding the jpeg spec, creating some kind of parser, etc. It might be that there are shortcuts that can be used without needing to do full intepretation of the original jpg but I think it would be a bad idea.
Oh, and not to forget there are different file formats for JPG apparently (JFIF and EXIF) that you will ened to understand...
I'd think very hard before avoiding objects that are specifically designed for the sort of thing you are trying to do.
.jpeg 文件只是一个没有 JPEG 解码器的字节包。 Bitmap 类中内置了一个,它可以很好地解码 .jpeg 文件。结果是一个 Bitmap 对象,你无法绕过它。
它支持通过 Graphics 类以及 Bitmap(Image, Size) 构造函数调整大小。但是,确实,缩小 .jpeg 图像通常会产生更大的文件。这是 Graphics.Interpolation 模式不可避免的副作用。它试图通过过滤器运行像素来改善缩小图像的外观。双三次过滤器在这方面做得非常出色。
人眼看起来很棒,但 JPEG 编码器看起来不太好。该滤镜会产生插值像素颜色,旨在避免尺寸缩小时图像细节完全消失。然而,这些混合像素值使得编码器更难压缩图像,从而产生更大的文件。
您可以修改 Graphics.InterpolationMode 并选择质量较低的过滤器。产生较差的图像,但更容易压缩。但我怀疑你会欣赏这个结果。
A .jpeg file is just a bag o' bytes without a JPEG decoder. There's one built into the Bitmap class, it does a fine job decoding .jpeg files. The result is a Bitmap object, you can't get around that.
And it supports resizing through the Graphics class as well as the Bitmap(Image, Size) constructor. But yes, making a .jpeg image smaller often produces a file that's larger. That's an unavoidable side-effect of Graphics.Interpolation mode. It tries to improve the appearance of the reduced image by running the pixels through a filter. The Bicubic filter does an excellent job of it.
Looks great to the human eye, doesn't look so great to the JPEG encoder. The filter produces interpolated pixel colors, designed to avoid making image details disappear completely when the size is reduced. These blended pixel values however make it harder on the encoder to compress the image, thus producing a larger file.
You can tinker with Graphics.InterpolationMode and select a lower quality filter. Produces a poorer image, but easier to compress. I doubt you'll appreciate the result though.