I see a weird behavior of an image.
I have two methods wich calculate a heatmap image. one method works in the GUI thread, and the other one in a background thread. Both methods should be the same and the output as an png seems to be good. The GUI thread created image is able to loaded in, but the created one from the background thread not.
I save the calculated image as file on my hard drive and load it in again. When I load it and assign it to the image source it is not null. But when the programm leaves the allocating method the image.source is null. I don't understand why.
This is the code:
private void LoadImageClicked(object sender, RoutedEventArgs e)
{
this.ImageHeatMap.BeginInit();
this.ImageHeatMap.Source = new BitmapImage(new Uri(@"HeatMapImage.png", UriKind.Relative));
this.ImageHeatMap.EndInit();
}
and this his how I save the image as png:
public void SaveFile(BitmapSource source, string filePath)
{
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(fileStream);
}
}
Interesting is that I can not view the Image.Source.Height property in debug mode. but with the working file I am able to read this property. Debug looks like this:
Error message in english (free translated): "this.Image:heatMap.Source.Height" has thrown an exception of type "System.IO.IOException"
Is somebody able to help me understanding where is the problem? Because I don't understand why I can't load the image. I am able to open it via the Windows picture viewer.
I think the problem is the metadata but I don't know - at the moment - how to fix it.