从 System.Drawing.Image.RawFormat 获取 ImageFormat
此代码在尝试调用 Image.Save(MemoryStream, ImageFormat)
时失败。
我得到例外:
值不能为空。参数名称:编码器”
ImageFormat format = generatedImage.RawFormat as ImageFormat;
image.ImageData = generatedImage.Save(format);
如果我直接传入 ImageFormat
对象,例如 ImageFormat.Jpeg
,它就可以工作。
转换 的最佳方法是什么>rawformat
到 ImageFormat
(最好没有 switch 语句或大量 if 语句)
谢谢 本
This code fails when trying to call Image.Save(MemoryStream, ImageFormat)
.
I get the exception:
a Value cannot be null.Parameter name: encoder"
ImageFormat format = generatedImage.RawFormat as ImageFormat;
image.ImageData = generatedImage.Save(format);
It works if I pass in an ImageFormat
object directly e.g. ImageFormat.Jpeg
.
What is the best way of converting the rawformat
to ImageFormat
(ideally without a switch statement or lots of if statements)
Thanks
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我使用以下 hepler 方法来实现相同的目的:
I use following hepler method for the same:
抱歉,我发现无法直接从解析或生成的 Image 对象中提取“正确的” ImageFormat 。
这是我的代码,您可以通过存储静态 ImageFormat 成员而不是 mimetype 来采用它。
您可以通过使用静态 ImageFormat 成员数组来整理它,但我认为您将无法避免切换或循环。
最好的问候,马蒂亚斯
I'm sorry, I found no possibility to directly extract a "proper" ImageFormat from the parsed or generated Image object.
This is my code, you can adopt it by storing the static ImageFormat member instead of the mimetype.
You could tidy it up by using an array of static ImageFormat members, but I think you won't be able to avoid a switch or a loop.
Best regards, Matthias
你在找这个吗?
are you looking for this?
还有一种方法可以将
Image
以RawFormat
格式保存到某个Stream
中。请参阅 http:// bytes.com/topic/c-sharp/answers/944402-how-access-raw-image-data-resource-file#post3733044对于我来说,其工作原理如下:
There's also a way to save an
Image
in itsRawFormat
to someStream
. See http://bytes.com/topic/c-sharp/answers/944402-how-access-raw-image-data-resource-file#post3733044For me that works like the following:
Cesare Imperiali 的上述答案在我的测试中有效。唯一的缺点(如果重要的话)是 Jpeg 的 .ToString() 返回“[ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]”而不是“Jpeg”。
如果这对您很重要,那么您可以使用更少的代码获得精确的静态 ImageFormat 的一种方法是:
Cesare Imperiali's answer above worked in my tests. The only downside (if it matters) is that the .ToString() for a Jpeg returns "[ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]" instead of "Jpeg".
If that matters to you, one way you can get the precise static ImageFormat's with less code is this way:
Cheburek 答案的 VB.NET 翻译:
The VB.NET translation of Cheburek's answer:
我尝试了比较指南的 Cheburek 方法。但对于某些 png 图像,指南不匹配。所以我必须编写一个逻辑,它将使用 Matthias Wuttke 的解决方案和 Cheburek 的解决方案提到的方法。
首先,我检查了 ImageCodecinfo,如果代码找不到图像格式,那么我使用 Matthias Wuttke 的解决方案比较图像格式。
如果上述两种解决方案都失败,则使用扩展方法来获取文件 mime 类型。
如果 mime 类型发生变化,则文件也会发生变化,我们正在计算下载的文件校验和,以与服务器上原始文件的校验和相匹配..所以对我们来说,获得正确的文件作为输出非常重要。
I tried Cheburek method of comparing the guid. but for some of the png images the guids were not matching. so i had to write a logic which will use both the methods mentioned by Matthias Wuttke's solution and Cheburek's solution.
first i was checking with the ImageCodecinfo and if code does not find the imageformat then i compared the imageformat using Matthias Wuttke's solution.
if both the above mentioned solution failed then used the extension method to get the file mime type..
if the mime type changes then the file also changes, we were calculating the downloaded files checksum to match with the checksum of the original file on the server .. so for us it was importent to get proper file as output.
从 RawFormat 中查找图像扩展名并保存的新鲜、现代且通用的答案如下:
A fresh, modern and general answer for finding an image extension from RawFormat and saving is like this: