/// <summary>Reads metadata from an <see cref="Stream"/>.</summary> /// <param name="stream">A stream from which the file data may be read. The stream must be positioned at the beginning of the file's data.</param> /// <returns>A list of <see cref="Directory"/> instances containing the various types of metadata found within the file's data.</returns> /// <exception cref="ImageProcessingException">The file type is unknown, or processing errors occurred.</exception> /// <exception cref="System.IO.IOException"/> public static DirectoryList ReadMetadata(Stream stream) { var fileType = FileTypeDetector.DetectFileType(stream); var directories = new List <Directory>(); directories.AddRange(fileType switch { FileType.Arw => TiffMetadataReader.ReadMetadata(stream), FileType.Avi => AviMetadataReader.ReadMetadata(stream), FileType.Bmp => BmpMetadataReader.ReadMetadata(stream), FileType.Crx => QuickTimeMetadataReader.ReadMetadata(stream), FileType.Cr2 => TiffMetadataReader.ReadMetadata(stream), FileType.Eps => EpsMetadataReader.ReadMetadata(stream), FileType.Gif => GifMetadataReader.ReadMetadata(stream), FileType.Ico => IcoMetadataReader.ReadMetadata(stream), FileType.Jpeg => JpegMetadataReader.ReadMetadata(stream), FileType.Mp3 => Mp3MetadataReader.ReadMetadata(stream), FileType.Nef => TiffMetadataReader.ReadMetadata(stream), FileType.Netpbm => new Directory[] { NetpbmMetadataReader.ReadMetadata(stream) }, FileType.Orf => TiffMetadataReader.ReadMetadata(stream), FileType.Pcx => new Directory[] { PcxMetadataReader.ReadMetadata(stream) }, FileType.Png => PngMetadataReader.ReadMetadata(stream), FileType.Psd => PsdMetadataReader.ReadMetadata(stream), FileType.QuickTime => QuickTimeMetadataReader.ReadMetadata(stream), FileType.Raf => RafMetadataReader.ReadMetadata(stream), FileType.Rw2 => TiffMetadataReader.ReadMetadata(stream), FileType.Tga => TgaMetadataReader.ReadMetadata(stream), FileType.Tiff => TiffMetadataReader.ReadMetadata(stream), FileType.Wav => WavMetadataReader.ReadMetadata(stream), FileType.WebP => WebPMetadataReader.ReadMetadata(stream), FileType.Unknown => throw new ImageProcessingException("File format could not be determined"), _ => Enumerable.Empty <Directory>() });
public virtual Task <Size> GetImageSize(byte[]?bytes) { return(Task.Run(() => { if (bytes == null) { return Size.Empty; } try { using (var stream = new MemoryStream(bytes)) { var type = FileTypeDetector.DetectFileType(stream); stream.Seek(0, SeekOrigin.Begin); switch (type) { case FileType.Jpeg: var jpeg = JpegMetadataReader.ReadMetadata(stream); var jpegdir = jpeg.OfType <JpegDirectory>().FirstOrDefault(); return new Size(jpegdir.GetImageWidth(), jpegdir.GetImageHeight()); case FileType.Png: var png = PngMetadataReader.ReadMetadata(stream); var pngdir = png.OfType <PngDirectory>().FirstOrDefault(); return new Size(pngdir.GetInt32(PngDirectory.TagImageWidth), pngdir.GetInt32(PngDirectory.TagImageHeight)); case FileType.Gif: var gif = GifMetadataReader.ReadMetadata(stream); var gifdir = gif.OfType <GifHeaderDirectory>().FirstOrDefault(); return new Size(gifdir.GetInt32(GifHeaderDirectory.TagImageWidth), gifdir.GetInt32(GifHeaderDirectory.TagImageHeight)); case FileType.WebP: var webp = WebPMetadataReader.ReadMetadata(stream); var webpdir = webp.OfType <WebPDirectory>().FirstOrDefault(); return new Size(webpdir.GetInt32(WebPDirectory.TagImageWidth), webpdir.GetInt32(WebPDirectory.TagImageHeight)); case FileType.Bmp: var bmp = BmpMetadataReader.ReadMetadata(stream); var bmpdir = bmp.OfType <BmpHeaderDirectory>().FirstOrDefault(); return new Size(bmpdir.GetInt32(BmpHeaderDirectory.TagImageWidth), bmpdir.GetInt32(BmpHeaderDirectory.TagImageHeight)); default: return Size.Empty; } } } catch (Exception) { //Crashes.TrackError(e); // No need to track this } return Size.Empty; })); }
ReadMetadata([NotNull] Stream stream) { var fileType = FileTypeDetector.DetectFileType(stream); switch (fileType) { case FileType.Jpeg: return(JpegMetadataReader.ReadMetadata(stream)); case FileType.Tiff: case FileType.Arw: case FileType.Cr2: case FileType.Nef: case FileType.Orf: case FileType.Rw2: return(TiffMetadataReader.ReadMetadata(stream)); case FileType.Psd: return(PsdMetadataReader.ReadMetadata(stream)); case FileType.Png: return(PngMetadataReader.ReadMetadata(stream)); case FileType.Bmp: return(new Directory[] { BmpMetadataReader.ReadMetadata(stream) }); case FileType.Gif: return(GifMetadataReader.ReadMetadata(stream)); case FileType.Ico: return(IcoMetadataReader.ReadMetadata(stream)); case FileType.Pcx: return(new Directory[] { PcxMetadataReader.ReadMetadata(stream) }); case FileType.Riff: return(WebPMetadataReader.ReadMetadata(stream)); case FileType.Raf: return(RafMetadataReader.ReadMetadata(stream)); case FileType.QuickTime: return(QuickTimeMetadataReader.ReadMetadata(stream)); case FileType.Netpbm: return(new Directory[] { NetpbmMetadataReader.ReadMetadata(stream) }); } throw new ImageProcessingException("File format is not supported"); }
/// <summary>Reads metadata from an <see cref="Stream"/>.</summary> /// <param name="stream">A stream from which the file data may be read. The stream must be positioned at the beginning of the file's data.</param> /// <returns>A list of <see cref="Directory"/> instances containing the various types of metadata found within the file's data.</returns> /// <exception cref="ImageProcessingException">The file type is unknown, or processing errors occurred.</exception> /// <exception cref="System.IO.IOException"/> public static DirectoryList ReadMetadata(Stream stream) { var fileType = FileTypeDetector.DetectFileType(stream); var fileTypeDirectory = new FileTypeDirectory(fileType); switch (fileType) { case FileType.Jpeg: return(Append(JpegMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Tiff: case FileType.Arw: case FileType.Cr2: case FileType.Nef: case FileType.Orf: case FileType.Rw2: return(Append(TiffMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Psd: return(Append(PsdMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Png: return(Append(PngMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Bmp: return(Append(BmpMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Gif: return(Append(GifMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Ico: return(Append(IcoMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Pcx: return(new Directory[] { PcxMetadataReader.ReadMetadata(stream), fileTypeDirectory }); case FileType.WebP: return(Append(WebPMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Avi: return(Append(AviMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Wav: return(Append(WavMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Raf: return(Append(RafMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.QuickTime: case FileType.Crx: return(Append(QuickTimeMetadataReader.ReadMetadata(stream), fileTypeDirectory)); case FileType.Netpbm: return(new Directory[] { NetpbmMetadataReader.ReadMetadata(stream), fileTypeDirectory }); case FileType.Unknown: throw new ImageProcessingException("File format could not be determined"); case FileType.Riff: case FileType.Crw: default: throw new ImageProcessingException("File format is not supported"); }
protected override async Task <Metadata> ReadMetadata( Metadata metadata, MetadataReaderFileInfo fileInfo, MetadataReaderOption option) { var directories = await _fileService.ReadFileStream(fileInfo.FileHandle, readStream => { if (fileInfo.MimeType == MimeType.image_png) { return(ValueTask.FromResult(PngMetadataReader.ReadMetadata(readStream))); } if (fileInfo.MimeType == MimeType.image_jpeg) { return(ValueTask.FromResult(JpegMetadataReader.ReadMetadata(readStream))); } if (fileInfo.MimeType == MimeType.image_bmp) { return(ValueTask.FromResult(BmpMetadataReader.ReadMetadata(readStream))); } if (fileInfo.MimeType == MimeType.image_gif) { return(ValueTask.FromResult(GifMetadataReader.ReadMetadata(readStream))); } if (fileInfo.MimeType == MimeType.image_webp) { return(ValueTask.FromResult(WebPMetadataReader.ReadMetadata(readStream))); } return(ValueTask.FromResult(MetadataExtractor.ImageMetadataReader.ReadMetadata(readStream))); }); foreach (var directory in directories) { if (directory is JpegDirectory jpegDirectory) { ParseJpegDirectory(jpegDirectory, metadata); } else if (directory is WebPDirectory webPDirectory) { ParseWebPDirectory(webPDirectory, metadata); } else if (directory is ExifIfd0Directory exifIfd0Directory) { ParseExifDirectory(exifIfd0Directory, metadata); } else if (directory is ExifSubIfdDirectory exifSubIfdDirectory) { ParseExifDirectory(exifSubIfdDirectory, metadata); } else if (directory is PngDirectory pngDirectory) { ParsePngDirectory(pngDirectory, metadata); } else if (directory is XmpDirectory xmpDirectory) { ParseXmpDirectory(xmpDirectory, metadata); } else if (directory is CanonMakernoteDirectory canonMakernoteDirectory) { ParseCanonMakernoteDirectory(canonMakernoteDirectory, metadata); } } return(metadata); }