/// <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) { string fileExtension = FormatDecoderAttribute.GetNormalizedExtension(fileName); IImageDecoder decoder = FormatDecoderAttribute.GetDecoders(fileExtension, decoderTypes, decoders.Value); if (decoder != null) { // open stream using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { // open decoder decoder.Open(stream); // read the first frame from the image Bitmap bitmap = decoder.DecodeFrame(0, out imageInfo); // close the decoder and return the bitmap decoder.Close(); return(bitmap); } } else { // use default .NET's image decoding routine Bitmap bitmap = FromFile(fileName); decoderTypes[fileExtension] = null; // mark that the file could be loaded using default .NET decoders imageInfo = new ImageInfo(bitmap.Width, bitmap.Height, Image.GetPixelFormatSize(bitmap.PixelFormat), 0, 1); return(bitmap); } }
/// <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); }
/// <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).ToLower( ); if ((fileExtension != string.Empty) && (fileExtension.Length != 0)) { fileExtension = fileExtension.Substring(1); if (decoders.ContainsKey(fileExtension)) { IImageDecoder decoder = decoders[fileExtension]; // open stream FileStream stream = new FileStream(fileName, FileMode.Open); // open decoder decoder.Open(stream); // read the first frame bitmap = decoder.DecodeFrame(0, out imageInfo); // close decoder and stream decoder.Close( ); stream.Close( ); stream.Dispose( ); 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); }
public static Bitmap DecodeFromFile(string fileName, out ImageInfo imageInfo) { Bitmap bitmap = null; string text = Path.GetExtension(fileName).ToLower(); if (text != string.Empty && text.Length != 0) { text = text.Substring(1); if (decoders.ContainsKey(text)) { IImageDecoder imageDecoder = decoders[text]; FileStream fileStream = new FileStream(fileName, FileMode.Open); imageDecoder.Open(fileStream); bitmap = imageDecoder.DecodeFrame(0, out imageInfo); imageDecoder.Close(); fileStream.Close(); fileStream.Dispose(); return(bitmap); } } bitmap = FromFile(fileName); imageInfo = new ImageInfo(bitmap.Width, bitmap.Height, Image.GetPixelFormatSize(bitmap.PixelFormat), 0, 1); return(bitmap); }