I am having a problem displaying an image with a web URL in a WPF user control. I have worked through all the suggestions for a similar problem asked on this site in Aug 2008 (http://stackoverflow.com/questions/20586/wpf-image-urisource-and-data-binding) but none of those suggestions have worked.
What I'd like to do is:
<Image Width="50" Name="MemberImage">
<Image.Source>
<BitmapImage DecodePixelWidth="50" UriSource="{Binding Member.ImageFilePathUri}" />
</Image.Source>
</Image>
ImageFilePathUri is a Uri created from the string path through:
public Uri ImageFilePathUri
{
get
{
return new Uri(this.ImageFilePath);
}
}
}
This gives the "Property 'UriSource' or property 'StreamSource' must be set." error as expected.
I have also tried using a value converter:
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var image = new BitmapImage();
image.BeginInit();
if (value != null)
{
image.UriSource = new Uri((string)value);
}
image.DecodePixelWidth = 50;
image.EndInit();
return image;
}
}
However, binding to it using:
<Image Name="TestImage" Width="50" Source="{Binding Path=Member.ImageFilePath, Converter=Parliament.HansardApplicationSuite.Logging.Helpers.ImageConverter}"></Image>
doesn't display the image.
A further attempt to load the image programmatically, in the control constructor and/or the control Loaded event have also not worked:
if (this.MemberRollItemViewModel.Member != null)
{
var image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(this.MemberRollItemViewModel.Member.ImageFilePath);
image.DecodePixelWidth = 50;
image.EndInit();
this.MemberImage.Source = image;
}
The one thing that has worked is saving the image to a local file path and displaying that:
<Image Width="50" Name="MemberImage">
<Image.Source>
<BitmapImage DecodePixelWidth="50" UriSource="C:\Data\6bc64e7b-2df5-40d5-b6c4-eaf732318222.jpg" />
</Image.Source>
</Image>
This is obviously only useful in debugging the problem and is not a solution. The same code but substituting the http address for the local file path doesn't work.
<Image.Source>
<BitmapImage DecodePixelWidth="50" UriSource="http://member.org/6bc64e7b-2df5-40d5-b6c4-eaf732318222.jpg" />
</Image.Source>
Update:
This is the MemberImage property implementation.
public BitmapImage MemberImage
{
get
{
var image = new BitmapImage();
if (this.Member != null)
{
WebRequest request = WebRequest.Create(new Uri(this.Member.ImageFilePath, UriKind.Absolute));
request.Timeout = -1;
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
BinaryReader reader = new BinaryReader(responseStream);
MemoryStream memoryStream = new MemoryStream();
byte[] bytebuffer = new byte[BytesToRead];
int bytesRead = reader.Read(bytebuffer, 0, BytesToRead);
while (bytesRead > 0)
{
memoryStream.Write(bytebuffer, 0, bytesRead);
bytesRead = reader.Read(bytebuffer, 0, BytesToRead);
}
image.BeginInit();
memoryStream.Seek(0, SeekOrigin.Begin);
image.StreamSource = memoryStream;
image.EndInit();
}
return image;
}
}
Update:
This is how I am binding to the control in my view:
<Image Width="50" Source="{Binding MemberImage}" />
MemberImage is the property I have given above. My data context is being set correctly because that property is being run, it's just not returning an image.