System.Drawing.Image 的文件扩展名
我正在编写一个需要将 System.Drawing.Image 保存到文件的方法。在不知道创建 Image
的原始文件的情况下,是否有办法确定它应该具有什么文件扩展名?
我想出的最佳解决方案是使用带有 Image.RawFormat 值的 Switch/Case 语句。
以原始格式保存 Image
还重要吗?从 PNG 生成的图像
与从 JPEG 生成的图像有什么不同吗?或者存储在 Image
对象中的数据是否完全通用?
I'm writing a method that needs to save a System.Drawing.Image
to a file. Without knowing the original file the Image
was created from, is there anyway to determine what file extension it should have?
The best solution I've come up with is to use a Switch/Case statement with the value of Image.RawFormat.
Does it even matter that I save the Image
in it's original format? Is an Image
generated from a PNG any different from say one generated from a JPEG? Or is the data stored in an Image
object completely generic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然 Steve Danner 是正确的,因为一旦将 JPG 创建的图像加载到内存中,从 JPG 创建的图像看起来就会与从 PNG 创建的图像不同,因为它是未压缩的数据流。
这意味着您可以将其保存为您想要的任何文件格式。
但是,如果您加载 JPG 图像,然后将其另存为另一个 JPG,则由于压缩算法,您将丢弃更多信息。如果您重复执行此操作,您最终将丢失图像。
如果可以的话我建议总是保存为PNG。
While Steve Danner is correct in that an image created from a JPG will look different to an image created from a PNG once it's loaded into memory it's an uncompressed data stream.
This means that you can save it out to any file format you want.
However, if you load a JPG image and then save it as another JPG you are throwing away more information due to the compression algorithm. If you do this repeatedly you will eventually lose the image.
If you can I'd recommend always saving as PNG.
Image.RawFormat 有虱子,远离它。我见过几篇报告,说它没有任何明显的理由没有法律价值。目前尚未确诊。
你说得对,保存成什么格式并不重要。加载文件后,具有相同像素格式的任何位图(非矢量)的内部格式都是相同的。通常避免重新压缩 jpeg 文件,它们往往会变得更大并获得更多工件。史蒂夫提到多帧文件,它们需要以不同的方式保存。
Image.RawFormat has cooties, stay away from it. I've seen several reports of it having no legal value for no apparent reason. Undiagnosed as yet.
You are quite right, it doesn't matter what format you save it to. After you loaded the file, the internal format is the same for any bitmap (not vector) with the same pixel format. Generally avoid recompressing jpeg files, they tend to get bigger and acquire more artifacts. Steve mentions multi-frame files, they need to be saved a different way.
是的,这确实很重要,因为不同的文件格式支持不同的功能,例如压缩、多帧等。
我一直使用像您一样的 switch 语句,也许会融入扩展方法或其他东西中。
Yes, it definitely matters because different fileformats support different features such as compression, multiple frames, etc.
I've always used a switch statement like you have, perhaps baked into an extension method or something.
要明确回答您的问题“我以原始格式保存图像是否重要?”:是的,确实如此,但是以一种消极的方式。
加载图像时,它会在内部解压缩为位图(或作为 ChrisF 称之为未压缩数据流)。因此,如果原始图像使用有损压缩(例如 jpeg),以相同格式保存将再次导致信息丢失(即更多伪影、更少细节、更低质量)。特别是如果您重复读取-修改-保存的操作,这是应该避免的。
(请注意,如果您不修改图片,这也是要避免的。仅重复解压缩 - 压缩循环会降低图像质量)。
因此,如果磁盘空间在这里不是问题(通常在硬盘时代还没有足够大的空间来容纳高清视频),请始终以无损压缩格式或未压缩格式存储任何中间图片。您可以考虑以压缩格式保存最终输出,具体取决于您的用途。 (如果您想在网络上呈现这些最终图片,jpeg 或 png 是不错的选择)。
To answer your question 'Does it even matter that I save the Image in it's original format?' explicitly: Yes, it does, but in a negative way.
When you load the image, it is uncompressed internally to a bitmap (or as ChrisF calls it, an uncompressed data stream). So if the original image used a lossy compression (for example jpeg), saving it in the same format will again result in loss of information (i.e. more artifacts, less detail, lower quality). Especially if you have repeated actions of read - modify - save, this is something to avoid.
(Note that it is also something to avoid if you are not modifying the picture. Just the repeated decompress - compress cycles will degrade the image quality).
So if disk space is not an issue here (and it usually isn't in the age of hard disks that are big enough for HD video), always store any intermediate pictures in lossless compression formats, or uncompressed. You may consider saving the finall output in a compressed format, depending on what you use it for. (If you want to present those final pictures on the web, jpeg or png would be good choices).