0

Ok, there is countless from byte to String and etc, but i simply can't find anything on how i can convert the resource files visual studio so nicely convert into a byte[] for me no matter what i do, into a normal Image so i can actually use it in my C# code?

Tai Petersen
  • 19
  • 1
  • 2

2 Answers2

1

You can use a valueconverter to do this -

public class InMemoryImageValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var image = new BitmapImage();
        var memoryStream = new MemoryStream((byte[])value);
        image.SetSource(memoryStream);
        return image;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
        return null;
    }
}

adapted from https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/PictureChooser/Cirrious.MvvmCross.Plugins.PictureChooser.WindowsPhone/MvxInMemoryImageValueConverter.cs#L17

then you can use that in the Image control as

<Image Source="{Binding TheBytes, Converter={StaticResource InMemoryImage}}" />
Stuart
  • 66,722
  • 7
  • 114
  • 165
0

if your image is resource of the application then you can do as following

Uri uriR = new Uri("/WP7SampleProject3;component/images/appbar.feature.email.rest.png", UriKind.Relative);
BitmapImage imgSourceR = new BitmapImage(uriR);
this.imageR.Source = imgSourceR;
Damith
  • 62,401
  • 13
  • 102
  • 153