Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better TIF to PNG conversion with Bitmap.Save()?

Tags:

c#

bitmap

png

tiff

Update: Tried SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; and it's noticeable at full zoom, but it doesn't solve the problem, the problem is just anti-aliased now...

The TIFs are scanned documents, and contain things like lines for tables and text.

Current approach:

using System.Drawing;
using System.Drawing.Imaging;    

var image = Image.FromFile(tifFileName);

Image bitmap = new Bitmap(image, (int)(image.Width), (int)(image.Height));

var imageFinal = new Bitmap(image.Width, image.Height);

var graphic = Graphics.FromImage(imageFinal);

graphic.DrawImage(image, 0, 0, image.Width, image.Height);

using(var imgStream = new MemoryStream())
{
    imageFinal.Save(imgStream, ImageFormat.Png);
    return new MemoryStream(imgStream.GetBuffer());
}

But, it ends up looking like garbage, for example any kind of slightly slanted line has a hint of a stair step, and other fine elements such as text look rough. Especially in comparison to using GIMP to save a TIF as a PNG, which looks great.

So, is there something I can add to make this work better? Or am I going to have to find another approach entirely?

like image 916
MetaGuru Avatar asked Jan 16 '23 12:01

MetaGuru


2 Answers

My immediate impression is that you're going to too much trouble, since you aren't resizing:

var image = Image.FromFile(@"C:\Sample.tiff");
image.Save(@"C:\Sample.png", ImageFormat.Png);

If using the Image type doesn't solve your aliasing problems, try picking your encoder manually:

#region Using Directives

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

#endregion

namespace TiffToBitmap
{
    internal class Program
    {
        private static void Main()
        {
            // Just save the image.
            SaveImage(@"C:\Sample1.png", "image/tiff");

            // Get a byte array from the converted image.
            var imageBytes = GetBytes("image/tiff");

            // Save it for easy comparison.
            File.WriteAllBytes(@"C:\Sample2.png", imageBytes);
        }

        private static byte[] GetBytes(string mimeType)
        {
            var image = Image.FromFile(@"C:\Sample.tiff");

            var encoders = ImageCodecInfo.GetImageEncoders();
            var imageCodecInfo = encoders.FirstOrDefault(encoder => encoder.MimeType == mimeType);

            if (imageCodecInfo == null)
            {
                return null;
            }

            using (var memoryStream = new MemoryStream())
            {
                var imageEncoderParams = new EncoderParameters(1);
                imageEncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                image.Save(memoryStream, imageCodecInfo, imageEncoderParams);

                return memoryStream.GetBuffer();
            }
        }

        private static void SaveImage(string path, string mimeType)
        {
            var image = Image.FromFile(@"C:\Sample.tiff");

            var encoders = ImageCodecInfo.GetImageEncoders();
            var imageCodecInfo = encoders.FirstOrDefault(encoder => encoder.MimeType == mimeType);

            if (imageCodecInfo == null)
            {
                return;
            }

            var imageEncoderParams = new EncoderParameters(1);
            imageEncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
            image.Save(path, imageCodecInfo, imageEncoderParams);
        }
    }
}
like image 106
bopapa_1979 Avatar answered Jan 18 '23 02:01

bopapa_1979


Here is my realization of image/tiff type conversion:

Convertion block:

/// <summary>
/// Convert Tiff image to another mime-type
/// </summary>
/// <param name="tiffImage">Source Tiff file</param>
/// <param name="mimeType">Desired result mime-type</param>
/// <returns>Converted image</returns>
public Bitmap ConvertTiffToBitmap(Image tiffImage, string mimeType)
{
    var imageCodecInfo = ImageCodecInfo.GetImageEncoders().First(encoder => encoder.MimeType == MimeType.Tiff);

    using (var memoryStream = new MemoryStream())
    {
        // Setting encode params
        var imageEncoderParams = new EncoderParameters(1)
        {
            Param = {[0] = new EncoderParameter(Encoder.Quality, 100L)}
        };

        tiffImage.Save(memoryStream, imageCodecInfo, imageEncoderParams);

        var converter = new ImageConverter();

        // Reading stream data to new image
        var tempTiffImage = (Image)converter.ConvertFrom(memoryStream.GetBuffer());

        // Setting new result mime-type
        imageCodecInfo = ImageCodecInfo.GetImageEncoders().First(encoder => encoder.MimeType == mimeType);
        tempTiffImage?.Save(memoryStream, imageCodecInfo, imageEncoderParams);

        return new Bitmap(Image.FromStream(memoryStream, true));
    }
}

public static class MimeType
{
    public const string Bmp = "image/bmp";
    public const string Gif = "image/gif";
    public const string Jpeg = "image/jpeg";
    public const string Png = "image/png";
    public const string Tiff = "image/tiff";
}

Usage:

var sourceImg = Image.FromFile(@"C:\MyImage.tif", true);

var codec = ImageCodecInfo.GetImageDecoders().First(c => c.FormatID == sourceImg.RawFormat.Guid);
if (codec.MimeType == MimeType.Tiff)
{
    sourceImg = ConvertTiffToBitmap(sourceImg, MimeType.Jpeg);
}

Hope, it could be usefull

like image 43
Fragment Avatar answered Jan 18 '23 01:01

Fragment