Exemplo n.º 1
0
        /// <summary>
        /// Decode first frame for the specified file.
        /// </summary>
        ///
        /// <param name="fileName">File name to read image from.</param>
        /// <param name="imageInfo">Information about the decoded image.</param>
        ///
        /// <returns>Return decoded image. In the case if file format support multiple
        /// frames, the method return the first frame.</returns>
        ///
        /// <remarks><para>The method uses table of registered image decoders to find the one,
        /// which should be used for the specified file. If there is not appropriate decoder
        /// found, the method uses default .NET's image decoding routine (see
        /// <see cref="System.Drawing.Image.FromFile(string)"/>).</para></remarks>
        ///
        public static Bitmap DecodeFromFile(string fileName, out ImageInfo imageInfo)
        {
            Bitmap bitmap = null;

            string fileExtension = Path.GetExtension(fileName).ToUpperInvariant();

            if ((fileExtension != string.Empty) && (fileExtension.Length != 0))
            {
                fileExtension = fileExtension.Substring(1);

                if (!decoders.ContainsKey(fileExtension))
                {
                    FormatDecoderAttribute.PopulateDictionaryWithDecodersFromAllAssemblies <IImageDecoder>(decoders, fileExtension);
                }

                if (decoders.ContainsKey(fileExtension))
                {
                    IImageDecoder decoder = (IImageDecoder)Activator.CreateInstance(decoders[fileExtension]);

                    // open stream
                    using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                    {
                        // open decoder
                        decoder.Open(stream);

                        // read the first frame
                        bitmap = decoder.DecodeFrame(0, out imageInfo);

                        decoder.Close();
                    }

                    return(bitmap);
                }
            }

            // use default .NET's image decoding routine
            bitmap = FromFile(fileName);

            imageInfo = new ImageInfo(bitmap.Width, bitmap.Height, Image.GetPixelFormatSize(bitmap.PixelFormat), 0, 1);

            return(bitmap);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Decode a signal from the specified file.
        /// </summary>
        ///
        /// <param name="fileName">File name to read signal from.</param>
        /// <param name="frameInfo">Information about the decoded signal.</param>
        ///
        /// <returns>Return decoded signal.</returns>
        ///
        public static Signal DecodeFromFile(string fileName, out FrameInfo frameInfo)
        {
            Signal signal = null;

            string fileExtension = Path.GetExtension(fileName).ToUpperInvariant();

            if ((fileExtension != string.Empty) && (fileExtension.Length != 0))
            {
                fileExtension = fileExtension.Substring(1);

                if (!decoders.ContainsKey(fileExtension))
                {
                    FormatDecoderAttribute.PopulateDictionaryWithDecodersFromAllAssemblies <IAudioDecoder>(decoders, fileExtension);
                }

                if (decoders.ContainsKey(fileExtension))
                {
                    IAudioDecoder decoder = (IAudioDecoder)Activator.CreateInstance(decoders[fileExtension]);

                    // open stream
                    using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                    {
                        // open decoder
                        decoder.Open(stream);

                        // read all audio frames
                        signal = decoder.Decode();

                        decoder.Close();
                    }

                    frameInfo = new FrameInfo(signal.Channels, signal.SampleRate, Signal.GetSampleSize(signal.SampleFormat), 0, signal.Length);

                    return(signal);
                }
            }

            throw new ArgumentException(String.Format("No suitable decoder has been found for the file format {0}. If ", fileExtension) +
                                        "you are trying to decode .wav files, please add a reference to Accord.Audio.DirectSouond.", "fileName");
        }