The Resx Designer in Visual Studio 2012 has been improved and now provides better support for image files.
An image file can be added to a resx file by opening the resx file in design view and by selecting the Images item in the Resx Designer. You can then drag and drop an image file onto the Resx Designer surface.
An Image
control can now be bound to a localized image, as shown in the following excerpt:
<Image Source="{Binding Resources.Instance.ImageResourceName,
Source={StaticResource BindableResources},
Converter={StaticResource BitmapImageConverter}}" />
As in the previous string resources example, the data-binding’s Source
property uses the ResourceBindingWrapper BindableResources
instance defined in the app’s resources. The data-binding’s Path
property is set to Resources.Instance.ImageResourceName
, which is a byte[]
property for the image.
Notice the use of the BitmapImageConverter
in the previous excerpt. This IValueConverter
is responsible for taking the image data in the form of a byte[]
and converting it into an image (see Listing 19.3).
public class BitmapImageConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
BitmapImage bitmapImage = new BitmapImage();
using (MemoryStream stream = new MemoryStream(bytes))
{
stream.Seek(0, SeekOrigin.Begin);
bitmapImage.SetSource(stream);
}
return bitmapImage;
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
3.142.249.42