Reputation: 3536
I have distorted image if actual image's size is larger than control's property. For example: image is 800 x 600, control is only 200 x 200.
System.Windows.Controls.Image im = new System.Windows.Controls.Image();
im.Stretch = Stretch.None ;
im.Width = 200;
im.Height = 200;
im.Margin = new Thickness(5, 5, 5, 5);
im.Source = loadImageFromArray(m_imgs[f]);
private BitmapSource loadImageFromArray(Byte[] imageData)
{
using (MemoryStream ms = new MemoryStream(imageData))
{
var decoder = BitmapDecoder.Create(ms,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnDemand);
return decoder.Frames[0];
}
}
Tried to play with Image.Stretch but none of it's values doesn't give proper display. I want control to resize image without cropping and display it. Images are in jpeg format.
VS2013, WPF, netfw 4.0.
UPD: sorry guys, but I found the answer. Problem contains in BitmapCacheOptions. With BitmapCacheOption.OnLoad all works great. Don't know why OnDemand works properly only with small images. Will post is as answer.
Upvotes: 1
Views: 438
Reputation: 3536
The problem was in cache option. As Clemens pointed out, we need to choose OnLoad
cache option if you wish to close the bitmapStream
after the bitmap is created.
Upvotes: 1
Reputation: 222700
When you set Uniform
The content is resized to fit in the destination dimensions while it preserves its native aspect ratio.
System.Windows.Controls.Image im = new System.Windows.Controls.Image();
im.Stretch = Stretch.Uniform;
im.MaxHeight = 200;
im.MaxWidth = 200;
Upvotes: 0