protected static void TestTiffEncoderCore <TPixel>(
            TestImageProvider <TPixel> provider,
            TiffBitsPerPixel?bitsPerPixel,
            TiffPhotometricInterpretation photometricInterpretation,
            TiffCompression compression = TiffCompression.None,
            TiffPredictor predictor     = TiffPredictor.None,
            bool useExactComparer       = true,
            float compareTolerance      = 0.001f,
            IImageDecoder imageDecoder  = null)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            using Image <TPixel> image = provider.GetImage();
            var encoder = new TiffEncoder
            {
                PhotometricInterpretation = photometricInterpretation,
                BitsPerPixel        = bitsPerPixel,
                Compression         = compression,
                HorizontalPredictor = predictor
            };

            // Does DebugSave & load reference CompareToReferenceInput():
            image.VerifyEncoder(
                provider,
                "tiff",
                bitsPerPixel,
                encoder,
                useExactComparer ? ImageComparer.Exact : ImageComparer.Tolerant(compareTolerance),
                referenceDecoder: imageDecoder ?? ReferenceDecoder);
        }
Exemple #2
0
        private void MenuSaveOnClick(object sender, EventArgs e)
        {
            if (_saveFileDialog.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }
            ImageEncoder encoder = null;

            switch (_saveFileDialog.FilterIndex)
            {
            case 1:
                //for more control over saving, use the PdfCollection and PdfImage classes
                encoder = new PdfEncoder();
                break;

            case 2:
                encoder = new TiffEncoder();
                break;

            case 3:
                encoder = new JpegEncoder();
                break;
            }
            _workspaceViewer.Save(_saveFileDialog.FileName, encoder);
        }
Exemple #3
0
        private void UploadFile(HttpContext context, string filename)
        {
            var file = context.Request.Files[0];

            using (var outStream = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite))
            {
                using (var ms = new MemoryStream())
                {
                    file.InputStream.CopyTo(ms);
                    ms.Seek(0, 0);

                    var decoder = RegisteredDecoders.GetDecoder(ms);
                    if (decoder is TiffDecoder || decoder is PdfDecoder || decoder is OfficeDecoder)
                    {
                        ms.CopyTo(outStream);
                    }
                    else
                    {
                        var ic          = new ImageCollection(ms, null);
                        var tiffEncoder = new TiffEncoder();
                        ic.Save(outStream, tiffEncoder, null);
                    }
                }
            }
        }
        public void EncoderOptions_SetPhotometricInterpretationAndCompression_Works(
            TiffPhotometricInterpretation?photometricInterpretation,
            TiffCompression compression,
            TiffBitsPerPixel expectedBitsPerPixel,
            TiffCompression expectedCompression)
        {
            // arrange
            var tiffEncoder = new TiffEncoder {
                PhotometricInterpretation = photometricInterpretation, Compression = compression
            };

            using Image input   = new Image <Rgb24>(10, 10);
            using var memStream = new MemoryStream();

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);
            TiffFrameMetadata rootFrameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(expectedBitsPerPixel, rootFrameMetaData.BitsPerPixel);
            Assert.Equal(expectedCompression, rootFrameMetaData.Compression);
        }
        public void TiffEncoder_EncodeMultiframe_Create <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            using Image <TPixel> image = provider.GetImage();

            using var image0 = new Image <Rgba32>(image.Width, image.Height, Color.Red.ToRgba32());

            using var image1 = new Image <Rgba32>(image.Width, image.Height, Color.Green.ToRgba32());

            using var image2 = new Image <Rgba32>(image.Width, image.Height, Color.Yellow.ToRgba32());

            image.Frames.AddFrame(image0.Frames.RootFrame);
            image.Frames.AddFrame(image1.Frames.RootFrame);
            image.Frames.AddFrame(image2.Frames.RootFrame);
            image.Frames.RemoveFrame(0);

            TiffBitsPerPixel bitsPerPixel = TiffBitsPerPixel.Bit8;
            var encoder = new TiffEncoder
            {
                PhotometricInterpretation = TiffPhotometricInterpretation.PaletteColor,
                BitsPerPixel = bitsPerPixel,
                Compression  = TiffCompression.Lzw
            };

            using (var ms = new System.IO.MemoryStream())
            {
                image.Save(ms, encoder);

                ms.Position      = 0;
                using var output = Image.Load <Rgba32>(ms);

                Assert.Equal(3, output.Frames.Count);

                ImageFrame <Rgba32> frame0 = output.Frames[0];
                ImageFrame <Rgba32> frame1 = output.Frames[1];
                ImageFrame <Rgba32> frame2 = output.Frames[2];

                Assert.Equal(Color.Red.ToRgba32(), frame0[10, 10]);
                Assert.Equal(Color.Green.ToRgba32(), frame1[10, 10]);
                Assert.Equal(Color.Yellow.ToRgba32(), frame2[10, 10]);

                Assert.Equal(TiffCompression.Lzw, frame0.Metadata.GetTiffMetadata().Compression);
                Assert.Equal(TiffCompression.Lzw, frame1.Metadata.GetTiffMetadata().Compression);
                Assert.Equal(TiffCompression.Lzw, frame1.Metadata.GetTiffMetadata().Compression);

                Assert.Equal(TiffPhotometricInterpretation.PaletteColor, frame0.Metadata.GetTiffMetadata().PhotometricInterpretation);
                Assert.Equal(TiffPhotometricInterpretation.PaletteColor, frame1.Metadata.GetTiffMetadata().PhotometricInterpretation);
                Assert.Equal(TiffPhotometricInterpretation.PaletteColor, frame2.Metadata.GetTiffMetadata().PhotometricInterpretation);
            }

            image.VerifyEncoder(
                provider,
                "tiff",
                bitsPerPixel,
                encoder,
                ImageComparer.Exact);
        }
        public void TiffEncode_WorksWithDiscontiguousBuffers <TPixel>(TestImageProvider <TPixel> provider, TiffPhotometricInterpretation photometricInterpretation)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            provider.LimitAllocatorBufferCapacity().InPixelsSqrt(200);
            using Image <TPixel> image = provider.GetImage();

            var encoder = new TiffEncoder {
                PhotometricInterpretation = photometricInterpretation
            };

            image.DebugSave(provider, encoder);
        }
Exemple #7
0
        public void TiffCore()
        {
            TiffPhotometricInterpretation photometricInterpretation = TiffPhotometricInterpretation.Rgb;

            var encoder = new TiffEncoder()
            {
                Compression = this.Compression, PhotometricInterpretation = photometricInterpretation
            };

            using var memoryStream = new MemoryStream();
            this.core.SaveAsTiff(memoryStream, encoder);
        }
        public void TiffEncoder_PreservesCompression <TPixel>(TestImageProvider <TPixel> provider, TiffCompression expectedCompression)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            // arrange
            var tiffEncoder = new TiffEncoder();

            using Image <TPixel> input = provider.GetImage();
            using var memStream = new MemoryStream();

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);
            Assert.Equal(expectedCompression, output.Frames.RootFrame.Metadata.GetTiffMetadata().Compression);
        }
Exemple #9
0
        public void TiffCore()
        {
            TiffPhotometricInterpretation photometricInterpretation = TiffPhotometricInterpretation.Rgb;

            // Workaround for 1-bit bug
            if (this.Compression == TiffCompression.CcittGroup3Fax || this.Compression == TiffCompression.Ccitt1D)
            {
                photometricInterpretation = TiffPhotometricInterpretation.WhiteIsZero;
            }

            var encoder = new TiffEncoder()
            {
                Compression = this.Compression, PhotometricInterpretation = photometricInterpretation
            };

            using var memoryStream = new MemoryStream();
            this.core.SaveAsTiff(memoryStream, encoder);
        }
        public void TiffEncoder_PreservesBitsPerPixel_WhenInputIsL8()
        {
            // arrange
            var tiffEncoder = new TiffEncoder();

            using Image input   = new Image <L8>(10, 10);
            using var memStream = new MemoryStream();
            TiffBitsPerPixel expectedBitsPerPixel = TiffBitsPerPixel.Bit8;

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);
            TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(expectedBitsPerPixel, frameMetaData.BitsPerPixel);
        }
        private string UploadFile(HttpContext context, string filename)
        {
            string         msg  = "";
            HttpPostedFile file = context.Request.Files[0];

            byte[] fileBytes = new byte[file.ContentLength];
            file.InputStream.Read(fileBytes, 0, file.ContentLength);


            using (FileStream outStream = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(fileBytes, 0, fileBytes.Length);
                    ms.Position = 0;
                    try
                    {
                        ImageDecoder decoder = RegisteredDecoders.GetDecoder(ms);
                        if (decoder is TiffDecoder)
                        {
                            outStream.Write(fileBytes, 0, fileBytes.Length);
                            outStream.Position = 0;
                        }
                        else
                        {
                            ImageCollection ic          = new ImageCollection(ms, null);
                            TiffEncoder     tiffEncoder = new TiffEncoder();
                            ic.Save(outStream, tiffEncoder, null);
                        }
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                    }

                    ms.Position = 0;
                }
            }

            return(msg);
        }
        public void EncoderOptions_UnsupportedBitPerPixel_DefaultTo24Bits(TiffBitsPerPixel bitsPerPixel)
        {
            // arrange
            var tiffEncoder = new TiffEncoder {
                BitsPerPixel = bitsPerPixel
            };

            using Image input   = new Image <Rgb24>(10, 10);
            using var memStream = new MemoryStream();

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);

            TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(TiffBitsPerPixel.Bit24, frameMetaData.BitsPerPixel);
        }
        /// <summary>
        /// Examines the file extension on fileName to return the correct ImageEncoder
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private ImageEncoder GetEncoder(string fileName)
        {
            string ext = Path.GetExtension(fileName);

            ImageEncoder returnEnc = null;

            if (ext != null)
            {
                switch (ext.ToLower())
                {
                // Not using these
                //case ".jpg":
                //case ".jpeg":
                //    returnEnc = new JpegEncoder();
                //    break;
                //case ".png":
                //    returnEnc = new PngEncoder();
                //    break;
                //case ".gif":
                //    returnEnc = new GifEncoder();
                //    break;
                //case ".bmp":
                //    returnEnc = new BmpEncoder();
                //    break;
                case ".tif":
                case ".tiff":
                    returnEnc = new TiffEncoder();
                    break;

                case ".pdf":
                    returnEnc = new PdfEncoder();
                    break;

                default:
                    returnEnc = null;
                    break;
                }
            }
            return(returnEnc);
        }
        public void EncoderOptions_SetBitPerPixel_Works(TiffBitsPerPixel bitsPerPixel)
        {
            // arrange
            var tiffEncoder = new TiffEncoder {
                BitsPerPixel = bitsPerPixel
            };

            using Image input   = new Image <Rgb24>(10, 10);
            using var memStream = new MemoryStream();

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);

            TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(bitsPerPixel, frameMetaData.BitsPerPixel);
            Assert.Equal(TiffCompression.None, frameMetaData.Compression);
        }
        public void TiffEncoder_EncodeMultiframe_RemoveFrames <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            using Image <TPixel> image = provider.GetImage();
            Assert.True(image.Frames.Count > 1);

            image.Frames.RemoveFrame(0);

            TiffBitsPerPixel bitsPerPixel = TiffBitsPerPixel.Bit24;
            var encoder = new TiffEncoder
            {
                PhotometricInterpretation = TiffPhotometricInterpretation.Rgb,
                BitsPerPixel = bitsPerPixel,
                Compression  = TiffCompression.Deflate
            };

            image.VerifyEncoder(
                provider,
                "tiff",
                bitsPerPixel,
                encoder,
                ImageComparer.Exact);
        }
        /// <summary>
        /// Examines the file extension on fileName to return the correct ImageEncoder.
        /// </summary>
        /// <param name="fileName">Name of the file to examine.</param>
        /// <returns><see cref="ImageEncoder"/> instance that can be used to encode specified file.</returns>
        private ImageEncoder GetEncoder(string fileName)
        {
            string ext = Path.GetExtension(fileName);

            ImageEncoder returnEnc = null;

            if (ext != null)
            {
                switch (ext.ToLower())
                {
                case ".tif":
                case ".tiff":
                    returnEnc = new TiffEncoder();
                    break;

                case ".pdf":
                    returnEnc = new PdfEncoder();
                    break;
                }
            }

            return(returnEnc);
        }
        public void ConvertToGrayScale(string sourceFile, string targetFile)
        {
            MultiFramedImageEncoder encoder = new TiffEncoder();

            using (ImageSource src = new FileSystemImageSource(sourceFile, true))
            {
                ImageCollection images = new ImageCollection();

                while (src.HasMoreImages())
                {
                    AtalaImage img = src.AcquireNext();

                    img = img.GetChangedPixelFormat(PixelFormat.Pixel8bppGrayscale);
                    images.Add(img);
                    //img.Dispose();
                }

                using (Stream s = File.Create(targetFile))
                {
                    encoder.Save(s, images, null);
                }
            }
        }
        public void TiffEncoder_EncodesWithCorrectBiColorModeCompression <TPixel>(TestImageProvider <TPixel> provider, TiffCompression compression, TiffCompression expectedCompression)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            // arrange
            var encoder = new TiffEncoder()
            {
                Compression = compression, BitsPerPixel = TiffBitsPerPixel.Bit1
            };

            using Image <TPixel> input = provider.GetImage();
            using var memStream = new MemoryStream();

            // act
            input.Save(memStream, encoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);
            TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata();

            Assert.Equal(TiffBitsPerPixel.Bit1, frameMetaData.BitsPerPixel);
            Assert.Equal(expectedCompression, frameMetaData.Compression);
        }
        //returns an encoder based on image type
        public static ImageEncoder GetEncoderFromType(ImageType type)
        {
            ImageEncoder encoder = null;

            switch (type)
            {
            case ImageType.Jpeg:
                encoder = new JpegEncoder();
                break;

            case ImageType.Png:
                encoder = new PngEncoder();
                break;

            case ImageType.J2k:
                encoder = new Jp2Encoder();
                break;

            case ImageType.Bmp:
                encoder = new BmpEncoder();
                break;

            case ImageType.Emf:
                encoder = new EmfEncoder();
                break;

            case ImageType.Gif:
                encoder = new GifEncoder();
                break;

            case ImageType.Pcx:
                encoder = new PcxEncoder();
                break;

            case ImageType.Psd:
                encoder = new PsdEncoder();
                break;

            case ImageType.Tga:
                encoder = new TgaEncoder();
                break;

            case ImageType.Tiff:
                encoder = new TiffEncoder();
                break;

            case ImageType.Wbmp:
                encoder = new WbmpEncoder();
                break;

            case ImageType.Wmf:
                encoder = new WmfEncoder();
                break;

            case ImageType.Tla:
                encoder = new TlaEncoder();
                break;

            default:
                MessageBox.Show("当前的图像格式不支持");
                break;
            }

            return(encoder);
        }
 /// <summary>
 /// Saves the image to the given stream with the Tiff format.
 /// </summary>
 /// <param name="source">The image this method extends.</param>
 /// <param name="stream">The stream to save the image to.</param>
 /// <param name="encoder">The encoder to save the image with.</param>
 /// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 public static void SaveAsTiff(this Image source, Stream stream, TiffEncoder encoder)
 => source.Save(
     stream,
     encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TiffFormat.Instance));
