Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Image UriSource and Data Binding using http:\\ URL
    primarykey
    data
    text
    <p>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 (<a href="https://stackoverflow.com/questions/20586/wpf-image-urisource-and-data-binding">Image UriSource and Data Binding</a>) but none of those suggestions have worked.</p> <p>What I'd like to do is:</p> <pre><code>&lt;Image Width="50" Name="MemberImage"&gt; &lt;Image.Source&gt; &lt;BitmapImage DecodePixelWidth="50" UriSource="{Binding Member.ImageFilePathUri}" /&gt; &lt;/Image.Source&gt; &lt;/Image&gt; </code></pre> <p>ImageFilePathUri is a Uri created from the string path through:</p> <pre><code>public Uri ImageFilePathUri { get { return new Uri(this.ImageFilePath); } } } </code></pre> <p>This gives the "Property 'UriSource' or property 'StreamSource' must be set." error as expected.</p> <p>I have also tried using a value converter:</p> <pre><code>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; } } </code></pre> <p>However, binding to it using:</p> <pre><code>&lt;Image Name="TestImage" Width="50" Source="{Binding Path=Member.ImageFilePath, Converter=Parliament.HansardApplicationSuite.Logging.Helpers.ImageConverter}"&gt;&lt;/Image&gt; </code></pre> <p>doesn't display the image.</p> <p>A further attempt to load the image programmatically, in the control constructor and/or the control Loaded event have also not worked:</p> <pre><code>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; } </code></pre> <p>The one thing that has worked is saving the image to a local file path and displaying that:</p> <pre><code>&lt;Image Width="50" Name="MemberImage"&gt; &lt;Image.Source&gt; &lt;BitmapImage DecodePixelWidth="50" UriSource="C:\Data\6bc64e7b-2df5-40d5-b6c4-eaf732318222.jpg" /&gt; &lt;/Image.Source&gt; &lt;/Image&gt; </code></pre> <p>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.</p> <pre><code>&lt;Image.Source&gt; &lt;BitmapImage DecodePixelWidth="50" UriSource="http://member.org/6bc64e7b-2df5-40d5-b6c4-eaf732318222.jpg" /&gt; &lt;/Image.Source&gt; </code></pre> <p>Update:</p> <p>This is the MemberImage property implementation.</p> <pre><code>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 &gt; 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; } } </code></pre> <p>Update:</p> <p>This is how I am binding to the control in my view:</p> <pre><code>&lt;Image Width="50" Source="{Binding MemberImage}" /&gt; </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload