使用LibTiff.net创建平铺的Tiff

时间:2017-04-19 12:51:42

标签: c# tiff libtiff.net

我使用LibTiff.net来裁剪平铺Tiff的一部分并将其导出为平铺的Tiff,但遇到了"无法将平铺图像写入剥离图像的问题"。虽然" Tiff.open(" out.tif"," w")制作一个剥离图像,但如何创建一个平铺Tiff来填充输入数据呢?

using (Tiff input = Tiff.Open(@"E:\Sample_04.tif", "r"))
        {

            int width = input.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
            int height = input.GetField(TiffTag.IMAGELENGTH)[0].ToInt();

            int tileWidth = input.GetField(TiffTag.TILEWIDTH)[0].ToInt();
            int tileLentgh = input.GetField(TiffTag.TILELENGTH)[0].ToInt();

            int samplesPerPixel = input.GetField(TiffTag.SAMPLESPERPIXEL)[0].ToInt();
            int bitsPerSample = input.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt();
            int photo = input.GetField(TiffTag.PHOTOMETRIC)[0].ToInt();
            int config = input.GetField(TiffTag.PLANARCONFIG)[0].ToInt();

            int tiles = 0;
            int tileSize = input.TileSize();
            byte[][] buffer = new byte[tileSize][];


            int tileHeightCount = height / tileLentgh;
            int tileWidthCount = width / tileWidth;


            for (int y = 0; y < tileLentgh*5; y += tileLentgh)
            {
                for (int x = 0; x < tileWidth*5; x += tileWidth)
                {
                    buffer[tiles] = new byte[tileSize];
                    input.ReadTile(buffer[tiles], 0, x, y, 0, 0);
                    tiles++;
                }
            }


            // writing
            using (Tiff output = Tiff.Open("out.tif", "w"))
            {

                output.SetField(TiffTag.SAMPLESPERPIXEL, samplesPerPixel);
                output.SetField(TiffTag.IMAGEWIDTH, width );
                output.SetField(TiffTag.IMAGELENGTH, height);
                output.SetField(TiffTag.BITSPERSAMPLE, bitsPerSample);
                output.SetField(TiffTag.ROWSPERSTRIP, output.DefaultStripSize(0));
                output.SetField(TiffTag.PHOTOMETRIC, photo);
                output.SetField(TiffTag.PLANARCONFIG, config);

                int c = 0;

                for (int y = 0; y < tileLentgh*5; y += tileLentgh)
                {
                    for (int x = 0; x < tileWidth*5; x += tileWidth)
                    {

                        output.WriteTile(buffer[c], x, y, 0, 0);
                        c++;
                    }
                }
            }
        }
        System.Diagnostics.Process.Start("555.tif");

    }

1 个答案:

答案 0 :(得分:0)

要创建平铺的TIFF,请不要使用

output.SetField(TiffTag.ROWSPERSTRIP, output.DefaultStripSize(0));

在使用TiffTag.TILEWIDTH方法之前,您绝对必须设置TiffTag.TILELENGTHWriteTile字段。