예제 #1
0
        /// <summary>
        /// ファイルのタイプを確認する
        /// </summary>
        /// <param name="filePath">確認するファイルパス</param>
        /// <returns>メディアタイプ</returns>
        private static MediaType CheckMediaType(string filePath)
        {
            var extension = Path.GetExtension(filePath).ToLower();

            if (MediaChecker.CheckPictureExtensions(extension))
            {
                return(MediaType.Picture);
            }

            throw new FileFormatException();
        }
예제 #2
0
        /// <summary>
        /// 画像一覧に表示するサムネイル画像を作成する
        /// </summary>
        /// <param name="filePath">画像ファイルパス</param>
        /// <returns>BitmapSource</returns>
        public static BitmapSource CreatePictureThumbnailImage(string filePath)
        {
            const int LongSideLength = 100;

            if (MediaChecker.CheckRawFileExtension(Path.GetExtension(filePath).ToLower()))
            {
                return(DecodePicture(filePath, LongSideLength));
            }
            else
            {
                using var sourceStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                sourceStream.Seek(0, SeekOrigin.Begin);
                var bitmapFrame = BitmapFrame.Create(sourceStream);
                var metadata    = bitmapFrame.Metadata as BitmapMetadata;
                var thumbnail   = bitmapFrame.Thumbnail;

                if (thumbnail != null)
                {
                    // リサイズ処理
                    var scale = LongSideLength / (double)thumbnail.PixelWidth;
                    if (thumbnail.PixelWidth < thumbnail.PixelHeight)
                    {
                        scale = LongSideLength / (double)thumbnail.PixelHeight;
                    }
                    thumbnail = new TransformedBitmap(thumbnail, new ScaleTransform(scale, scale));

                    // リサイズ後に回転
                    thumbnail = RotateImage(metadata, thumbnail);

                    // リサイズ、回転した画像を書き出す
                    var decodeImage = new WriteableBitmap(thumbnail);
                    decodeImage.Freeze();

                    return(decodeImage);
                }
                else
                {
                    return(DecodePicture(filePath, LongSideLength));
                }
            }
        }
예제 #3
0
        /// <summary>
        /// 画像をデコードする
        /// </summary>
        /// <param name="filePath">画像ファイルパス</param>
        /// <param name="longSideLength">長辺の長さ(この長さにあわせて画像がリサイズされる)</param>
        /// <returns>BitmapSource</returns>
        private static BitmapSource DecodePicture(string filePath, int longSideLength)
        {
            if (MediaChecker.CheckRawFileExtension(Path.GetExtension(filePath).ToLower()))
            {
                if (NativeMethods.GetRawThumbnailImageData(filePath, longSideLength, out var imageData) != 0)
                {
                    throw new FileFormatException("File format is wrong.");
                }

                return(CreateBitmapSourceFromImageStruct(imageData));
            }
            else
            {
                if (NativeMethods.GetNormalThumbnailImageData(filePath, longSideLength, out var imageData) != 0)
                {
                    throw new FileFormatException("File format is wrong.");
                }

                return(CreateBitmapSourceFromImageStruct(imageData));
            }
        }
