i am having a tiff file of LZW compression and i wanted to convert the same into CCITT T.6. Is it possible? if sol please give me some advice or sample code about the same. Thanks and regards

Recommended Answers

All 2 Replies

Sure it is. There are plenty of examples of LZW compression if you just search for them. Decompress it, then compress it with CCITT T.6

Search engines are your friend.

The System.Windows.Media libraries make this so easy as to be trivial:

public static class ImageProcessing
{
    /// <summary>
    /// Encodes the provided TIFF file with new compression and pixel format.
    /// </summary>
    /// <param name="path">The source TIFF image file.</param>
    /// <param name="compression">Desired TIFF compression.</param>
    /// <param name="format">Desired pixel format.</param>
    /// <remarks>
    /// The original file is overwritten with the result.
    /// </remarks>
    public static void Encode(string path, TiffCompressOption compression, PixelFormat format)
    {
        var decoder = TiffBitmapDecoder.Create(
            new Uri(path),
            BitmapCreateOptions.PreservePixelFormat,
            BitmapCacheOption.OnLoad);
        var encoder = new TiffBitmapEncoder();

        encoder.Compression = compression;
        encoder.Frames.Add(BitmapFrame.Create(SetPixelFormat(decoder.Frames[0], format)));

        using (var stream = new FileStream(path, FileMode.Truncate, FileAccess.Write))
        {
            encoder.Save(stream);
        }
    }

    /// <summary>
    /// Sets the pixel format for an image.
    /// </summary>
    /// <param name="image">The original image.</param>
    /// <param name="pixelFormat">The desired pixel format.</param>
    /// <returns>The image converted ot the desired pixel format.</returns>
    private static BitmapSource SetPixelFormat(BitmapSource image, PixelFormat format)
    {
        var formatted = new FormatConvertedBitmap();

        formatted.BeginInit();
        formatted.Source = image;
        formatted.DestinationFormat = format;
        formatted.EndInit();

        return formatted;
    }
}

Just call that function like this:

ImageProcessing.Encode("myfile.tif", TiffCompressOption.Ccitt4, PixelFormats.BlackWhite);

Easy peasy. :) Some things are hard with that library, but dicking around with the compression and pixel format of a TIFF is a no brainer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.