private void btnCloseTIFF_Click(object sender, System.EventArgs e) { if (tiff == IntPtr.Zero) { return; } // reset handle, text fields LibTIFF.Close(tiff); tiff = (IntPtr)0; tbTIFFPtr.Text = tiff.ToString(); tbRasterScanlineSize.Text = ""; tbScanlineSize.Text = ""; tbImageDimensions.Text = ""; tbTileDimensions.Text = ""; tbBitsPerSample.Text = ""; tbRowsPerStrip.Text = ""; tbSamplesPerPixel.Text = ""; tbInitialScan0Ptr.Text = ""; tbLastScan0Ptr.Text = ""; tbContrastingTime.Text = ""; // free bitmat data BitmapData bmpData = ((Bitmap)pbPicture.Image).LockBits( new Rectangle(0, 0, pbPicture.Image.Width, pbPicture.Image.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); LibTIFF._free(bmpData.Scan0); ((Bitmap)pbPicture.Image).UnlockBits(bmpData); // dispose and clear Bitmap object pbPicture.Image.Dispose(); pbPicture.Image = null; }
private void tbCreateTIFF_Click(object sender, System.EventArgs e) { if (sfdCreateTIFF.ShowDialog() != DialogResult.OK) { return; } DateTime startTime = DateTime.Now; // image size int width = 1024 * Convert.ToInt16(tbSizeFactorX.Text); int height = 1024 * Convert.ToInt16(tbSizeFactorY.Text); short rowsPerStrip = (short)height; // create file IntPtr newTiff = LibTIFF.Open(sfdCreateTIFF.FileName, "w"); IntPtr pBuffer; if (chkSaveStrips.Checked) { rowsPerStrip = Convert.ToInt16(tbSaveRowsPerStrip.Text); } // set required tag. WATCH THE TYPES!!! LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_IMAGEWIDTH, (short)width); LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_IMAGELENGTH, (short)height); LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_BITSPERSAMPLE, (int)16); LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_SAMPLESPERPIXEL, (short)1); LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_ROWSPERSTRIP, (short)rowsPerStrip); LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_PHOTOMETRIC, (short)TIFFTags.PHOTOMETRIC_MINISBLACK); LibTIFF.SetField(newTiff, (int)TIFFTags.TIFFTAG_PLANARCONFIG, (short)TIFFTags.PLANARCONFIG_CONTIG); if (chkSaveStrips.Checked) { // write strips pBuffer = LibTIFF._malloc(width * 2 * rowsPerStrip); // assume raw data to be provided as short[] short[] shortbits = new short[width * rowsPerStrip]; // strips int strips = height / rowsPerStrip; for (int strip = 0; strip < strips; strip++) { for (int y = 0; y < rowsPerStrip; y++) { // pixels in line: set to specific gray value for (int x = 0; x < width; x++) { // short = -32,768 to 32,767 = signed 16-bit integer // ushort is not supported by Marshal.Copy shortbits[y * width + x] = (short)(10 * y); } } // copy data to unsafe context Marshal.Copy(shortbits, 0, pBuffer, width * rowsPerStrip); // ... and write to file int ret = LibTIFF.WriteRawStrip(newTiff, (uint)strip, pBuffer, width * 2 * rowsPerStrip); if (ret == -1) { Console.WriteLine("writing strip " + strip + " returned " + ret); } } } else { // write scanline by scanline pBuffer = LibTIFF._malloc(width * 2); // 16 bpp // assume raw data to be provided as short[] short[] shortbits = new short[width]; // lines for (int y = 0; y < height; y++) { // pixels in line: set to specific gray value for (int x = 0; x < width; x++) { // short = -32,768 to 32,767 = signed 16-bit integer // ushort is not supported by Marshal.Copy shortbits[x] = (short)(100 * y); } // copy data to unsafe context Marshal.Copy(shortbits, 0, pBuffer, width); // ... and write to file if (!LibTIFF.WriteScanline(newTiff, pBuffer, (uint)y)) { Console.WriteLine("writing line " + y + " failed."); } } } LibTIFF._free(pBuffer); LibTIFF.Close(newTiff); MessageBox.Show("Time: " + (DateTime.Now - startTime)); }