void ConvertImage(string filename) { string source_ext = Path.GetExtension(filename).TrimStart('.').ToLowerInvariant(); string target_name = Path.GetFileName(filename); string target_ext = m_image_format.Extensions.FirstOrDefault(); target_name = Path.ChangeExtension(target_name, target_ext); using (var file = BinaryStream.FromFile(filename)) { var src_format = ImageFormat.FindFormat(file); if (null == src_format) { return; } Stream output = null; try { if (src_format.Item1 == m_image_format && m_image_format.Extensions.Any(ext => ext == source_ext)) { if (AreSamePaths(filename, target_name)) { return; } output = CreateNewFile(target_name); file.Position = 0; file.AsStream.CopyTo(output); } else { file.Position = 0; var image = src_format.Item1.Read(file, src_format.Item2); output = CreateNewFile(target_name); m_image_format.Write(output, image); } } catch // delete destination file on conversion failure { // FIXME if user chooses to overwrite file, and conversion results in error, // then original file will be lost. output.Dispose(); output = null; File.Delete(target_name); throw; } finally { if (output != null) { output.Dispose(); } } } }
void PrintMetaData(string filename) { using (var file = BinaryStream.FromFile(filename)) { var format = ImageFormat.FindFormat(file); if (null == format) { Console.Error.WriteLine("{0}: file format not recognized", filename); return; } var image = format.Item2; Console.WriteLine("{0,16} [{4}] {1}x{2} {3}bpp", filename, image.Width, image.Height, image.BPP, format.Item1.Tag); } }
void ConvertFile(string filename, ImageFormat format) { ImageData image; using (var file = BinaryStream.FromFile(filename)) { image = ImageFormat.Read(file); if (null == image) { Console.Error.WriteLine("{0}: Unknown image format", filename); return; } } string target_ext = format.Extensions.First(); string outname = Path.GetFileNameWithoutExtension(filename) + '.' + target_ext; Console.WriteLine("{0} => {1}", filename, outname); using (var outfile = new FileStream(outname, FileMode.Create, FileAccess.Write)) { format.Write(outfile, image); } }
void ConvertAudio(string filename) { using (var file = BinaryStream.FromFile(filename)) using (var input = AudioFormat.Read(file)) { if (null == input) { return; } string output_name = Path.GetFileName(filename); var source_ext = Path.GetExtension(filename).TrimStart('.').ToLowerInvariant(); string source_format = input.SourceFormat; if (CommonAudioFormats.Contains(source_format)) { if (source_ext == source_format) { return; } output_name = Path.ChangeExtension(output_name, source_format); using (var output = CreateNewFile(output_name)) { input.Source.Position = 0; input.Source.CopyTo(output); } } else { if (source_ext == "wav") { return; } output_name = Path.ChangeExtension(output_name, "wav"); using (var output = CreateNewFile(output_name)) AudioFormat.Wav.Write(input, output); } } }