@@ -16,6 +16,43 @@ public enum ImageFormat
1616 unknown
1717 }
1818
19+ #if NET45 || NETSTANDARD2_0
20+ public static ImageFormat GetImageFormat ( byte [ ] bytes )
21+ {
22+ byte [ ] bmp = new byte [ ] { ( byte ) 'B' , ( byte ) 'M' } ; // BMP
23+ byte [ ] gif = new byte [ ] { ( byte ) 'G' , ( byte ) 'I' , ( byte ) 'F' } ; // GIF
24+ byte [ ] png = new byte [ ] { 137 , 80 , 78 , 71 } ; // PNG
25+ byte [ ] tiff = new byte [ ] { 73 , 73 , 42 } ; // TIFF
26+ byte [ ] tiff2 = new byte [ ] { 77 , 77 , 42 } ; // TIFF
27+ byte [ ] jpeg = new byte [ ] { 255 , 216 , 255 , 224 } ; // jpeg
28+ byte [ ] jpeg2 = new byte [ ] { 255 , 216 , 255 , 225 } ; // jpeg canon
29+
30+ if ( bytes . StartsWith ( bmp ) )
31+ return ImageFormat . bmp ;
32+
33+ if ( bytes . StartsWith ( gif ) )
34+ return ImageFormat . gif ;
35+
36+ if ( bytes . StartsWith ( png ) )
37+ return ImageFormat . png ;
38+
39+ if ( bytes . StartsWith ( tiff ) )
40+ return ImageFormat . tiff ;
41+
42+ if ( bytes . StartsWith ( tiff2 ) )
43+ return ImageFormat . tiff ;
44+
45+ if ( bytes . StartsWith ( jpeg ) )
46+ return ImageFormat . jpg ;
47+
48+ if ( bytes . StartsWith ( jpeg2 ) )
49+ return ImageFormat . jpg ;
50+
51+ return ImageFormat . unknown ;
52+ }
53+ #endif
54+
55+ #if NET5_0
1956 public static ImageFormat GetImageFormat ( ReadOnlySpan < byte > bytes )
2057 {
2158 ReadOnlySpan < byte > bmp = stackalloc byte [ ] { ( byte ) 'B' , ( byte ) 'M' } ; // BMP
@@ -49,6 +86,8 @@ public static ImageFormat GetImageFormat(ReadOnlySpan<byte> bytes)
4986
5087 return ImageFormat . unknown ;
5188 }
89+ #endif
90+
5291 }
5392
5493}
0 commit comments