从byte []中找出Image的ContentType

Nic*_*rke 8 c# asp.net asp.net-mvc

有没有办法找出图像的ContentType只来自原始字节?

目前我有一个数据库列,只存储byte [],我用它来显示网页上的图像.

MemoryStream ms = new MemoryStream(imageBytes);
Image image = Image.FromStream(ms);
image.Save(context.HttpContext.Response.OutputStream, <--ContentType-->);
Run Code Online (Sandbox Code Playgroud)

我当然可以将ContentType保存在表中的另一列中,但只是想知道是否有其他方式,例如,也许.Net有办法询问数据以获取类型.

Nic*_*rke 14

文件/魔术签名是要走的路.以下是代码的工作版本.

参考:Stackoverflow - 获取图像尺寸而不读取整个文件

ImageFormat contentType = ImageHelper.GetContentType(this.imageBytes);

MemoryStream ms = new MemoryStream(this.imageBytes);
Image image = Image.FromStream(ms);
image.Save(context.HttpContext.Response.OutputStream, contentType);
Run Code Online (Sandbox Code Playgroud)

然后是助手类:

public static class ImageHelper
{
    public static ImageFormat GetContentType(byte[] imageBytes)
    {
        MemoryStream ms = new MemoryStream(imageBytes);

        using (BinaryReader br = new BinaryReader(ms))
        {
            int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;

            byte[] magicBytes = new byte[maxMagicBytesLength];

            for (int i = 0; i < maxMagicBytesLength; i += 1)
            {
                magicBytes[i] = br.ReadByte();

                foreach (var kvPair in imageFormatDecoders)
                {
                    if (magicBytes.StartsWith(kvPair.Key))
                    {
                        return kvPair.Value;
                    }
                }
            }

            throw new ArgumentException("Could not recognise image format", "binaryReader");
        }
    }

    private static bool StartsWith(this byte[] thisBytes, byte[] thatBytes)
    {
        for (int i = 0; i < thatBytes.Length; i += 1)
        {
            if (thisBytes[i] != thatBytes[i])
            {
                return false;
            }
        }
        return true;
    }

    private static Dictionary<byte[], ImageFormat> imageFormatDecoders = new Dictionary<byte[], ImageFormat>()
    {
        { new byte[]{ 0x42, 0x4D }, ImageFormat.Bmp},
        { new byte[]{ 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, ImageFormat.Gif },
        { new byte[]{ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, ImageFormat.Gif },
        { new byte[]{ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, ImageFormat.Png },
        { new byte[]{ 0xff, 0xd8 }, ImageFormat.Jpeg },
    };
Run Code Online (Sandbox Code Playgroud)


Joo*_*rts 7

这对我ms有用,是记忆流.缺点是它必须加载图像.

Dim fmt As System.Drawing.Imaging.ImageFormat
Dim content As String

Using bmp As New Drawing.Bitmap(ms)
    fmt = bmp.RawFormat
End Using

Select Case fmt.Guid
    Case Drawing.Imaging.ImageFormat.Bmp.Guid
        content = "image/x-ms-bmp"

    Case Drawing.Imaging.ImageFormat.Jpeg.Guid
        content = "image/jpeg"

    Case Drawing.Imaging.ImageFormat.Gif.Guid
        content = "image/gif"

    Case Drawing.Imaging.ImageFormat.Png.Guid
        content = "image/png"

    Case Else
        content = "application/octet-stream"

End Select
Run Code Online (Sandbox Code Playgroud)