I am trying to use libtiffdotnet to work with TIFF files. I need to convert the TIF to System.Bitmap. There is a sample in the documentation on how it can be done. But this method takes long time on large TIFF files. So I am trying to obtain the scanlines of the original TIFF and write to a Bitmap. Here's the code I am using
using (Tiff image = Tiff.Open(@"input.tif", "r")){
FieldValue[] value = image.GetField(TiffTag.IMAGEWIDTH);
int width = value[0].ToInt();
value = image.GetField(TiffTag.IMAGELENGTH);
int height = value[0].ToInt();
int stride = image.ScanlineSize();
byte[][] scanline = new byte[height][];
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int length = 0;
for (int i = 0; i < height; i++)
{
scanline[i] = new byte[width];
image.ReadScanline(scanline[i], i);
System.Runtime.InteropServices.Marshal.Copy(scanline[i], 0, ptr + length, stride);
length += stride*4;
}
bmp.UnlockBits(bmpData);
bmp.Save(@"output.png");
}
Input.tiff: enter image description here
Output.png : enter image description here
I want to know how it should actually be done, what I am doing wrong.