使用Bitmap.Save()更好的TIF到PNG转换?

时间:2012-09-21 14:29:07

标签: c# bitmap png tiff

更新:尝试SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;并且在完全缩放时显而易见,但它无法解决问题,现在问题只是消除锯齿......

TIF是扫描文档,包含表格和文本行等内容。

目前的做法:

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());
}

但是,它最终看起来像垃圾,例如任何一种略微倾斜的线条都有一个阶梯的暗示,而其他精细元素如文字看起来很粗糙。特别是与使用GIMP将TIF保存为PNG相比,这看起来很棒。

那么,我可以添加一些东西来使这项工作更好吗?或者我将不得不完全找到另一种方法?

2 个答案:

答案 0 :(得分:6)

我的直接印象是,由于你没有调整大小,你会遇到太多麻烦:

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

如果使用图像类型无法解决锯齿问题,请尝试手动选择编码器:

#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);
        }
    }
}

答案 1 :(得分:1)

这是我对image / tiff类型转换的实现:

转换块:

/// <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";
}

用法:

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);
}

希望,它可能是有用的