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;
}
}
}