diff --git a/WordsLive/Images/ImagesPresentation.cs b/WordsLive/Images/ImagesPresentation.cs index 69744c08..383b923a 100644 --- a/WordsLive/Images/ImagesPresentation.cs +++ b/WordsLive/Images/ImagesPresentation.cs @@ -51,7 +51,9 @@ public ImagesPresentation() this.Control.Children.Add(frontGrid); Loader.SetDisplayOption(back, DisplayOptions.FullResolution); + Loader.SetMaxSize(back, Math.Max(Area.Width, Area.Height)); Loader.AddLoadedHandler(back, back_Loaded); + this.Area.WindowSizeChanged += Area_WindowSizeChanged; DoubleAnimation ani = new DoubleAnimation { From = 1.0, To = 0.0, FillBehavior = FillBehavior.Stop }; ani.Completed += ani_Completed; @@ -62,6 +64,11 @@ public ImagesPresentation() storyboard.Children.Add(ani); } + private void Area_WindowSizeChanged(object sender, EventArgs e) + { + Loader.SetMaxSize(back, Math.Max(Area.Width, Area.Height)); + } + void ani_Completed(object sender, EventArgs e) { front.Source = back.Source; @@ -121,5 +128,11 @@ private void Update(ImageInfo image) Loader.SetSourceType(back, image.SourceType); Loader.SetSource(back, image.Source); } + + public override void Close() + { + this.Area.WindowSizeChanged -= Area_WindowSizeChanged; + base.Close(); + } } } diff --git a/WordsLive/Utils/ImageLoader/Loader.cs b/WordsLive/Utils/ImageLoader/Loader.cs index 9c78037b..c2785b07 100644 --- a/WordsLive/Utils/ImageLoader/Loader.cs +++ b/WordsLive/Utils/ImageLoader/Loader.cs @@ -104,6 +104,20 @@ public static void SetDisplayOption(Image obj, DisplayOptions value) DependencyProperty.RegisterAttached("DisplayOption", typeof(DisplayOptions), typeof(Loader), new UIPropertyMetadata(DisplayOptions.Preview)); + [AttachedPropertyBrowsableForType(typeof(Image))] + public static int GetMaxSize(Image obj) + { + return (int)obj.GetValue(MaxSizeProperty); + } + + public static void SetMaxSize(Image obj, int value) + { + obj.SetValue(MaxSizeProperty, value); + } + public static readonly DependencyProperty MaxSizeProperty = + DependencyProperty.RegisterAttached("MaxSize", typeof(int), typeof(Loader), new UIPropertyMetadata(0)); + + [AttachedPropertyBrowsableForType(typeof(Image))] public static bool GetIsLoading(Image obj) diff --git a/WordsLive/Utils/ImageLoader/Manager.cs b/WordsLive/Utils/ImageLoader/Manager.cs index 28178519..95e9155d 100644 --- a/WordsLive/Utils/ImageLoader/Manager.cs +++ b/WordsLive/Utils/ImageLoader/Manager.cs @@ -264,10 +264,12 @@ private ImageSource GetBitmapSource(LoadImageRequest loadTask, DisplayOptions lo Stream imageStream = null; SourceType sourceType = SourceType.LocalDisk; + int maxSize = 0; image.Dispatcher.Invoke(new ThreadStart(delegate { sourceType = Loader.GetSourceType(image); + maxSize = Loader.GetMaxSize(image); })); try @@ -366,12 +368,25 @@ private ImageSource GetBitmapSource(LoadImageRequest loadTask, DisplayOptions lo { BitmapFrame bitmapFrame = BitmapFrame.Create(imageStream); int rotation = GetRotation(bitmapFrame.Metadata as BitmapMetadata); + bool isOrientationChanged = rotation != 0 && rotation != 180; + + double usedFactor = 1.0; + + if (maxSize != 0) + { + var origHeight = isOrientationChanged ? bitmapFrame.PixelWidth : bitmapFrame.PixelHeight; + var origWidth = isOrientationChanged ? bitmapFrame.PixelHeight : bitmapFrame.PixelWidth; + var heightFactor = maxSize / (double)origHeight; + var widthFactor = maxSize / (double)origWidth; + usedFactor = Math.Max(heightFactor, widthFactor); + } TransformedBitmap bitmapImage = new TransformedBitmap(); bitmapImage.BeginInit(); bitmapImage.Source = bitmapFrame as BitmapSource; TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(new RotateTransform(rotation)); + transformGroup.Children.Add(new ScaleTransform(usedFactor, usedFactor)); bitmapImage.Transform = transformGroup; bitmapImage.EndInit(); @@ -422,6 +437,12 @@ private ImageSource GetBitmapSource(LoadImageRequest loadTask, DisplayOptions lo player.Close(); } } + catch (System.OutOfMemoryException) + { + // for big images we sometimes run out of memory, + // but a manual GC seems to help ... + GC.Collect(); + } catch (Exception) { } }