示例#1
0
文件: Image.cs 项目: lpdink/IViewer
        public static Image FromFile(string path)
        {
            var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            var image  = new Image();

            try {
                var format = FileTypeDetector.DetectFileType(stream);
                stream.Seek(0, SeekOrigin.Begin);
                image.Metadata = new Metadata(path);
                stream.Seek(0, SeekOrigin.Begin);
                byte[] data;

                switch (format)
                {
                case FileType.Bmp:
                    data = new byte[stream.Length];
                    stream.Read(data, 0, data.Length);
                    image.source = BmpDecoder.FromBytes(data);
                    break;

                case FileType.WebP:
                    data = new byte[stream.Length];
                    stream.Read(data, 0, data.Length);
                    image.source = WebpDecoder.FromBytes(data);
                    break;

                case FileType.QuickTime:
                    var qtFt = image.Metadata.Directories
                               .OfType <QuickTimeFileTypeDirectory>()
                               .FirstOrDefault()
                               ?.GetDescription(1);
                    if (qtFt != "avif" && qtFt != "avis" && qtFt != "av01")
                    {
                        return(null);
                    }

                    data = new byte[stream.Length];
                    stream.Read(data, 0, data.Length);
                    image.source = AvifDecoder.FromBytes(data);
                    break;

                case FileType.Heif:
                    data = new byte[stream.Length];
                    stream.Read(data, 0, data.Length);
                    image.source = HeifDecoder.FromBytes(data);
                    break;

                case FileType.Jpeg:
                case FileType.Tiff:
                case FileType.Png:
                case FileType.Gif:
                case FileType.Ico:
                    image.source = WpfDecoder.FromStream(stream);
                    break;

                case FileType.Unknown:
                    if (stream.Length < 4)
                    {
                        return(null);
                    }

                    var header = new byte[4];
                    stream.Read(header, 0, 4);
                    if (FlifDecoder.MagicDetect(header) == DetectResult.Yes)
                    {
                        data = new byte[stream.Length];
                        stream.Seek(0, SeekOrigin.Begin);
                        stream.Read(data, 0, data.Length);
                        image.source = FlifDecoder.FromBytes(data);
                    }

                    break;

                default:
                    return(null);
                }

                return(image.source != null ? image : null);
            }
            catch (Exception) {
                return(null);
            }
            finally {
                stream.Dispose();
            }
        }
示例#2
0
        MyExifData get_exif_data(string str_file)
        {
            MyExifData myExifData;

            myExifData.bSuccessExifData = false;
            myExifData.strFileExt       = null;
            myExifData.bImage           = false;
            myExifData.bVideo           = false;
            myExifData.dtDate           = new DateTime();
            myExifData.bLoadFileFail    = true;
            FileStream myStream = null;

            try
            {
                myStream = new FileStream(str_file, FileMode.Open);
                FileType fileType = FileTypeDetector.DetectFileType(myStream);
                myStream.Close();

                IEnumerable <MetadataExtractor.Directory> directories;
                directories = ImageMetadataReader.ReadMetadata(str_file);
                DateTime dtTime;

                myExifData.bLoadFileFail = false;

                if (fileType == FileType.Jpeg || fileType == FileType.Png || fileType == FileType.Bmp)
                {
                    var subIfdDirectory = directories.OfType <ExifSubIfdDirectory>().FirstOrDefault();
                    var dateTime        = subIfdDirectory?.GetDescription(ExifDirectoryBase.TagDateTimeOriginal);

                    if (dateTime != null)
                    {
                        dtTime = ConvertDateExif(dateTime);
                        myExifData.bSuccessExifData = true;
                        myExifData.strFileExt       = Path.GetExtension(str_file);
                        myExifData.bImage           = true;
                        myExifData.bVideo           = false;
                        myExifData.dtDate           = dtTime;
                    }
                }
                else if (fileType == FileType.QuickTime)//동영상일 경우
                {
                    bool bUseVideoFile = UseVideoFile(str_file);
                    if (bUseVideoFile)
                    {
                        var subIfdDirectory = directories.OfType <QuickTimeMovieHeaderDirectory>().FirstOrDefault();
                        var dateTime        = subIfdDirectory?.GetDescription(QuickTimeMovieHeaderDirectory.TagCreated);
                        if (dateTime != null)
                        {
                            dtTime = ConvertDateVideo(dateTime);
                            myExifData.bSuccessExifData = true;
                            myExifData.strFileExt       = Path.GetExtension(str_file);
                            myExifData.bImage           = false;
                            myExifData.bVideo           = true;
                            myExifData.dtDate           = dtTime;
                        }
                    }
                }
            }
            catch (MetadataExtractor.ImageProcessingException e)
            {
                //WriteLog("Exception-" + str_file);
                string strMsg = string.Format("Exception-{0}", str_file);
                m_queueLogMsg.Enqueue(strMsg);
                return(myExifData);
            }
            catch (System.UnauthorizedAccessException e)
            {
                //WriteLog("Exception-" + str_file);
                if (myStream != null)
                {
                    myStream.Close();
                }


                string strMsg = string.Format("Exception-{0}", str_file);
                m_queueLogMsg.Enqueue(strMsg);
                return(myExifData);
            }
            catch (System.IO.IOException e)
            {
                //WriteLog("Exception-" + str_file);
                string strMsg = string.Format("Exception-{0}", str_file);
                m_queueLogMsg.Enqueue(strMsg);
                return(myExifData);
            }

            return(myExifData);
        }
        public static DirectoryList ReadMetadata([NotNull] 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(new Directory[] { 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.Riff:
                return(Append(WebPMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.Raf:
                return(Append(RafMetadataReader.ReadMetadata(stream), fileTypeDirectory));

            case FileType.QuickTime:
                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.Crw:
            default:
                throw new ImageProcessingException("File format is not supported");
            }

            DirectoryList Append(IEnumerable <Directory> list, Directory directory)
            => new List <Directory>(list)
            {
                directory
            };
        }
示例#4
0
        /// <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.Eps:
                return(Append(EpsMetadataReader.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.Tga:
                return(Append(TgaMetadataReader.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");
            }