|
@@ -23,6 +23,7 @@ using System;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
using Syncfusion.Pdf.Graphics;
|
|
using Syncfusion.Pdf.Graphics;
|
|
using Syncfusion.Pdf;
|
|
using Syncfusion.Pdf;
|
|
|
|
+using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
|
namespace InABox.WPF
|
|
namespace InABox.WPF
|
|
{
|
|
{
|
|
@@ -613,6 +614,36 @@ namespace InABox.WPF
|
|
return System.Windows.Media.Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
|
|
return System.Windows.Media.Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static bool TryGetImageType(byte[] imageData, [NotNullWhen(true)] out ImageFormat? format)
|
|
|
|
+ {
|
|
|
|
+ foreach (var signatureEntry in SignatureTable)
|
|
|
|
+ foreach (var signature in signatureEntry.Value)
|
|
|
|
+ {
|
|
|
|
+ var isMatch = true;
|
|
|
|
+ for (var i = 0; i < signature.Length; i++)
|
|
|
|
+ {
|
|
|
|
+ var signatureByte = signature[i];
|
|
|
|
+
|
|
|
|
+ // ToString("X") gets the hex representation and pads it to always be length 2
|
|
|
|
+ var imageByte = imageData[i]
|
|
|
|
+ .ToString("X2");
|
|
|
|
+
|
|
|
|
+ if (signatureByte == imageByte)
|
|
|
|
+ continue;
|
|
|
|
+ isMatch = false;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (isMatch)
|
|
|
|
+ {
|
|
|
|
+ format = signatureEntry.Key;
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ format = null;
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Takes a byte array and determines the image file type by
|
|
/// Takes a byte array and determines the image file type by
|
|
/// comparing the first few bytes of the file to a list of known
|
|
/// comparing the first few bytes of the file to a list of known
|
|
@@ -623,27 +654,10 @@ namespace InABox.WPF
|
|
/// <exception cref="ArgumentException">Thrown if the image type can't be determined</exception>
|
|
/// <exception cref="ArgumentException">Thrown if the image type can't be determined</exception>
|
|
public static ImageFormat GetImageType(byte[] imageData)
|
|
public static ImageFormat GetImageType(byte[] imageData)
|
|
{
|
|
{
|
|
- foreach (var signatureEntry in SignatureTable)
|
|
|
|
- foreach (var signature in signatureEntry.Value)
|
|
|
|
|
|
+ if(TryGetImageType(imageData, out var format))
|
|
{
|
|
{
|
|
- var isMatch = true;
|
|
|
|
- for (var i = 0; i < signature.Length; i++)
|
|
|
|
- {
|
|
|
|
- var signatureByte = signature[i];
|
|
|
|
-
|
|
|
|
- // ToString("X") gets the hex representation and pads it to always be length 2
|
|
|
|
- var imageByte = imageData[i]
|
|
|
|
- .ToString("X2");
|
|
|
|
-
|
|
|
|
- if (signatureByte == imageByte)
|
|
|
|
- continue;
|
|
|
|
- isMatch = false;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (isMatch) return signatureEntry.Key;
|
|
|
|
|
|
+ return format;
|
|
}
|
|
}
|
|
-
|
|
|
|
throw new ArgumentException("The byte array did not match any known image file signatures.");
|
|
throw new ArgumentException("The byte array did not match any known image file signatures.");
|
|
}
|
|
}
|
|
|
|
|