private bool SaveAsImageFreeImage(
            Stream stream, PixFileFormat format,
            PixSaveOptions options, int qualityLevel)
        {
            var dib = ToFiBitMap();

            var flags = FREE_IMAGE_SAVE_FLAGS.DEFAULT;

            switch (format)
            {
            case PixFileFormat.Jpeg: if (qualityLevel > 0)
                {
                    flags = (FREE_IMAGE_SAVE_FLAGS)qualityLevel;
                }
                break;

            case PixFileFormat.Exr:  if (PixFormat.Type == typeof(float))
                {
                    flags |= FREE_IMAGE_SAVE_FLAGS.EXR_FLOAT;
                }
                break;

            default: break;
            }

            var result = FreeImage.SaveToStream(dib, stream, s_fifImageFormatMap[format], flags);

            FreeImage.UnloadEx(ref dib);
            return(result);
        }
Exemplo n.º 2
0
        protected bool SaveAsImageSystem(
            Stream stream, PixFileFormat fileFormat,
            PixSaveOptions options, int qualityLevel)
        {
            if (Format == Col.Format.BW)
            {
                return(false);
            }
            BitmapEncoder encoder = null;

            switch (fileFormat)
            {
            case PixFileFormat.Jpeg:
                encoder = qualityLevel > 0
                                ? new JpegBitmapEncoder {
                    QualityLevel = qualityLevel
                }
                                : new JpegBitmapEncoder();
                break;

            default:
                encoder = GetEncoder(fileFormat);
                if (encoder == null)
                {
                    return(false);
                }
                break;
            }
            encoder.Frames.Add(ToBitmapFrame());
            encoder.Save(stream);
            stream.Flush();
            return(true);
        }
Exemplo n.º 3
0
        protected static BitmapEncoder GetEncoder(PixFileFormat format)
        {
            Func <BitmapEncoder> encCreator;

            if (s_FormatEncoder.TryGetValue(format, out encCreator))
            {
                return(encCreator());
            }

            return(null);
        }
Exemplo n.º 4
0
        private bool SaveAsImageDevil(
            string file, PixFileFormat format,
            PixSaveOptions options, int qualityLevel)
        {
            Devil.ImageType imageType;
            if (!s_fileFormats.TryGetValue(format, out imageType))
            {
                return(false);
            }

            return(SaveDevIL(() => IL.Save(imageType, file), qualityLevel));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Save image to stream via devil.
        /// </summary>
        /// <returns>True if the file was successfully saved.</returns>
        internal static bool SaveAsImageDevil(
            this PixImage image,
            Stream stream, PixFileFormat format,
            PixSaveOptions options, int qualityLevel)
        {
            Devil.ImageType imageType;
            if (!s_fileFormats.TryGetValue(format, out imageType))
            {
                return(false);
            }

            return(image.SaveDevIL(() => IL.SaveStream(imageType, stream), qualityLevel));
        }
Exemplo n.º 6
0
        internal static bool SaveAsImageDevil(
            this PixImage image,
            string file, PixFileFormat format,
            PixSaveOptions options, int qualityLevel)
        {
            Devil.ImageType imageType;
            if (!s_fileFormats.TryGetValue(format, out imageType))
            {
                return(false);
            }

            lock (s_devilLock)
            {
                return(image.SaveDevIL(() => IL.Save(imageType, file), qualityLevel));
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Save image to stream via System.Drawing.
        /// </summary>
        /// <returns>True if the file was successfully saved.</returns>
        private bool SaveAsImageBitmap(
            Stream stream, PixFileFormat format,
            PixSaveOptions options, int qualityLevel)
        {
            try
            {
                var self        = this.ToCanonicalDenseLayout();
                var size        = self.Size;
                var pixelFormat = s_pixelFormats[self.PixFormat];
                var imageFormat = s_imageFormats[format];


                using (var bmp = new Bitmap(size.X, size.Y, pixelFormat))
                {
                    var bdata = bmp.LockBits(new Rectangle(0, 0, size.X, size.Y), ImageLockMode.ReadOnly, pixelFormat);
                    self.Data.CopyTo(bdata.Scan0);
                    bmp.UnlockBits(bdata);

                    if (qualityLevel >= 0)
                    {
                        var codec      = s_imageCodecInfos[imageFormat.Guid];
                        var parameters = new EncoderParameters(1);
                        parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)qualityLevel);
                        bmp.Save(stream, codec, parameters);
                        parameters.Dispose();
                    }
                    else
                    {
                        bmp.Save(stream, imageFormat);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }