I am trying to convert a byte array, obtained from a Hikvision camera stream, into jpg. I use ImageSharp library.
My code is as follows:
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
private static bool ByteArrayToFile(string fileName, byte[] byteArray)
{
try
{
using (var image = Image.Load<Rgba32>(byteArray)) // <-- line 132
{
image.SaveAsJpeg(fileName);
return true;
}
}
catch (Exception ex)
{
ServiceLogger.LogException(ex);
return false;
}
}
Unfortunately it doesn't work, I get the following exception:
2023-05-15 16:44:30.2871 ERROR [1.0.0.1] Exception stack trace: at SixLabors.ImageSharp.Formats.Jpeg.JpegThrowHelper.ThrowNotSupportedException(String errorMessage) at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoderCore.ProcessApplicationHeaderMarker(BufferedReadStream stream, Int32 remaining) at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoderCore.ParseStream(BufferedReadStream stream, SpectralConverter spectralConverter, CancellationToken cancellationToken) at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoderCore.Decode[TPixel](BufferedReadStream stream, CancellationToken cancellationToken) at SixLabors.ImageSharp.Formats.ImageDecoderUtilities.Decode[TPixel](IImageDecoderInternals decoder, Configuration configuration, Stream stream, Func
3 largeImageExceptionFactory, CancellationToken cancellationToken) at SixLabors.ImageSharp.Formats.ImageDecoderUtilities.Decode[TPixel](IImageDecoderInternals decoder, Configuration configuration, Stream stream, CancellationToken cancellationToken) at SixLabors.ImageSharp.Formats.Jpeg.JpegDecoder.Decode[TPixel](JpegDecoderOptions options, Stream stream, CancellationToken cancellationToken) at SixLabors.ImageSharp.Formats.SpecializedImageDecoder
1.Decode[TPixel](DecoderOptions options, Stream stream, CancellationToken cancellationToken) at SixLabors.ImageSharp.Formats.ImageDecoder.<>c__DisplayClass0_01.<Decode>b__0(Stream s) at SixLabors.ImageSharp.Formats.ImageDecoder.<WithSeekableStream>g__PeformActionAndResetPosition|11_0[T](Stream s, Int64 position, <>c__DisplayClass11_0
1&) at SixLabors.ImageSharp.Formats.ImageDecoder.WithSeekableStream[T](DecoderOptions options, Stream stream, Func2 action) at SixLabors.ImageSharp.Formats.ImageDecoder.Decode[TPixel](DecoderOptions options, Stream stream) at SixLabors.ImageSharp.Image.Decode[TPixel](DecoderOptions options, Stream stream) at SixLabors.ImageSharp.Image.<>c__DisplayClass84_0
1.b__0(Stream s) at SixLabors.ImageSharp.Image.WithSeekableStream[T](DecoderOptions options, Stream stream, Func2 action) at SixLabors.ImageSharp.Image.Load[TPixel](DecoderOptions options, Stream stream) at SixLabors.ImageSharp.Image.Load[TPixel](DecoderOptions options, ReadOnlySpan
1 data) at SixLabors.ImageSharp.Image.Load[TPixel](ReadOnlySpan`1 data) at DMS.Helpers.SaveImage.ByteArrayToFile(String fileName, Byte[] byteArray) in D:\source\repos\DMS\Helpers\SaveImage.cs:line 132 2023-05-15 16:44:30.2871 ERROR Unknown App0 Marker - Expected JFIF.
From my limited knowledge of the JPG format, I think the error says that the beginning of my data doesn't have the expected JFIF marker.
So I logged the first 8 bytes of my data. Here is an example:
FF D8 FF E0 00 A5 0A 5B
Isn't this an acceptable JFIF marker for a JPG image? If yes, then why am I getting this error?