Skip to content

Commit ddb2402

Browse files
authored
Fix processing ICO type resources (#157)
1 parent 707f248 commit ddb2402

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

MetadataProcessor.Shared/Tables/nanoResourcesTable.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Collections;
1010
using System.Collections.Generic;
1111
using System.Drawing;
12+
using System.Drawing.Imaging;
1213
using System.IO;
1314
using System.Linq;
1415
using System.Resources;
@@ -173,18 +174,43 @@ private static ResourceKind GetResourceKind(
173174
}
174175

175176
// Check if the data is a bitmap, failure just means it is not.
177+
// First 4 bytes of the resource data is the length
178+
byte[] subset = new byte[resourceData.Length - 4];
179+
Array.Copy(resourceData, 4, subset, 0, subset.Length);
180+
MemoryStream ms = new MemoryStream(subset);
181+
176182
try
177183
{
178-
byte[] subset = new byte[resourceData.Length - 4];
179-
Array.Copy(resourceData, 4,subset, 0,subset.Length);
180-
MemoryStream ms = new MemoryStream(subset);
181-
182-
Bitmap bitmapImage = Image.FromStream(ms) as Bitmap;
183-
return ResourceKind.Bitmap;
184+
// Check for supported bitmap type
185+
Image img = Image.FromStream(ms);
186+
if (Guid.Equals(img.RawFormat,ImageFormat.Bmp))
187+
{
188+
return ResourceKind.Bitmap;
189+
}
190+
else if (Guid.Equals(img.RawFormat, ImageFormat.Jpeg))
191+
{
192+
return ResourceKind.Bitmap;
193+
}
194+
else if (Guid.Equals(img.RawFormat, ImageFormat.Gif))
195+
{
196+
return ResourceKind.Bitmap;
197+
}
198+
else if (Guid.Equals(img.RawFormat,ImageFormat.Icon)
199+
|| Guid.Equals(img.RawFormat,ImageFormat.Emf)
200+
|| Guid.Equals(img.RawFormat,ImageFormat.Exif)
201+
|| Guid.Equals(img.RawFormat,ImageFormat.MemoryBmp)
202+
|| Guid.Equals(img.RawFormat,ImageFormat.Png)
203+
|| Guid.Equals(img.RawFormat,ImageFormat.Tiff)
204+
|| Guid.Equals(img.RawFormat, ImageFormat.Wmf) )
205+
{
206+
// Any future support for other image types to be handled here
207+
// Currently, fall through and pass data as a byte array
208+
}
184209
}
185210
catch
186211
{
187-
// Ignore error if not a bitmap
212+
// Not an error
213+
// The data is not an image, fall through and treat as a binary array
188214
}
189215

190216
// None of the above, assume binary

0 commit comments

Comments
 (0)