I want the image which is added to FlowDocument is loaded as a bitmap data when this image is visible by page change or scrolling.(not IsVisible property)
because My senario is that images(png, jpg...) are in a zip file. and I will load zip file to memory and decompress it to memory stream. So, this memory stream has png, jpg binary(low size). and I change this png binary data to BitmapImage class. This bitmapImage class is added to flowDocument.
The problem is zip file has many image files and after i changed the images to BitmapImage classes, it takes so many Memory Size.
So, I want to change it to below. 1. Save decompressed image(png, jpg...) data to MemoryStream. 2. Add this images without changing it to BitmapClasses. 3. Dynamically change images data to BitmapClasses when the image should be displayed by chainging page or scrolling. However, I cannot find the way of number 2 above. I tried Display Image from Byte Array in WPF - Memory Issues but it is different situation.
How Can I load image dynamically when the image is visible at FlowDocument?
You can try this way. 1. extend Image Class and Add IsVisibleChanged event handler. 2. The class has memory stream variable. 3. when you add images, just add memory stream except Source. 4. when IsVisibleChanged handler is called, add memory stream to source.
This is my sample code.
public class sampleImage : Image
{
public MemoryStream memoryStream = null;
public sampleImage () : base()
{
IsVisibleChanged += new DependencyPropertyChangedEventHandler(shandler);
}
void shandler(object sender, DependencyPropertyChangedEventArgs e)
{
if (IsVisible)
{
memoryStream.Position = 0;
var bitmap = BitmapFrame.Create(memoryStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
Source = bitmap;
} else {
Source = null;
GC.Collect(); // it depends on you.
}
}
}