예제 #4
0
        /// <summary>
        /// ファイルからExif情報を取得してリストに設定する
        /// </summary>
        /// <param name="filePath">画像のファイルパス</param>
        /// <param name="exifDataList">読み込んだExif情報を設定するリスト</param>
        public static void SetExifDataFromFile(string filePath, ExifInfo[] exifDataList)
        {
            var extension         = Path.GetExtension(filePath);
            var fileExtensionType = MediaChecker.GetFileExtensionType(extension.ToLower());
            var directories       = GetMetadataDirectories(filePath, fileExtensionType).ToArray();
            var subIfdDirectories = directories.OfType <ExifSubIfdDirectory>().ToArray();

            foreach (var exifInfo in exifDataList)
            {
                switch (exifInfo.ExifPropertyType)
                {
                case PropertyType.FileName:
                    exifInfo.ExifParameterValue = Path.GetFileName(filePath);
                    break;

                case PropertyType.Date:
                    exifInfo.ExifParameterValue = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagDateTime);
                    break;

                case PropertyType.CameraModel:
                    exifInfo.ExifParameterValue = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagModel);
                    break;

                case PropertyType.CameraManufacturer:
                    exifInfo.ExifParameterValue = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagMake);
                    break;

                case PropertyType.ImageWidth:
                    exifInfo.ExifParameterValue = MediaChecker.CheckRawFileExtension(extension.ToLower())
                            ? $"{GetExifDataFromMetadata(subIfdDirectories, ExifDirectoryBase.TagImageWidth)} pixel"
                            : $"{GetExifDataFromMetadata(directories, ExifDirectoryBase.TagExifImageWidth)} pixel";
                    break;

                case PropertyType.ImageHeight:
                    exifInfo.ExifParameterValue = MediaChecker.CheckRawFileExtension(extension.ToLower())
                            ? $"{GetExifDataFromMetadata(subIfdDirectories, ExifDirectoryBase.TagImageHeight)} pixel"
                            : $"{GetExifDataFromMetadata(directories, ExifDirectoryBase.TagExifImageHeight)} pixel";
                    break;

                case PropertyType.HorizonResolution:
                    var horizonResolution = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagXResolution);
                    exifInfo.ExifParameterValue = !string.IsNullOrEmpty(horizonResolution) ? $"{horizonResolution} dpi" : horizonResolution;
                    break;

                case PropertyType.VerticalResolution:
                    var verticalResolution = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagYResolution);
                    exifInfo.ExifParameterValue = !string.IsNullOrEmpty(verticalResolution) ? $"{verticalResolution} dpi" : verticalResolution;
                    break;

                case PropertyType.Bitdepth:
                    var bitDepth = fileExtensionType switch
                    {
                        FileExtensionType.Jpeg => GetExifDataFromMetadata(directories, JpegDirectory.TagDataPrecision),
                        FileExtensionType.Bmp => GetExifDataFromMetadata(directories, BmpHeaderDirectory.TagBitsPerPixel),
                        FileExtensionType.Png => GetExifDataFromMetadata(directories, PngDirectory.TagBitsPerSample),
                        FileExtensionType.Gif => GetExifDataFromMetadata(directories, GifHeaderDirectory.TagBitsPerPixel),
                        FileExtensionType.Tiff or FileExtensionType.Dng or FileExtensionType.Nef => GetExifDataFromMetadata(directories, ExifDirectoryBase.TagBitsPerSample).AsSpan(0, 1),
                        FileExtensionType.Unknown => throw new NotImplementedException(),
                              _ => throw new ArgumentOutOfRangeException(),
                    };
                    exifInfo.ExifParameterValue = bitDepth.ToString();
                    break;

                case PropertyType.ShutterSpeed:
                    var shutterSpeed = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagExposureTime);
                    exifInfo.ExifParameterValue = !string.IsNullOrEmpty(shutterSpeed) ? $"{shutterSpeed} sec" : shutterSpeed;
                    break;

                case PropertyType.Fnumber:
                    exifInfo.ExifParameterValue = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagFNumber);
                    break;

                case PropertyType.Iso:
                    exifInfo.ExifParameterValue = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagIsoEquivalent);
                    break;

                case PropertyType.FocalLength:
                    var focalLength = GetExifDataFromMetadata(directories, ExifDirectoryBase.Tag35MMFilmEquivFocalLength);
                    exifInfo.ExifParameterValue = !string.IsNullOrEmpty(focalLength) ? $"{focalLength} mm" : focalLength;
                    break;

                case PropertyType.ExposureProgram:
                    var exposureProgram = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagExposureProgram);
                    exifInfo.ExifParameterValue = !string.IsNullOrEmpty(exposureProgram) ? Enum.Parse <ExposureProgramType>(exposureProgram).ToString() : exposureProgram;
                    break;

                case PropertyType.WhiteBalance:
                    var whiteBalance = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagWhiteBalance);
                    exifInfo.ExifParameterValue = !string.IsNullOrEmpty(whiteBalance) ? Enum.Parse <WhiteBalanceType>(whiteBalance).ToString() : whiteBalance;
                    break;

                case PropertyType.MeteringMode:
                    var meteringMode = GetExifDataFromMetadata(directories, ExifDirectoryBase.TagMeteringMode);
                    exifInfo.ExifParameterValue = !string.IsNullOrEmpty(meteringMode) ? Enum.Parse <MeteringModeType>(meteringMode).ToString() : meteringMode;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }