@@ -58,12 +58,10 @@ public enum FileType { Unrecognized, Bitmap, Gif, Png, Jpeg, Emf, Xml }
58
58
/// <returns>Returns true if the detection was successful.</returns>
59
59
public static bool TryDetectFileType ( Stream stream , out FileType type )
60
60
{
61
- using ( SequentialBinaryReader reader = new SequentialBinaryReader ( stream , leaveOpen : true ) )
62
- {
63
- type = DetectFileType ( reader ) ;
64
- stream . Seek ( 0L , SeekOrigin . Begin ) ;
65
- return type != FileType . Unrecognized ;
66
- }
61
+ using var reader = new SequentialBinaryReader ( stream , leaveOpen : true ) ;
62
+ type = DetectFileType ( reader ) ;
63
+ stream . Seek ( 0L , SeekOrigin . Begin ) ;
64
+ return type != FileType . Unrecognized ;
67
65
}
68
66
69
67
/// <summary>
@@ -74,21 +72,19 @@ public static bool TryDetectFileType(Stream stream, out FileType type)
74
72
/// <exception cref="ArgumentException">The image was of an unrecognised format.</exception>
75
73
public static Size GetDimensions ( Stream stream )
76
74
{
77
- using ( SequentialBinaryReader reader = new SequentialBinaryReader ( stream , leaveOpen : true ) )
75
+ using var reader = new SequentialBinaryReader ( stream , leaveOpen : true ) ;
76
+ FileType type = DetectFileType ( reader ) ;
77
+ stream . Seek ( 0L , SeekOrigin . Begin ) ;
78
+ return type switch
78
79
{
79
- FileType type = DetectFileType ( reader ) ;
80
- stream . Seek ( 0L , SeekOrigin . Begin ) ;
81
- switch ( type )
82
- {
83
- case FileType . Bitmap : return DecodeBitmap ( reader ) ;
84
- case FileType . Gif : return DecodeGif ( reader ) ;
85
- case FileType . Jpeg : return DecodeJfif ( reader ) ;
86
- case FileType . Png : return DecodePng ( reader ) ;
87
- case FileType . Emf : return DecodeEmf ( reader ) ;
88
- case FileType . Xml : return DecodeXml ( stream ) ;
89
- default : return Size . Empty ;
90
- }
91
- }
80
+ FileType . Bitmap => DecodeBitmap ( reader ) ,
81
+ FileType . Gif => DecodeGif ( reader ) ,
82
+ FileType . Jpeg => DecodeJfif ( reader ) ,
83
+ FileType . Png => DecodePng ( reader ) ,
84
+ FileType . Emf => DecodeEmf ( reader ) ,
85
+ FileType . Xml => DecodeXml ( stream ) ,
86
+ _ => Size . Empty ,
87
+ } ;
92
88
}
93
89
94
90
/// <summary>
@@ -123,36 +119,19 @@ public static Size KeepAspectRatio(Size actualSize, Size preferredSize)
123
119
private static FileType DetectFileType ( SequentialBinaryReader reader )
124
120
{
125
121
byte [ ] magicBytes = new byte [ MaxMagicBytesLength ] ;
126
- for ( int i = 0 ; i < MaxMagicBytesLength ; i += 1 )
127
- {
128
- magicBytes [ i ] = reader . ReadByte ( ) ;
129
- foreach ( var kvPair in imageFormatDecoders )
130
- {
131
- if ( StartsWith ( magicBytes , kvPair . Key ) )
132
- {
133
- return kvPair . Value ;
134
- }
135
- }
136
- }
137
-
138
- return FileType . Unrecognized ;
139
- }
122
+ reader . Read ( magicBytes , 0 , MaxMagicBytesLength ) ;
140
123
141
- /// <summary>
142
- /// Determines whether the beginning of this byte array instance matches the specified byte array.
143
- /// </summary>
144
- /// <returns>Returns true if the first array starts with the bytes of the second array.</returns>
145
- private static bool StartsWith ( byte [ ] thisBytes , byte [ ] thatBytes )
146
- {
147
- for ( int i = 0 ; i < thatBytes . Length ; i += 1 )
124
+ var headerSpan = magicBytes . AsSpan ( ) ;
125
+ foreach ( var kvPair in imageFormatDecoders )
148
126
{
149
- if ( thisBytes [ i ] != thatBytes [ i ] )
127
+ // Determines whether the beginning of this array matches s known header.
128
+ if ( headerSpan . StartsWith ( kvPair . Key ) )
150
129
{
151
- return false ;
130
+ return kvPair . Value ;
152
131
}
153
132
}
154
133
155
- return true ;
134
+ return FileType . Unrecognized ;
156
135
}
157
136
158
137
private static Size DecodeBitmap ( SequentialBinaryReader reader )
@@ -220,16 +199,16 @@ private static Size DecodeJfif(SequentialBinaryReader reader)
220
199
return Size . Empty ;
221
200
222
201
// next 2-bytes are <segment-size>: [high-byte] [low-byte]
223
- var segmentLength = ( int ) reader . ReadUInt16 ( ) ;
202
+ int segmentLength = reader . ReadUInt16 ( ) ;
224
203
225
204
// segment length includes size bytes, so subtract two
226
205
segmentLength -= 2 ;
227
206
228
207
if ( segmentType == 0xC0 || segmentType == 0xC2 )
229
208
{
230
209
reader . ReadByte ( ) ; // bits/sample, usually 8
231
- int height = ( int ) reader . ReadUInt16 ( ) ;
232
- int width = ( int ) reader . ReadUInt16 ( ) ;
210
+ int height = reader . ReadUInt16 ( ) ;
211
+ int width = reader . ReadUInt16 ( ) ;
233
212
return new Size ( width , height ) ;
234
213
}
235
214
else
0 commit comments