Hello!
I am having some trouble with an image, and I hope somebody can help me, because I think I might be missing a very simple thing, it can't be sooo dificult!
I am developing a WPF application, that has to show an image, so I used a Image control. That image comes from a WCF service, that returns it like a byte[], then I convert it to a BitmapImage.
The first thing I tried was to put a button, and on the click event called the WCF service, convert the image and set it as Source of the Image control, and everything worked fine:
<Window x:Class="ImageTester.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="333.3" Width="356.631"><Grid><Button Height="23" Margin="19.278,21.42,0,0" Name="button1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="74.51" Click="button1_Click">Button</Button><Image Margin="19.278,78.54,0,0" Name="image1" Stretch="Fill" HorizontalAlignment="Left" Width="40" Height="40" VerticalAlignment="Top" /></Grid></Window>
private void button1_Click(object sender, RoutedEventArgs e)
{
//get the image from the WCF service
byte[] imgb = service_client.getImage();
//Convert it to BitmapImage
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = new MemoryStream(imgb);
image.EndInit();
//Set the image as Source of the control
image1.Source = image;
}
So when I run the application and click the button the image is shown. The problem is that I don't want to do it from code-behind, I need to do it in xaml through the Binding property of the Image control. I have been searching and found this solution:
<Window x:Class="ImageTester.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="333.3" Width="356.631"><Grid><Button Height="23" Margin="19.278,21.42,0,0" Name="button1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="74.51" Click="button1_Click">Button</Button><Image Margin="19.278,78.54,0,0" Name="image1" Stretch="Fill" HorizontalAlignment="Left" Width="40" Height="40" VerticalAlignment="Top" /><Image Width="90" Name="image2" Source="{Binding Path=ImageSource}" Margin="122.315,100.15,0,0" HorizontalAlignment="Left" Height="90" VerticalAlignment="Top" /><Button Height="23" HorizontalAlignment="Right" Margin="0,33.33,58.883,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click_1">Button</Button></Grid></Window>
I added a sencon image, "image2" and in the Source property y wrote "Binding Path=ImageSource". In the code-behind I added the object "IMageSource", so the get method returns the BitmapImage:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
//......
public object ImageSource
{
get
{
//Get the image from the WCF service
byte[] ib = service_client.getImage();
//Convert it to BitmapImage
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = new MemoryStream(ib);
image.EndInit();
//Return the image
return image;
}
}
}
It all seems to be right, but it doesn't work, when I run the app and the page is loaded nothing happens, there is no image.
I don't know if I have to add something on the xam, like setting the ImageSource as a resource or something like that, because I usually do this kind of things from code behind, not from the xaml.
Hope somebody can help me, I will be grateful. Thanks!