tags:

views:

92

answers:

1

Hi,

I am using BitMiracle LibTIFF. I cannot find any sample code to take a 32bpp argb colour image and write the bitmap to a tiff using this library.

Has anyone else attempted this?

Here is my sample code. It produces a file, but it cannot be viewed by any software that I have.

EDIT: The code now works, but the colors are wrong!

    public static void WriteTiff(Image image, string fileName)
    {
        Bitmap target = image as Bitmap;
        BitmapData bmd = target.LockBits(
            target.GetRectangle(),
            ImageLockMode.ReadOnly,
            PixelFormat.Format32bppArgb);
        var bits = new byte[bmd.Stride * bmd.Height];
        Marshal.Copy(bmd.Scan0, bits, 0, bits.Length);
        target.UnlockBits(bmd);

        Tiff tiff = Tiff.Open(fileName, "w");
        tiff.SetField(TiffTag.IMAGEWIDTH, target.Width);
        tiff.SetField(TiffTag.IMAGELENGTH, target.Height);
        tiff.SetField(TiffTag.COMPRESSION, Compression.NONE);
        tiff.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
        tiff.SetField(TiffTag.BITSPERSAMPLE, 8);
        tiff.SetField(TiffTag.SAMPLESPERPIXEL, 4);
        tiff.SetField(TiffTag.ROWSPERSTRIP, target.Height);
        tiff.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);

        tiff.WriteEncodedStrip(0, bits, bits.Length);
        tiff.Close();

    }

Thanks

A: 

EDIT The latest build of LibTiff.Net contains samples for conversion of a System.Drawing.Bitmap to 32-bit or 24-bit color LZW compressed TIFF images.

There is also samples for conversion of a TIFF image to 32-bit or 24-bit System.Drawing.Bitmaps. /EDIT

You may also want to review "Graphics programming with LibTiff.Net" article in documentation. It should give you basic information about creating color TIFF files.

Bobrovsky