抱歉,我发现无法从解析或生成的 Image 对象中直接提取“正确”的 ImageFormat。
这是我的代码,您可以通过存储静态 ImageFormat 成员而不是 mimetype 来采用它。
if (image.RawFormat.Equals(ImageFormat.Jpeg))
binary.MetaInfo.Mimetype = "image/jpeg";
else if (image.RawFormat.Equals(ImageFormat.Bmp))
binary.MetaInfo.Mimetype = "image/bmp";
else if (image.RawFormat.Equals(ImageFormat.Emf))
binary.MetaInfo.Mimetype = "image/emf";
else if (image.RawFormat.Equals(ImageFormat.Exif))
binary.MetaInfo.Mimetype = "image/exif";
else if (image.RawFormat.Equals(ImageFormat.Gif))
binary.MetaInfo.Mimetype = "image/gif";
else if (image.RawFormat.Equals(ImageFormat.Icon))
binary.MetaInfo.Mimetype = "image/icon";
else if (image.RawFormat.Equals(ImageFormat.Png))
binary.MetaInfo.Mimetype = "image/png";
else if (image.RawFormat.Equals(ImageFormat.Tiff))
binary.MetaInfo.Mimetype = "image/tiff";
else if (image.RawFormat.Equals(ImageFormat.Wmf))
binary.MetaInfo.Mimetype = "image/wmf";
您可以通过使用静态 ImageFormat 成员数组来整理它,但我认为您将无法避免切换或循环。
最好的问候, 马蒂亚斯