DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • .NET Aspire: Building Cloud-Native Applications
  • A Developer-Centric Cloud Architecture Framework (DCAF) for Enterprise Platforms
  • Simplify Your Compliance With Google Cloud Assured Workloads
  • Compliance Automated Standard Solution (COMPASS), Part 7: Compliance-to-Policy for IT Operation Policies Using Auditree

Trending

  • AWS Kiro: The Agentic IDE That Makes Specs the Unit of Work
  • How to Build and Optimize AI Models for Real-World Applications
  • We Went Multi-Cloud and Almost Drowned: Lessons From Running Across AWS, GCP, and Azure
  • The Death of "Text-Only" ChatOps: Why Google's A2UI Matters for DevOps and SRE
  1. DZone
  2. Coding
  3. Frameworks
  4. Converting Multi-Frame TIFF to GIF in Cross-Platform .NET Environments

Converting Multi-Frame TIFF to GIF in Cross-Platform .NET Environments

Converting multi-frame TIFF images to GIF format is a unique challenge, especially in a cross-platform .NET context. Here, explore a solution to this challenge.

By 
Somasundaram Kumarasamy user avatar
Somasundaram Kumarasamy
·
Nov. 28, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
3.2K Views

Join the DZone community and get the full member experience.

Join For Free

Converting multi-frame TIFF images to GIF format has a unique challenge, especially in a cross-platform .NET context. The issue is the Windows-specific nature of System.Drawing.Common, which limits the deployment of solutions in cross-platform cloud environments. This article presents a solution to this challenge, targeted for cloud platforms and capable of handling TIFF images of varying sizes.

Background

During our project to migrate a legacy .NET system to cross-platform microservices using .NET, we faced a significant challenge in converting TIFF images to GIF or BMP formats. This issue stemmed from the fact that System.Drawing.Common, a key library in our legacy system, is supported only on the Windows platform. When you try to use   System.Drawing.Common you will encounter the following error:

System.Drawing.Common is not supported on this platform.

TIFF (Tagged Image File Format) is popular in digital imaging but when it comes to rendering in HTML, you will have to convert it into some primitive formats supported by the browsers. TIFF's support for multiple frames of complex images, while GIF's wide compatibility and simplicity make it ideal for web use. However, converting between these formats can be tricky, particularly when dealing with multi-frame TIFFs of varying dimensions.

Microsoft recommends several alternative libraries for handling image processing in a cross-platform environment. These include:

  1. SkiaSharp: A powerful 2D graphics library that provides a broad range of image manipulation features
  2. ImageSharp: Offers a tiered license structure and supports a variety of image formats
  3. Aspose.Drawing: A commercial license library, known for extensive graphics functionality
  4. Microsoft.Maui.Graphics: Part of the MAUI framework, it provides cross-platform graphics rendering

Despite the capabilities of these libraries, we encountered limitations with TIFF image support. Notably, only ImageSharp offers limited support for TIFF images, with a key restriction being the expectation that each frame of a multi-frame TIFF image should be of the same size.

Solution Approach

The solution uses TiffLibrary and SixLabors.ImageSharp, leveraging their image processing capabilities while ensuring cross-platform compatibility. Unlike System.Drawing.Common, these libraries are not restricted to Windows, making them suitable for diverse cloud environments. The implementation focuses on reading each frame of the TIFF image, resizing frames as needed to maintain consistency, and then converting it into a GIF file for each frame.

Implementation

Here's a high-level overview of the implementation:

  1. In this implementation, the TiffFileReader is used to read through the TIFF file's directories (IFDs). 
  2. For each IFD, an image decoder is created and used to decode the image.
  3. Each frame is saved in GIF format using the ImageSharp library.
  4.  These are then added to a list of images.  
C#
 
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using TiffLibrary;
using Image = SixLabors.ImageSharp.Image;

List<Image> images = new List<Image>();
byte[] var = File.ReadAllBytes("testcolor.tif");
using (var msTif = new MemoryStream(var))
using (var ms = new MemoryStream())
{
    using (TiffFileReader reader = await TiffFileReader.OpenAsync(msTif, true))
    {
        TiffStreamOffset ifdOffSet = reader.FirstImageFileDirectoryOffset;

        while (!ifdOffSet.IsZero)
        {
            TiffImageFileDirectory ifd = reader.ReadImageFileDirectory(ifdOffSet);
            TiffImageDecoder decoder = reader.CreateImageDecoder(ifd);
            Image image = new Image<Rgba32>(decoder.Width, decoder.Height);
            decoder.Decode(image);
            image.SaveAsGif(ms);
            images.Add(image);
            ifdOffSet = ifd.NextOffset;
        }
    }
}


Limitations and Considerations

A notable limitation of this solution is its lack of support for Photometric interpretation, which may affect the color rendering in some TIFF images. However, for many applications, particularly where color fidelity is not critical, this limitation is manageable.

Results and Impact

The deployment of this solution in a cloud environment demonstrated its effectiveness in converting multi-frame TIFF images to GIFs of varied frame sizes.

Conclusion

This solution successfully bridges the gap in converting multi-frame TIFF images to GIF format in a cross-platform .NET environment. Its adaptability for cloud platforms and ability to handle images of varying sizes make it a valuable tool in the field of image processing.

GIF .NET Framework Cloud Image conversion

Opinions expressed by DZone contributors are their own.

Related

  • .NET Aspire: Building Cloud-Native Applications
  • A Developer-Centric Cloud Architecture Framework (DCAF) for Enterprise Platforms
  • Simplify Your Compliance With Google Cloud Assured Workloads
  • Compliance Automated Standard Solution (COMPASS), Part 7: Compliance-to-Policy for IT Operation Policies Using Auditree

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook