示例#1
0
        /// <summary>
        /// Encodes the image using a specified encoder.
        /// </summary>
        /// <param name="imageEncoder">The image encoder to use.</param>
        /// <returns>A new, corresponding encoded image.</returns>
        public EncodedImage Encode(IImageToStreamEncoder imageEncoder)
        {
            var encodedImage = new EncodedImage(this.Width, this.Height, this.PixelFormat);

            encodedImage.EncodeFrom(this, imageEncoder);
            return(encodedImage);
        }
示例#2
0
        /// <summary>
        /// Encodes an image in-place into the given encoded image instance using the specified encoder.
        /// </summary>
        /// <param name="encodedImage">Encoded image into which to encode in-place.</param>
        /// <param name="image">Image to be encoded.</param>
        /// <param name="encoder">Encoder to use.</param>
        public static void EncodeFrom(EncodedImage encodedImage, Image image, BitmapEncoder encoder)
        {
            System.Windows.Media.PixelFormat format;
            if (image.PixelFormat == PixelFormat.BGR_24bpp)
            {
                format = System.Windows.Media.PixelFormats.Bgr24;
            }
            else if (image.PixelFormat == PixelFormat.Gray_16bpp)
            {
                format = System.Windows.Media.PixelFormats.Gray16;
            }
            else if (image.PixelFormat == PixelFormat.Gray_8bpp)
            {
                format = System.Windows.Media.PixelFormats.Gray8;
            }
            else
            {
                format = System.Windows.Media.PixelFormats.Bgr32;
            }

            encodedImage.EncodeFrom(image, (_, stream) =>
            {
                BitmapSource bitmapSource = BitmapSource.Create(image.Width, image.Height, 96, 96, format, null, image.ImageData, image.Stride * image.Height, image.Stride);
                encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
                encoder.Save(stream);
            });
        }
示例#3
0
        /// <summary>
        /// Decodes a specified encoded image with a specified decoder into the current image.
        /// </summary>
        /// <param name="encodedImage">The encoded image to decode.</param>
        /// <param name="imageDecoder">The image decoder to use.</param>
        /// <remarks>The image width, height and pixel format must match. The method should not be called concurrently.</remarks>
        public void DecodeFrom(EncodedImage encodedImage, IImageFromStreamDecoder imageDecoder)
        {
            if (encodedImage.Width != this.Width || encodedImage.Height != this.Height ||
                (encodedImage.PixelFormat != PixelFormat.Undefined && encodedImage.PixelFormat != this.PixelFormat))
            {
                throw new InvalidOperationException("Cannot decode from an encoded image that has a different width, height, or pixel format.");
            }

            imageDecoder.DecodeFromStream(encodedImage.ToStream(), this);
        }