Example #1
0
        private void SaveImage(System.Drawing.Image image, string suffix)
        {
            // store the resolution in output file.
            // Call this method after actual draw because it may affect drawing the text
            if (image is Bitmap)
            {
                (image as Bitmap).SetResolution(ResolutionX, ResolutionY);
            }
            if (IsMultiFrameTiff)
            {
                // select the image encoder
                ImageCodecInfo    info = ExportUtils.GetCodec("image/tiff");
                EncoderParameters ep   = new EncoderParameters(2);
                ep.Param[0] = new EncoderParameter(Encoder.Compression, MonochromeTiff ?
                                                   (long)MonochromeTiffCompression : (long)EncoderValue.CompressionLZW);

                if (image == masterTiffImage)
                {
                    // save the master bitmap
                    if (MonochromeTiff)
                    {
                        masterTiffImage = ConvertToBitonal(image as Bitmap);
                    }
                    ep.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
                    masterTiffImage.Save(Stream, info, ep);
                }
                else
                {
                    // save the frame
                    if (MonochromeTiff)
                    {
                        System.Drawing.Image oldImage = image;
                        image = ConvertToBitonal(image as Bitmap);
                        oldImage.Dispose();
                    }
                    ep.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
                    masterTiffImage.SaveAdd(image, ep);
                }
            }
            else if (ImageFormat != ImageExportFormat.Metafile)
            {
                string extension = Path.GetExtension(FileName);
                string fileName  = Path.ChangeExtension(FileName, suffix + extension);
                // empty suffix means that we should use the Stream that was created in the ExportBase
                Stream stream = suffix == "" ? Stream : new FileStream(fileName, FileMode.Create);
                if (suffix != "")
                {
                    GeneratedFiles.Add(fileName);
                }

                if (ImageFormat == ImageExportFormat.Jpeg)
                {
                    ExportUtils.SaveJpeg(image, stream, JpegQuality);
                }
                else if (ImageFormat == ImageExportFormat.Tiff && MonochromeTiff)
                {
                    // handle monochrome tiff separately
                    ImageCodecInfo    info = ExportUtils.GetCodec("image/tiff");
                    EncoderParameters ep   = new EncoderParameters();
                    ep.Param[0] = new EncoderParameter(Encoder.Compression, (long)MonochromeTiffCompression);

                    using (Bitmap bwImage = ConvertToBitonal(image as Bitmap))
                    {
                        bwImage.Save(stream, info, ep);
                    }
                }
                else
                {
                    ImageFormat format = System.Drawing.Imaging.ImageFormat.Bmp;
                    switch (ImageFormat)
                    {
                    case ImageExportFormat.Gif:
                        format = System.Drawing.Imaging.ImageFormat.Gif;
                        break;

                    case ImageExportFormat.Png:
                        format = System.Drawing.Imaging.ImageFormat.Png;
                        break;

                    case ImageExportFormat.Tiff:
                        format = System.Drawing.Imaging.ImageFormat.Tiff;
                        break;
                    }
                    image.Save(stream, format);
                }

                if (suffix != "")
                {
                    stream.Dispose();
                }
            }

            if (image != masterTiffImage)
            {
                image.Dispose();
            }
        }