首页 > 代码库 > 根据文件头判断图片格式

根据文件头判断图片格式

测试代码

byte[] imgb;            using (FileStream fs = new FileStream(@"D:\favicon.ico", FileMode.Open))            {                imgb = new byte[fs.Length];                fs.Read(imgb, 0, (int)fs.Length);            }            string imgtype = imgb.GetImageType();

扩展方法实现

 public static string GetImageType(this byte[] imgb)        {            //JPEG/JPG - 文件头标识 (2 bytes): $ff, $d8 (SOI) (JPEG 文件标识) - 文件结束标识 (2 bytes): $ff, $d9 (EOI)            if (imgb[0]== 0xff && imgb[1] == 0xd8 && imgb[imgb.Length-2] == 0xff && imgb[imgb.Length - 1] == 0xd9 )            {                return "JPEG/JPG";            }            //TGA - 未压缩的前5字节   00 00 02 00 00 - RLE压缩的前5字节   00 00 10 00 00            if ((imgb[0] == 0 && imgb[1] == 0 && imgb[2] ==  2 && imgb[3] == 0 && imgb[4] == 0) ||                (imgb[0] == 0 && imgb[1] == 0 && imgb[2] == 10 && imgb[3] == 0 && imgb[4] == 0))            {                return "TGA";            }            //PNG - 文件头标识 (8 bytes)   89 50 4E 47 0D 0A 1A 0A            if (imgb[0] == 0x89 && imgb[1] == 0x50 && imgb[2] == 0x4e && imgb[3] == 0x47 &&                imgb[4] == 0x0d && imgb[5] == 0x0a && imgb[6] == 0x1a && imgb[7] == 0x0a)            {                return "PNG";            }            //GIF - 文件头标识 (6 bytes)   47 49 46 38 39(37) 61                         G  I  F  8  9 (7)  a            if (imgb[0] == 0x47 && imgb[1] == 0x49 && imgb[2] == 0x46 && imgb[3] == 0x38 &&                (imgb[4] ==0x39 || imgb[4] == 0x37) && imgb[5] == 0x61)            {                return "GIF";            }            //BMP - 文件头标识 (2 bytes)   42 4D                         B  M            if (imgb[0] == 0x42 && imgb[1] == 0x4d )            {                return "BMP";            }            //PCX - 文件头标识 (1 bytes)   0A            if (imgb[0] == 0x0a)            {                return "PCX";            }            ///TIFF - 文件头标识 (2 bytes)  4D 4D 或 49 49            if ((imgb[0] == 0x4d && imgb[1] == 0x4d)|| (imgb[0] == 0x49 && imgb[1] == 0x49))            {                return "TIFF";            }            //ICO - 文件头标识 (8 bytes)   00 00 01 00 01 00 30 30             if (imgb[0] == 0x00 && imgb[1] == 0x00 && imgb[2] == 0x01 && imgb[3] == 0x00 &&                imgb[4] == 0x01 && imgb[5] == 0x00 && imgb[6] == 0x30 && imgb[7] == 0x30)            {                return "ICO";            }            //CUR - 文件头标识 (8 bytes)   00 00 02 00 01 00 20 20            if (imgb[0] == 0x00 && imgb[1] == 0x00 && imgb[2] == 0x02 && imgb[3] == 0x00 &&               imgb[4] == 0x01 && imgb[5] == 0x00 && imgb[6] == 0x20 && imgb[7] == 0x20)            {                return "CUR";            }            ///IFF - 文件头标识 (4 bytes)   46 4F 52 4D                        F  O  R  M            if (imgb[0] == 0x46 && imgb[1] == 0x4f && imgb[2] == 0x52 && imgb[3] == 0x4d)            {                return "IFF";            }            //ANI - 文件头标识 (4 bytes)   52 49 46 46                         R  I  F  F            if (imgb[0] == 0x52 && imgb[1] == 0x49 && imgb[2] == 0x46 && imgb[3] == 0x46)            {                return "IFF";            }            return string.Empty;        }

 

根据文件头判断图片格式