/// <summary> /// 引数PATHをサムネイルとして読み出す /// </summary> /// <param name="imagePath">ファイルパス</param> /// <param name="width">サムネイルの画像幅</param> /// <returns></returns> public static BitmapSource LoadThumbnail(this string imagePath, int width = DefaultThumbnailWidth) { if (imagePath is null) { throw new ArgumentNullException(nameof(imagePath)); } if (!File.Exists(imagePath)) { throw new FileNotFoundException(imagePath); } using (var stream = File.Open(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { BitmapSource bitmapSource = null; var img = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None); if (img.Thumbnail != null) { bitmapSource = img.Thumbnail; // サムネイル読み出し高速化 } else { bitmapSource = img as BitmapSource; } var longSide = Math.Max(bitmapSource.PixelWidth, bitmapSource.PixelHeight); var scale = width / (double)longSide; var thumbnail = new TransformedBitmap(bitmapSource, new ScaleTransform(scale, scale)); var cachedBitmap = new CachedBitmap(thumbnail, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); cachedBitmap.Freeze(); return((BitmapSource)cachedBitmap); // アップキャストは常に合法 } }
private static BitmapSource ClearArea(BitmapSource frame, FrameMetadata metadata) { DrawingVisual visual = new DrawingVisual(); using (var context = visual.RenderOpen()) { var fullRect = new Rect(0, 0, frame.PixelWidth, frame.PixelHeight); var clearRect = new Rect(metadata.Left, metadata.Top, metadata.Width, metadata.Height); var clip = Geometry.Combine( new RectangleGeometry(fullRect), new RectangleGeometry(clearRect), GeometryCombineMode.Exclude, null); context.PushClip(clip); context.DrawImage(frame, fullRect); } var bitmap = new RenderTargetBitmap( frame.PixelWidth, frame.PixelHeight, frame.DpiX, frame.DpiY, PixelFormats.Pbgra32); bitmap.Render(visual); var result = new CachedBitmap(bitmap, BitmapCreateOptions.None, BitmapCacheOption.Default); if (result.CanFreeze && !result.IsFrozen) { result.Freeze(); } return(result); }
/// <summary> /// 画像のStreamから埋め込みサムネイル優先でBitmapSourceを作成する /// </summary> /// <param name="stream"></param> /// <param name="widthMax">埋め込みサムネイルが存在しなかった場合に、主画像をリサイズする最大幅</param> /// <returns></returns> private static BitmapSource ToBitmapSourceFromEmbeddedThumbnail(this Stream stream, double widthMax) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } var image = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None); BitmapSource bitmap; if (image.Thumbnail != null) { bitmap = image.Thumbnail; } else { var longSide = Math.Max(image.PixelWidth, image.PixelHeight); if (longSide == 0) { throw new DivideByZeroException(nameof(longSide)); } var scale = widthMax / (double)longSide; var thumbnail = new TransformedBitmap(image, new ScaleTransform(scale, scale)); var cache = new CachedBitmap(thumbnail, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); cache.Freeze(); bitmap = (BitmapSource)cache; // アップキャストは常に合法 } return(bitmap); }
public System.Windows.Media.Imaging.BitmapSource GetThumbnail(int width, int height) { BitmapSource thumb = null; // Try to read the thumbnail. if (Frame.Thumbnail != null) { try { double scale; scale = (Math.Min(width / (double)Frame.Thumbnail.PixelWidth, height / (double)Frame.Thumbnail.PixelHeight)); thumb = Scale(Frame.Thumbnail, scale); } catch { if (thumb != null) { thumb = null; } } } // Try to read the preview. if (thumb == null && Frame.Decoder?.Preview != null) { try { double scale; var preview = Frame.Decoder.Preview; scale = (Math.Min(width / (double)preview.PixelWidth, height / (double)preview.PixelHeight)); thumb = Scale(preview, scale); } catch { if (thumb != null) { thumb = null; } } } if (thumb == null) { double scale; scale = (Math.Min(width / (double)Frame.PixelWidth, height / (double)Frame.PixelHeight)); thumb = Scale(Frame, scale); } var retval = new CachedBitmap(thumb, BitmapCreateOptions.None, BitmapCacheOption.Default); retval.Freeze(); return(retval); }
/// <summary> /// 引数PATHをサムネイルとして読み出す /// </summary> /// <param name="imagePath">ファイルパス</param> /// <param name="widthMax">サムネイルの画像幅</param> /// <returns></returns> public static BitmapSource?ToBitmapSourceThumbnail(this string imagePath, double widthMax) { if (!File.Exists(imagePath)) { return(null); //throw new FileNotFoundException(imagePath); } using var stream = File.Open(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read); var img = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None); var bitmapSource = img.Thumbnail ?? (img as BitmapSource); var longSide = Math.Max(bitmapSource.PixelWidth, bitmapSource.PixelHeight); var scale = widthMax / (double)longSide; var thumbnail = new TransformedBitmap(bitmapSource, new ScaleTransform(scale, scale)); var cachedBitmap = new CachedBitmap(thumbnail, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); cachedBitmap.Freeze(); return((BitmapSource)cachedBitmap); // アップキャストは常に合法 }
private static BitmapSource MakeFrame( Int32Size fullSize, BitmapSource rawFrame, FrameMetadata metadata, BitmapSource baseFrame) { if (baseFrame == null && IsFullFrame(metadata, fullSize)) { // No previous image to combine with, and same size as the full image // Just return the frame as is return(rawFrame); } DrawingVisual visual = new DrawingVisual(); using (var context = visual.RenderOpen()) { if (baseFrame != null) { var fullRect = new Rect(0, 0, fullSize.Width, fullSize.Height); context.DrawImage(baseFrame, fullRect); } var rect = new Rect(metadata.Left, metadata.Top, metadata.Width, metadata.Height); context.DrawImage(rawFrame, rect); } var bitmap = new RenderTargetBitmap( fullSize.Width, fullSize.Height, 96, 96, PixelFormats.Pbgra32); bitmap.Render(visual); var result = new CachedBitmap(bitmap, BitmapCreateOptions.None, BitmapCacheOption.Default); if (result.CanFreeze && !result.IsFrozen) { result.Freeze(); } return(result); }