'Ubuntu-OS Compatible Tiff image format to Jpeg image format Convertion (.NET 5)

I want to Convert Tiff file/filestream into Jpeg file/filestream. I am curretly developing a Blazor Server app with .NET 5 in Windows. But my Server is in Ubuntu OS. I am trying to display tiff image in webpage. In order to do that I am trying to convert my tiff images into jpeg which is supported by webpages. But I did not manage to do that.

Below I have described the processes that I tried so far:

For convertion I was using the following code.

public byte[] convertToJpeg(Byte[] tiffBytes)
{
    Byte[] jpegBytes;

    using (MemoryStream inStream = new MemoryStream(tiffBytes))
    using (MemoryStream outStream = new MemoryStream())
    {
        System.Drawing.Bitmap.FromStream(inStream).Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        jpegBytes = outStream.ToArray();
    }
    return jpegBytes;
}

Unfortunately it runs perfectly fine in windows(my local PC's OS) but throws exception in Ubuntu(my server's OS).

Message : The type initializer for 'Gdip' threw an exception. Data : System.Collections.ListDictionaryInternal InnerException : System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.

After reading through here. I found the reason of the exception. But my requrement prevents me from accessing the server's PC and installing libgdiplus. Also the server's PC will not be available in the open internet, will not access the internet and works only inside the internal network. Also it does not seem like a good soluton.

So, I tried following Recommanded Action . Here is my code rewritten with ImageSharp.

public async Task<byte[]> ConvertToJpegAsync(byte[] inputByteArray)
{
    try
    {
        using (var inputStream = new MemoryStream(inputByteArray))
        using (var outputStream = new MemoryStream())
        using (var image = await Image.LoadAsync(inputStream))
        {
            await image.SaveAsJpegAsync(outputStream);

            // Convert stream to byte[]
            return outputStream.ToArray();
        }
    }
    catch (Exception ex)
    {
        // Log Here
    }


    // Empty byte array
    return Enumerable.Empty<byte>().ToArray();
}

But I get the following error trying to open a tiff file:

ex : SixLabors.ImageSharp.UnknownImageFormatException: Image cannot be loaded. Available decoders:

  • TGA : TgaDecoder
  • BMP : BmpDecoder
  • PNG : PngDecoder
  • GIF : GifDecoder
  • JPEG : JpegDecoder

In the end it seems nither ImageSharp nor SkiaSharp support Tiff. After further digging I found that ImageSharp does support it in it's alpha build. But I would reather not use alpa-build in my software.

So, in short a good implementation to convert Tiff to JPEG(or any web supported format such as bitmap, png) which works on the .NET 5 is what I am looking for.

  • Thank you


Solution 1:[1]

You code looks to be correct.

ImageSharp supports TIFF in its mainstream release. Which version did you reference, please try the latest from NuGet.org (currently 2.1.1).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ynse01