图片文件头以及解码

1.JPEG
- 文件头标识 (2 bytes): 0xff, 0xd8 (SOI) (JPEG 文件标识)
- 文件结束标识 (2 bytes): 0xff, 0xd9 (EOI)

2.TGA
- 未压缩的前5字节    00 00 02 00 00
- RLE压缩的前5字节   00 00 10 00 00

3.PNG
- 文件头标识 (8 bytes)   89 50 4E 47 0D 0A 1A 0A

4.GIF
- 文件头标识 (6 bytes)   47 49 46 38 39(37) 61
                                        G    I   F   8    9 (7)   a

5.BMP
- 文件头标识 (2 bytes)   42 4D
                                         B  M

6.PCX
- 文件头标识 (1 bytes)   0A

7.TIFF
- 文件头标识 (2 bytes)   4D 4D 或 49 49

8.ICO
- 文件头标识 (8 bytes)   00 00 01 00 01 00 20 20

9.CUR
- 文件头标识 (8 bytes)   00 00 02 00 01 00 20 20

10.IFF
- 文件头标识 (4 bytes)   46 4F 52 4D
                                         F   O  R  M

11.ANI
- 文件头标识 (4 bytes)   52 49 46 46

 

以下是C#解码示例,只示范了几种常见的

using System.IO;
using System.Drawing;  //引用添加程序集 System.Drawing



// 调用方法:  System.Windows.Controls.Image.Source = GetSourceData(imagesource);
// 调用方法:  BitmapSource bmpsou = GetSourceData(imagesource);


public static BitmapSource GetSourceData(MemoryStream Source)
        {          
            BitmapSource Mapsou = null;
            byte[] buff = Source.ToArray();
            try
            {
                switch (buff[0])
                {
                    case 0xff:   //Jpeg格式
                        {
                            if (buff[1] == 0xd8)
                            {
                                Mapsou = new JpegBitmapDecoder(Source, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default).Frames[0];
                            }
                        } break;

                    case 0x89:  //Png格式
                        {
                            if (buff[1] == 0x50 && buff[2] == 0x4E && buff[7] == 0x0A)
                            {
                                Mapsou = new PngBitmapDecoder(Source, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default).Frames[0];
                            }
                        } break;
                    case 0x42:  //Bmp格式
                        {
                            if (buff[1] == 0x4d)
                            {
                                Mapsou = new BmpBitmapDecoder(Source, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default).Frames[0];
                            } break;
                        }
                    case 0x47: //Gif格式 实现播放效果移步至 https://www.cnblogs.com/liunlls/p/wpf-gif.html
                        {
                            if (buff[1] == 0x49 && buff[5] == 0x61)
                            {
                                Mapsou = new GifBitmapDecoder(Source, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default).Frames[0];
                            }
                        }
                        break;
                    case 0x00:  //Icon格式
                        {
                            if (buff[1] == 0x00 && buff[2] == 0x01)
                            {
                                Mapsou = new IconBitmapDecoder(Source, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default).Frames[0];
                            }
                        }
                        break;

                    default: throw new Exception("图片格式错误或不支持此类型图片数据!");
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
            }

            return Mapsou;
        }

部分资料来源(文件头):https://blog.csdn.net/include1224/article/details/5195470?utm_source=copy

Wpf窗口播放Gif:https://www.cnblogs.com/liunlls/p/wpf-gif.html

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值