Dynamic loading of images in WPF Dynamic loading of images in WPF wpf wpf

Dynamic loading of images in WPF


It is because the Creation was delayed.If you want the picture to be loaded immediately, you can simply add this code into the init phase.

src.CacheOption = BitmapCacheOption.OnLoad;

like this:

src.BeginInit();src.UriSource = new Uri("picture.jpg", UriKind.Relative);src.CacheOption = BitmapCacheOption.OnLoad;src.EndInit();


In code to load resource in the executing assembly where my image 'Freq.png' was in the folder "Icons" and defined as "Resource".

        this.Icon = new BitmapImage(new Uri(@"pack://application:,,,/"              + Assembly.GetExecutingAssembly().GetName().Name              + ";component/"              + "Icons/Freq.png", UriKind.Absolute)); 

I also made a function if anybody would like it...

/// <summary>/// Load a resource WPF-BitmapImage (png, bmp, ...) from embedded resource defined as 'Resource' not as 'Embedded resource'./// </summary>/// <param name="pathInApplication">Path without starting slash</param>/// <param name="assembly">Usually 'Assembly.GetExecutingAssembly()'. If not mentionned, I will use the calling assembly</param>/// <returns></returns>public static BitmapImage LoadBitmapFromResource(string pathInApplication, Assembly assembly = null){    if (assembly == null)    {        assembly = Assembly.GetCallingAssembly();    }    if (pathInApplication[0] == '/')    {        pathInApplication = pathInApplication.Substring(1);    }    return new BitmapImage(new Uri(@"pack://application:,,,/" + assembly.GetName().Name + ";component/" + pathInApplication, UriKind.Absolute)); }

Usage:

        this.Icon = ResourceHelper.LoadBitmapFromResource("Icons/Freq.png");


This is strange behavior and although I am unable to say why this is occurring, I can recommend some options.

First, an observation. If you include the image as Content in VS and copy it to the output directory, your code works. If the image is marked as None in VS and you copy it over, it doesn't work.

Solution 1: FileStream

The BitmapImage object accepts a UriSource or StreamSource as a parameter. Let's use StreamSource instead.

        FileStream stream = new FileStream("picture.png", FileMode.Open, FileAccess.Read);        Image i = new Image();        BitmapImage src = new BitmapImage();        src.BeginInit();        src.StreamSource = stream;        src.EndInit();        i.Source = src;        i.Stretch = Stretch.Uniform;        panel.Children.Add(i);

The problem: stream stays open. If you close it at the end of this method, the image will not show up. This means that the file stays write-locked on the system.

Solution 2: MemoryStream

This is basically solution 1 but you read the file into a memory stream and pass that memory stream as the argument.

        MemoryStream ms = new MemoryStream();        FileStream stream = new FileStream("picture.png", FileMode.Open, FileAccess.Read);        ms.SetLength(stream.Length);        stream.Read(ms.GetBuffer(), 0, (int)stream.Length);        ms.Flush();        stream.Close();        Image i = new Image();        BitmapImage src = new BitmapImage();        src.BeginInit();        src.StreamSource = ms;        src.EndInit();        i.Source = src;        i.Stretch = Stretch.Uniform;        panel.Children.Add(i);

Now you are able to modify the file on the system, if that is something you require.