2

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, Func3 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.SpecializedImageDecoder1.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_01&) 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_01.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, ReadOnlySpan1 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?

Giorgos Betsos
  • 71,379
  • 9
  • 63
  • 98

1 Answers1

0

Analyzing the first 8 bytes provided it looks like there something definitely wrong with the stream.

FF D8 - SOI marker
FF E0 - APP0 marker
00 A5 - Length in bytes (165)
0A 5B - Unknown

Those last two bytes are an issue, though that provided length is very long! For jpeg you would expect the following.

4A 46 49 46 - JFIF Identifier

Some badly encoded images don't provide that marker, instead only providing the APP0 extension identifier JFXX (4A 46 58 58). Support for this scenario has been recently added to main.

I'm really curious to see what else is happening with the stream to see if it can be worked around. Can you please post a discussion here and we can have a closer look.

James South
  • 10,147
  • 4
  • 59
  • 115