コード例 #1
0
ファイル: ImageExtensions.cs プロジェクト: prepare/imagetools
        /// <summary>
        /// Converts the image to a stream, which can be assigned to
        /// a silverlight image control as image source and applies the specified
        /// filter before converting the image. The encoder to use will be created by
        /// the file extension name.
        /// </summary>
        /// <param name="image">The image, which should be converted. Cannot be null
        /// (Nothing in Visual Basic).</param>
        /// <param name="extension">The file extension to create the <see cref="IImageEncoder"/> that
        /// will be used to create an image stream.</param>
        /// <returns>The resulting stream.</returns>
        /// <exception cref="ArgumentException"><paramref name="extension"/> is empty or
        /// contains only blanks.</exception>
        /// <exception cref="ArgumentNullException">
        ///     <para><paramref name="image"/> is null (Nothing in Visual Basic).</para>
        ///     <para>- or -</para>
        ///     <para><paramref name="extension"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public static Stream ToStreamByExtension(this ExtendedImage image, string extension)
        {
            Contract.Requires <ArgumentNullException>(image != null, "Image cannot be null.");
            Contract.Requires <ArgumentException>(image.IsFilled, "Image has not been loaded.");
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(extension), "Extension cannot be null or empty.");

            IImageEncoder encoder = null;

            foreach (IImageEncoder availableEncoder in Encoders.GetAvailableEncoders())
            {
                if (availableEncoder != null && availableEncoder.IsSupportedFileExtension(extension))
                {
                    encoder = availableEncoder;
                    break;
                }
            }

            if (encoder == null)
            {
                encoder = new PngEncoder();
            }

            MemoryStream memoryStream = null;

            try
            {
                memoryStream = new MemoryStream();
                encoder.Encode(image, memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
            }
            catch
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                }
                throw;
            }
            return(memoryStream);
        }
コード例 #2
0
ファイル: ImageExtensions.cs プロジェクト: prepare/imagetools
        /// <summary>
        /// Writes to specified image to the stream. The method loops through all encoders and
        /// uses the encoder which supports the extension of the specified file name.
        /// </summary>
        /// <param name="image">The image to write to the stream. Cannot be null.</param>
        /// <param name="stream">The target stream. Cannot be null.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <exception cref="ArgumentNullException">
        ///     <para><paramref name="image"/> is null (Nothing in Visual Basic).</para>
        ///     <para>- or -</para>
        ///     <para><paramref name="stream"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public static void WriteToStream(this ExtendedImage image, Stream stream, string fileName)
        {
            Contract.Requires <ArgumentNullException>(image != null, "Image cannot be null.");
            Contract.Requires <ArgumentException>(image.IsFilled, "Image has not been loaded.");
            Contract.Requires <ArgumentNullException>(stream != null, "Stream cannot be null.");
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(fileName), "File name cannot be null or empty.");

            string path = Path.GetExtension(fileName);

            if (path == null || string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("The file name is not valid and contains no extension.");
            }

            string pathExtension = path.Substring(1);

            Contract.Assume(!string.IsNullOrEmpty(pathExtension));

            IImageEncoder encoder = null;

            foreach (IImageEncoder availableEncoder in Encoders.GetAvailableEncoders())
            {
                if (availableEncoder != null && availableEncoder.IsSupportedFileExtension(pathExtension))
                {
                    encoder = availableEncoder;
                    break;
                }
            }

            if (encoder == null)
            {
                throw new UnsupportedImageFormatException("Specified file extension is not supported.");
            }

            encoder.Encode(image, stream);
        }