11

此代码在尝试调用时失败Image.Save(MemoryStream, ImageFormat)

我得到了例外:

值不能为空。参数名称:编码器“

ImageFormat format = generatedImage.RawFormat as ImageFormat;
image.ImageData = generatedImage.Save(format);

如果我ImageFormat直接传入一个对象,例如ImageFormat.Jpeg.

rawformat转换to的最佳方法是什么ImageFormat(最好没有 switch 语句或大量 if 语句)

谢谢本

4

8 回答 8

17

我使用以下 hepler 方法:

public static string GetMimeType(Image i)
{
    var imgguid = i.RawFormat.Guid;
    foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders()) 
    {
        if (codec.FormatID == imgguid)
            return codec.MimeType;
    }
    return "image/unknown";
}
于 2011-06-13T21:14:28.050 回答
11

抱歉,我发现无法从解析或生成的 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 成员数组来整理它,但我认为您将无法避免切换或循环。

最好的问候, 马蒂亚斯

于 2011-04-26T13:53:19.487 回答
7

你在找这个吗?


System.Drawing.Imaging.ImageFormat fmt = new System.Drawing.Imaging.ImageFormat(generatedImage.RawFormat.Guid);
于 2012-11-11T23:30:25.400 回答
1

还有一种方法可以将Image其保存RawFormat到 some Stream。请参阅http://bytes.com/topic/c-sharp/answers/944402-how-access-raw-image-data-resource-file#post3733044

对我来说,它的工作原理如下:

byte[] GetRawImageData(Image img)
{
    using(MemoryStream ms = new MemoryStream())
    {
        img.Save(ms, img.RawFormat);
        return ms.ToArray();
    }
}
于 2013-03-17T11:35:55.727 回答
1

Cesare Imperiali上面的答案在我的测试中有效。唯一的缺点(如果重要的话)是 Jpeg 的 .ToString() 返回“[ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]”而不是“Jpeg”。

如果这对您很重要,那么您可以使用更少的代码获得精确的静态 ImageFormat 的一种方法是:

public static class ImageFilesHelper
{
    public static List<ImageFormat> ImageFormats =>
        typeof(ImageFormat).GetProperties(BindingFlags.Static | BindingFlags.Public)
          .Select(p => (ImageFormat)p.GetValue(null, null)).ToList();

    public static ImageFormat ImageFormatFromRawFormat(ImageFormat raw) =>
        ImageFormats.FirstOrDefault(f => raw.Equals(f)) ?? ImageFormat.Bmp;

}
// Usage:
var format = ImageFilesHelper.ImageFormatFromRawFormat(Image.FromFile(myFile).RawFormat);
于 2018-03-27T02:53:21.530 回答
0

我尝试了比较 guid 的 Cheburek 方法。但对于某些 png 图像,guid 不匹配。所以我必须编写一个逻辑,它将同时使用 Matthias Wuttke 的解决方案和 Cheburek 的解决方案提到的方法。

首先,我使用 ImageCodecinfo 检查,如果代码找不到图像格式,那么我使用 Matthias Wuttke 的解决方案比较了图像格式。

如果上述两个解决方案均失败,则使用扩展方法获取文件 mime 类型..

如果 mime 类型发生变化,那么文件也会发生变化,我们正在计算下载的文件校验和以匹配服务器上原始文件的校验和。所以对我们来说,获取正确的文件作为输出很重要。

于 2014-03-12T14:31:26.093 回答
0

Cheburek 的答案的 VB.NET 翻译:

Private Function GetMimeType(i As Drawing.Image) As String
    Dim imgguid As Guid = i.RawFormat.Guid
    For Each codec As ImageCodecInfo In ImageCodecInfo.GetImageDecoders()
        If (codec.FormatID = imgguid) Then
            Return codec.MimeType
        End If
    Next
    Return "image/unknown"
End Function
于 2014-01-17T13:01:10.123 回答
0

从 RawFormat 中查找图像扩展名并保存的一个新鲜、现代和通用的答案是这样的:

var formatDescription = ImageCodecInfo.GetImageDecoders().FirstOrDefault(w => w.FormatID == image.RawFormat.Guid)?.FormatDescription;

filePath = Path.ChangeExtension(filePath, formatDescription);

image.Save(filePath);
于 2019-01-30T14:42:58.613 回答