开发者

ConvertBase64ToImage converter from XAML in Silverlight 3

开发者 https://www.devze.com 2022-12-08 22:42 出处:网络
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

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



    }

}


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"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消