Exemple #21
0
        public void Encode_PreservesMetadata <TPixel>(TestImageProvider <TPixel> provider)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            // Load Tiff image
            using Image <TPixel> image = provider.GetImage(new TiffDecoder()
            {
                IgnoreMetadata = false
            });

            ImageMetadata       inputMetaData  = image.Metadata;
            ImageFrame <TPixel> rootFrameInput = image.Frames.RootFrame;
            TiffFrameMetadata   frameMetaInput = rootFrameInput.Metadata.GetTiffMetadata();

            byte[]      xmpProfileInput  = rootFrameInput.Metadata.XmpProfile;
            ExifProfile exifProfileInput = rootFrameInput.Metadata.ExifProfile;

            Assert.Equal(TiffCompression.Lzw, frameMetaInput.Compression);
            Assert.Equal(TiffBitsPerPixel.Bit4, frameMetaInput.BitsPerPixel);

            // Save to Tiff
            var tiffEncoder = new TiffEncoder()
            {
                PhotometricInterpretation = TiffPhotometricInterpretation.Rgb
            };

            using var ms = new MemoryStream();
            image.Save(ms, tiffEncoder);

            // Assert
            ms.Position            = 0;
            using var encodedImage = Image.Load <Rgba32>(ms);

            ImageMetadata       encodedImageMetaData         = encodedImage.Metadata;
            ImageFrame <Rgba32> rootFrameEncodedImage        = encodedImage.Frames.RootFrame;
            TiffFrameMetadata   tiffMetaDataEncodedRootFrame = rootFrameEncodedImage.Metadata.GetTiffMetadata();
            ExifProfile         encodedImageExifProfile      = rootFrameEncodedImage.Metadata.ExifProfile;

            byte[] encodedImageXmpProfile = rootFrameEncodedImage.Metadata.XmpProfile;

            Assert.Equal(TiffBitsPerPixel.Bit4, tiffMetaDataEncodedRootFrame.BitsPerPixel);
            Assert.Equal(TiffCompression.Lzw, tiffMetaDataEncodedRootFrame.Compression);

            Assert.Equal(inputMetaData.HorizontalResolution, encodedImageMetaData.HorizontalResolution);
            Assert.Equal(inputMetaData.VerticalResolution, encodedImageMetaData.VerticalResolution);
            Assert.Equal(inputMetaData.ResolutionUnits, encodedImageMetaData.ResolutionUnits);

            Assert.Equal(rootFrameInput.Width, rootFrameEncodedImage.Width);
            Assert.Equal(rootFrameInput.Height, rootFrameEncodedImage.Height);

            PixelResolutionUnit resolutionUnitInput   = UnitConverter.ExifProfileToResolutionUnit(exifProfileInput);
            PixelResolutionUnit resolutionUnitEncoded = UnitConverter.ExifProfileToResolutionUnit(encodedImageExifProfile);

            Assert.Equal(resolutionUnitInput, resolutionUnitEncoded);
            Assert.Equal(exifProfileInput.GetValue(ExifTag.XResolution).Value.ToDouble(), encodedImageExifProfile.GetValue(ExifTag.XResolution).Value.ToDouble());
            Assert.Equal(exifProfileInput.GetValue(ExifTag.YResolution).Value.ToDouble(), encodedImageExifProfile.GetValue(ExifTag.YResolution).Value.ToDouble());

            Assert.Equal(xmpProfileInput, encodedImageXmpProfile);

            Assert.Equal("IrfanView", exifProfileInput.GetValue(ExifTag.Software).Value);
            Assert.Equal("This is Название", exifProfileInput.GetValue(ExifTag.ImageDescription).Value);
            Assert.Equal("This is Изготовитель камеры", exifProfileInput.GetValue(ExifTag.Make).Value);
            Assert.Equal("This is Авторские права", exifProfileInput.GetValue(ExifTag.Copyright).Value);

            Assert.Equal(exifProfileInput.GetValue(ExifTag.ImageDescription).Value, encodedImageExifProfile.GetValue(ExifTag.ImageDescription).Value);
            Assert.Equal(exifProfileInput.GetValue(ExifTag.Make).Value, encodedImageExifProfile.GetValue(ExifTag.Make).Value);
            Assert.Equal(exifProfileInput.GetValue(ExifTag.Copyright).Value, encodedImageExifProfile.GetValue(ExifTag.Copyright).Value);

            // Note that the encoded profile has PlanarConfiguration explicitly set, which is missing in the original image profile.
            Assert.Equal((ushort)TiffPlanarConfiguration.Chunky, encodedImageExifProfile.GetValue(ExifTag.PlanarConfiguration)?.Value);
            Assert.Equal(exifProfileInput.Values.Count + 1, encodedImageExifProfile.Values.Count);
        }
 /// <summary>
 /// Saves the image to the given stream with the Tiff format.
 /// </summary>
 /// <param name="source">The image this method extends.</param>
 /// <param name="stream">The stream to save the image to.</param>
 /// <param name="encoder">The encoder to save the image with.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 public static Task SaveAsTiffAsync(this Image source, Stream stream, TiffEncoder encoder, CancellationToken cancellationToken = default) =>
 source.SaveAsync(
     stream,
     encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TiffFormat.Instance),
     cancellationToken);
        protected static void TestStripLength <TPixel>(
            TestImageProvider <TPixel> provider,
            TiffPhotometricInterpretation photometricInterpretation,
            TiffCompression compression,
            bool useExactComparer  = true,
            float compareTolerance = 0.01f)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            // arrange
            var tiffEncoder = new TiffEncoder()
            {
                PhotometricInterpretation = photometricInterpretation, Compression = compression
            };

            using Image <TPixel> input = provider.GetImage();
            using var memStream = new MemoryStream();
            TiffFrameMetadata inputMeta        = input.Frames.RootFrame.Metadata.GetTiffMetadata();
            TiffCompression   inputCompression = inputMeta.Compression ?? TiffCompression.None;

            // act
            input.Save(memStream, tiffEncoder);

            // assert
            memStream.Position = 0;
            using var output   = Image.Load <Rgba32>(memStream);
            ExifProfile         exifProfileOutput = output.Frames.RootFrame.Metadata.ExifProfile;
            TiffFrameMetadata   outputMeta        = output.Frames.RootFrame.Metadata.GetTiffMetadata();
            ImageFrame <Rgba32> rootFrame         = output.Frames.RootFrame;

            Number rowsPerStrip = exifProfileOutput.GetValue(ExifTag.RowsPerStrip) != null?exifProfileOutput.GetValue(ExifTag.RowsPerStrip).Value : TiffConstants.RowsPerStripInfinity;

            Assert.True(output.Height > (int)rowsPerStrip);
            Assert.True(exifProfileOutput.GetValue(ExifTag.StripOffsets)?.Value.Length > 1);
            Number[] stripByteCounts = exifProfileOutput.GetValue(ExifTag.StripByteCounts)?.Value;
            Assert.NotNull(stripByteCounts);
            Assert.True(stripByteCounts.Length > 1);
            Assert.NotNull(outputMeta.BitsPerPixel);

            foreach (Number sz in stripByteCounts)
            {
                Assert.True((uint)sz <= TiffConstants.DefaultStripSize);
            }

            // For uncompressed more accurate test.
            if (compression == TiffCompression.None)
            {
                for (int i = 0; i < stripByteCounts.Length - 1; i++)
                {
                    // The difference must be less than one row.
                    int stripBytes = (int)stripByteCounts[i];
                    int widthBytes = ((int)outputMeta.BitsPerPixel + 7) / 8 * rootFrame.Width;

                    Assert.True((TiffConstants.DefaultStripSize - stripBytes) < widthBytes);
                }
            }

            // Compare with reference.
            TestTiffEncoderCore(
                provider,
                inputMeta.BitsPerPixel,
                photometricInterpretation,
                inputCompression,
                useExactComparer: useExactComparer,
                compareTolerance: compareTolerance);
        }
        //returns an encoder based on image type
        public static ImageEncoder GetEncoderFromType( ImageType type )
        {
            ImageEncoder encoder = null;
            switch ( type )
            {
                case ImageType.Jpeg:
                    encoder = new JpegEncoder();
                    break;
                case ImageType.Png:
                    encoder = new PngEncoder();
                    break;
				case ImageType.J2k:
					encoder = new Jp2Encoder();
					break;
                case ImageType.Bmp:
                    encoder = new BmpEncoder();
                    break;
                case ImageType.Emf:
                    encoder = new EmfEncoder();
                    break;
                case ImageType.Gif:
                    encoder = new GifEncoder();
                    break;
                case ImageType.Pcx:
                    encoder = new PcxEncoder();
                    break;
                case ImageType.Psd:
                    encoder = new PsdEncoder();
                    break;
                case ImageType.Tga:
                    encoder = new TgaEncoder();
                    break;
                case ImageType.Tiff:
                    encoder = new TiffEncoder();
                    break;
                case ImageType.Wbmp:
                    encoder = new WbmpEncoder();
                    break;
                case ImageType.Wmf:
                    encoder = new WmfEncoder();
                    break;
                case ImageType.Tla:
                    encoder = new TlaEncoder();
                    break;
                default:
                    MessageBox.Show( "当前的图像格式不支持" );
                    break;
            }

            return encoder;
        } 
        /// <summary>
        /// Shows the encoder settings dialog and initializes the encoder.
        /// </summary>
        /// <param name="encoder">The encoder.</param>
        /// <param name="canAddImagesToExistingFile">The value indicating whether encoder can add images to the existing multipage image file.</param>
        /// <returns>
        /// <b>True</b> if settings are applied to the encoder; otherwise, <b>false</b>.
        /// </returns>
        protected virtual bool ShowEncoderSettingsDialog(
            ref EncoderBase encoder,
            bool canAddImagesToExistingFile)
        {
            // set encoder settings
            switch (encoder.Name)
            {
            case "Bmp":
                using (BmpEncoderSettingsForm bmpEncoderSettingsForm = new BmpEncoderSettingsForm())
                {
                    SetEncoderSettingsDialogProperties(bmpEncoderSettingsForm);
                    if (bmpEncoderSettingsForm.ShowDialog() != DialogResult.OK)
                    {
                        return(false);
                    }

                    BmpEncoder bmpEncoder = (BmpEncoder)encoder;

                    bmpEncoder.Settings = bmpEncoderSettingsForm.EncoderSettings;
                    return(true);
                }

            case "Png":
                using (PngEncoderSettingsForm pngEncoderSettingsForm = new PngEncoderSettingsForm())
                {
                    SetEncoderSettingsDialogProperties(pngEncoderSettingsForm);
                    if (pngEncoderSettingsForm.ShowDialog() != DialogResult.OK)
                    {
                        return(false);
                    }

                    PngEncoder pngEncoder = (PngEncoder)encoder;

                    pngEncoder.Settings = pngEncoderSettingsForm.EncoderSettings;
                    return(true);
                }

            case "Gif":
                using (GifEncoderSettingsForm gifEncoderSettingsForm = new GifEncoderSettingsForm())
                {
                    SetEncoderSettingsDialogProperties(gifEncoderSettingsForm);
                    gifEncoderSettingsForm.CanAddImagesToExistingFile = canAddImagesToExistingFile;
                    if (gifEncoderSettingsForm.ShowDialog() != DialogResult.OK)
                    {
                        return(false);
                    }

                    GifEncoder gifEncoder = (GifEncoder)encoder;

                    gifEncoder.Settings      = gifEncoderSettingsForm.EncoderSettings;
                    gifEncoder.CreateNewFile = !gifEncoderSettingsForm.AddImagesToExistingFile;
                    return(true);
                }

            case "Jpeg":
                using (JpegEncoderSettingsForm jpegEncoderSettingsForm = new JpegEncoderSettingsForm())
                {
                    SetEncoderSettingsDialogProperties(jpegEncoderSettingsForm);
                    if (jpegEncoderSettingsForm.ShowDialog() != DialogResult.OK)
                    {
                        return(false);
                    }

                    JpegEncoder jpegEncoder = (JpegEncoder)encoder;

                    jpegEncoder.Settings = jpegEncoderSettingsForm.EncoderSettings;
                    return(true);
                }

            case "Tiff":
                using (TiffEncoderSettingsForm tiffEncoderSettingsForm = new TiffEncoderSettingsForm())
                {
                    SetEncoderSettingsDialogProperties(tiffEncoderSettingsForm);
                    tiffEncoderSettingsForm.CanAddImagesToExistingFile = canAddImagesToExistingFile;
                    if (tiffEncoderSettingsForm.ShowDialog() != DialogResult.OK)
                    {
                        return(false);
                    }

                    TiffEncoder tiffEncoder = (TiffEncoder)encoder;

                    tiffEncoder.Settings      = tiffEncoderSettingsForm.EncoderSettings;
                    tiffEncoder.CreateNewFile = !tiffEncoderSettingsForm.AddImagesToExistingFile;
                    return(true);
                }

            case "Svg":
                using (SvgEncoderSettingsForm svgEncoderSettingsForm = new SvgEncoderSettingsForm())
                {
                    SetEncoderSettingsDialogProperties(svgEncoderSettingsForm);
                    if (svgEncoderSettingsForm.ShowDialog() != DialogResult.OK)
                    {
                        return(false);
                    }

                    SvgEncoder svgEncoder = (SvgEncoder)encoder;

                    svgEncoder.Settings = svgEncoderSettingsForm.EncoderSettings;
                    return(true);
                }
            }

            return(false);
        }