views:

354

answers:

1

Below is a Class that converts a string (coming from a sql server database) to a picture. How can I call a conversion function from a XAML Image control, so that when I open a childform I will see the image of an individual employee based on what row was chosen in the main forms datagrid. Basically, my question is - can I call a converter function that will translate the Photo text to an image at run time?. I tried something like this... I used code to do this before, but I would like to do it from XAML directly.

XAML:-

<Image x:Name="EmpPic"  
Source=Text="{Binding Photo, Mode=TwoWay, Converter={StaticResource PhotoConverter}" 
    HorizontalAlignment="Center"  
Width="165" 
Height="160" 
    Margin="2,2,2,2"/>

Code:-

    using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Data;

namespace SL3Demo.Utility
{
    //public class PhotoConverter : IValueConverter  
    //{
    //    //public BitmapImage ConvertBase64ToImage(string base64String)
    //    //{
    //    //    //Convert Base64 String to byte[]
    //    //    byte[] imageBytes = Convert.FromBase64String(base64String);

    //    //    BitmapImage bi = new BitmapImage();
    //    //    bi.SetSource(new MemoryStream(imageBytes));

    //    //    return bi;
    //    //}
    //}

    public class PhotoConverter 
    {
        public BitmapImage PhotoConvert(string value)
        {
            byte[] imageBytes = Convert.FromBase64String(value.ToString());
            BitmapImage bi = new BitmapImage();
            bi.SetSource(new MemoryStream(imageBytes));

            return bi;
        }



    }

}
A: 

Its close but needs a little more work. First the code, you need an implementation of IValueConverter:-

public class PhotoConverter : IValueConverter
{
    private BitmapImage PhotoConvert(string value)
    {
        BitmapImage bi = null;
        if (!String.IsNullOrEmpty(value))
        {
          byte[] imageBytes = Convert.FromBase64String(value);
          bi = new BitmapImage();
          bi.SetSource(new MemoryStream(imageBytes));
        }

        return bi;
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return PhotoConvert((string)value);
    }

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

Now you need to make an instance of this converter available in Xaml, a good place would be the UserControls Resources dictionary:-

<UserControl ...Usual set of xmlns here...
  xmlns:utils="clr-namespace:SL3Demo.Utility;assembly=SL3Demo">

   <UserControl.Resources>
     <utils:PhotoConverter x:Key="PhotoConverter" />
   </UserControl.Resources>

Then later on in your image:-

<Image x:Name="EmpPic"
    Source="{Binding Photo, Converter={StaticResource PhotoConverter} }" 
    HorizontalAlignment="Center" Width="165" Height="160" Margin="2,2,2,2"/>
AnthonyWJones
I tried your code but when I build I get this error:Error 1 Unexpected token None in Rule: MarkupExtension ::= '{' TYPENAME (Arguments)? @'}', in '{Binding Photo, Converter={StaticResource PhotoConverter}'.Do I need to add parameters to the XAML for the Image?
Tor Storli
There was a missing } from the Source attribute.
AnthonyWJones