1

On a WPF project, I need to set a ImageSource property from a local Resource. I am trying to use the following code:

   var bmp = new BitmapImage();
   bmp.BeginInit();
   bmp.UriSource = new Uri("pack://application:,,,/Resources/Identicons/no_user.jpg", UriKind.Absolute);
   bmp.EndInit();
   Avatar = bmp;

Where Avatar is defined before as:

private ImageSource myAvatar;

The problem is that BitmapImage does not support MetaData information, that the source image have (it was created with Paint.net) so it throws a error. The error is the following:

Metadata = 'bmp.Metadata' threw an exception of type 'System.NotSupportedException'

So I believe that I need alternatives to BitmapImage to properly load the desired image.

As a end note, using the same image inside xaml, directly in a "source" property it works fine. Thank you

Domvs Phi
  • 41
  • 1
  • 6

3 Answers3

1

Unlike BitmapImage, BitmapFrame supports the Metadata property:

So you may replace

Avatar = new BitmapImage(new Uri(...));

by

Avatar = BitmapFrame.Create(new Uri(...));

From MSDN:

BitmapFrame provides additional functionality not defined by BitmapSource ... BitmapFrame also supports the writing of metadata information by using the Metadata property or the CreateInPlaceBitmapMetadataWriter method.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Although this seams the correct answer, the return is null (no exception is shown), and I have sure that the image is available. Using: Avatar = BitmapFrame.Create(new Uri("pack://application:,,,/Resources/Identicons/no_user.jpg", UriKind.Absolute)); – Domvs Phi Jul 29 '15 at 14:57
  • Besides that the UriKind parameter isn't necessary, did you make sure that the image file is part of your Visual Studio project, as mentioned in my comment to your question? – Clemens Jul 29 '15 at 15:56
  • Yes it is. Is is located as a Resource. I even changed the name to another (wrong name) and a error is thrown about the missing file in that case. – Domvs Phi Jul 29 '15 at 16:00
0

As you have the path to the image it can be done by below using Image Control,

XAML

<Image>
    <Image.Source>
        <BitmapImage UriSource="{Binding ImagePath}"></BitmapImage>
    </Image.Source>
</Image>

VM

public ViewerViewModel()// Constructor 
    {
        ImagePath = @"..\Images\Untitled.jpg";// Change the image path based on your input.
    }
    private string _ImagePath = string.Empty;
    public string ImagePath {
        get { return _ImagePath; }
        set { _ImagePath = value; NotifyPropertyChanged(); }
    }
Abin
  • 2,868
  • 1
  • 23
  • 59
  • 1
    I am adding the ImageSource into a property in a Class, so not Image Control available. Thank you for trying to help. – Domvs Phi Jul 29 '15 at 15:03
0

Problem solved. This was inside a Class with the property:

    private ImageSource myAvatar;
....

    public ImageSource Avatar
    {
        set { myAvatar = Avatar; }
        get { return myAvatar; }
    }

And I was trying to change the Avatar (that sets the myAvatar through the set). I do not understand why, but changing directly the myAvatar works. For example doing:

BitmapSource image = BitmapFrame.Create(new Uri("pack://application:,,,/Resources/Identicons/no_user.jpg", UriKind.Absolute));
myAvatar = image;

Is ok, but:

BitmapSource image = BitmapFrame.Create(new Uri("pack://application:,,,/Resources/Identicons/no_user.jpg", UriKind.Absolute));
Avatar = image;

Always sets Avatar as null. This is a method inside a class. I would appreciate to understand why as it is not clear to me. Thank you.

Domvs Phi
  • 41
  • 1
  • 6
  • 1
    The property setter should be `set { myAvatar = value; }`. Otherwise the property is always set to the value it already has, i.e. `null`. – Clemens Jul 29 '15 at 16:54
  • 1
    Correct, and the same Class have more than 10 'set { something = value; }'. Do not know how I missed this one, and I though was related with the image source... Thank you. – Domvs Phi Jul 29 '15 at 17:30