Dynamically changing XAML image sources?

How can image sources in the XAML be changed during runtime? Right now I have them pointing to a embedded resource URI. In the view model I have the image controls defined but not bound to anything, how do I get these on the view?

Answers


For example, it's possible to use imageconverter. If you set property to bind, you can get value from converter and then return BitmapSource to bind.

public sealed class ImageConverter : IValueConverter
{
    internal static class NativeMethods
    {
        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool DeleteObject(IntPtr hObject);
    }

    public BitmapSource ToBitmapSource(System.Drawing.Bitmap source)
    {
        BitmapSource bitSrc = null;

        var hBitmap = source.GetHbitmap();

        try
        {
            bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }
        catch (Win32Exception)
        {
            bitSrc = null;
        }
        finally
        {
            NativeMethods.DeleteObject(hBitmap);
        }

        return bitSrc;
    }

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        // call function to get BitmapSource
        using (Bitmap bitmap = new Bitmap("{image path}"))
        {

           return ToBitmapSource(bitmap);
        }
    }

}


       <Image x:Name="UserImage" Source="{Binding MembershipUserViewModel.UserId, Converter={StaticResource _userIdToImageConverter}, UpdateSourceTrigger=Explicit}" Stretch="Fill" />


public class UserIdToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var image = String.Format("{0}/../{1}.jpg",
                  Application.Current.Host.Source,
                  value);

        var bitmapImage = new BitmapImage(new Uri(image)){CreateOptions = BitmapCreateOptions.IgnoreImageCache};
        return bitmapImage;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Need Your Help

How to avoid Infinite $digest Loop error while working with ngResource

angularjs ngresource

I am $watching the $scope.randomObjects in my directive. The $watch will throw an error if the function is not stable and I think that my curried function inside getDesiredAmountOfObjects is idempo...

I want the TextView to end in one Line…how to do it

java android xml textview

i am developing an app, i would have linked an image but i don't have enough reputation to do so....my problem is The text doesn't fit in one line it goes to the second line,obviously thats because i

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.