1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73:
| public Bitmap saveRoutine(string fileName, byte[] iData) { Bitmap akImage; byte[] buffer = null; ushort[] bufi = null;
int width = 2560; int height = 2160; int rowMultiplier = 0; int counterWidth = 0; byte[] _imageData = iData; unsafe { akImage = new Bitmap(2560, 2160, PixelFormat.Format16bppGrayScale); Rectangle dimension2 = new Rectangle(0, 0, akImage.Width, akImage.Height); BitmapData picData2 = akImage.LockBits(dimension2, ImageLockMode.ReadWrite, PixelFormat.Format16bppGrayScale); IntPtr pixelStartAddress2 = picData2.Scan0; System.Runtime.InteropServices.Marshal.Copy(_imageData, 0, pixelStartAddress2, _imageData.Length); akImage.UnlockBits(picData2); Tiff outputImage; using (outputImage = Tiff.Open(fileName, "w")) { bufi = new ushort[akImage.Width * akImage.Height];
outputImage.SetField(TiffTag.IMAGEWIDTH, width); outputImage.SetField(TiffTag.IMAGELENGTH, height); outputImage.SetField(TiffTag.SAMPLESPERPIXEL, 1); outputImage.SetField(TiffTag.BITSPERSAMPLE, 16); outputImage.SetField(TiffTag.ORIENTATION, BitMiracle.LibTiff.Classic.Orientation.TOPLEFT); outputImage.SetField(TiffTag.ROWSPERSTRIP, height); outputImage.SetField(TiffTag.XRESOLUTION, 2560); outputImage.SetField(TiffTag.YRESOLUTION, 2160); outputImage.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.CENTIMETER); outputImage.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG); outputImage.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK); outputImage.SetField(TiffTag.COMPRESSION, Compression.LZW); outputImage.SetField(TiffTag.FILLORDER, FillOrder.MSB2LSB); for (int row = 0; row < akImage.Height; ++row) { ushort* ptr = (ushort*)picData2.Scan0+(row *picData2.Stride); rowMultiplier = row + 1; counterWidth = 0;
for (int x = (akImage.Width * rowMultiplier) - akImage.Width; x < (akImage.Width * rowMultiplier); ++x ,++ptr) { bufi[counterWidth] = (ushort)ptr; counterWidth++; } buffer = new byte[bufi.Length * sizeof(ushort)]; Buffer.BlockCopy(bufi, 0, buffer, 0, buffer.Length); outputImage.WriteScanline(buffer, row); bufi = new ushort[akImage.Width * akImage.Height]; } } akImage.Dispose(); outputImage.Dispose(); } return akImage; } |