Skip to content

Commit

Permalink
Limit the size of full size images to the presentation area
Browse files Browse the repository at this point in the history
Otherwise we can run out of memory ...
  • Loading branch information
Boddlnagg committed Mar 30, 2024
1 parent e74390e commit 2a7fc30
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions WordsLive/Images/ImagesPresentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
14 changes: 14 additions & 0 deletions WordsLive/Utils/ImageLoader/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions WordsLive/Utils/ImageLoader/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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) { }
}

Expand Down

0 comments on commit 2a7fc30

Please sign in to comment.