diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
index 5ba1946349..4a73e8a4be 100644
--- a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
+++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Linq;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
@@ -239,6 +240,12 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
{
if (this.values == null)
{
+ if (!includeExifIdCode && this.StartsWithExifIdCode(this.data))
+ {
+ // skip the first 6 bytes (the Exif Code)
+ return this.data.Skip(6).ToArray();
+ }
+
return this.data;
}
@@ -251,6 +258,32 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
return writer.GetData(includeExifIdCode);
}
+ ///
+ /// Checks if a byte array start with the Exif Code: ASCII "Exif" followed by two zeros.
+ ///
+ /// The byte array to check for the Exif Code.
+ /// True, if the byte array starts with the Exif Code
+ private bool StartsWithExifIdCode(byte[] exifBytes)
+ {
+ if (exifBytes.Length < 6)
+ {
+ return false;
+ }
+
+ // The EXIF ID code: ASCII "Exif" followed by two zeros.
+ byte[] exifCode = { 69, 120, 105, 102, 0, 0 };
+ int exifLength = exifCode.Length;
+ for (int i = 0; i < exifCode.Length; i++)
+ {
+ if (exifBytes[i] != exifCode[i])
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
///
/// Synchronizes the profiles with the specified meta data.
///
diff --git a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs
index 6d3eb56317..98113f3921 100644
--- a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs
+++ b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs
@@ -351,6 +351,32 @@ namespace SixLabors.ImageSharp.Tests
}
}
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void TestProfileToByteArrayWorks(bool includeExifIdCode)
+ {
+ // arrange
+ byte[] exifBytesWithExifCode = new byte[] { 69, 120, 105, 102, 0, 0, 73, 73, 42, 0};
+ byte[] exifBytesWithoutExifCode = new byte[] { 73, 73, 42, 0 };
+ var profile = new ExifProfile(exifBytesWithExifCode);
+
+ // act
+ byte[] actual = profile.ToByteArray(includeExifIdCode);
+
+ // assert
+ Assert.NotNull(actual);
+ Assert.NotEmpty(actual);
+ if (includeExifIdCode)
+ {
+ Assert.Equal(exifBytesWithExifCode, actual);
+ }
+ else
+ {
+ Assert.Equal(exifBytesWithoutExifCode, actual);
+ }
+ }
+
private static ExifProfile CreateExifProfile()
{
var profile = new ExifProfile();