locked
Blank - Black Image control in WPF if image source is absolute Uri RRS feed

  • Question

  • Hi I have this problem. I use on listbox control own datatemplate. Listbox item consist one image control and some textblock.

    On image source I bind property type of Uri (absolute url - for example: http://u.aimg.sk/fotky/1730/71/17307141.jpg?v=2 )

    Listbox have about 50 - 300 items.

    If I test app, I sometimes see blank - white or black image instead user images.

    The problem you can see on this images:

    http://i.imgur.com/fwV0Q.png

    http://i.imgur.com/0bPAK.png

     

    I would like to know what cause this problem and how can I solve this problem. Image sources are good, I check it in browser.

    Thank for advice.

    Tuesday, February 8, 2011 6:27 PM

Answers

All replies

  • since  01ciculienka's picture appears there is no problem :p.

    the downloading doesn't start before the binding, try to trigger the PropertyChanged event again after the download complete.

    <Grid Name="grid">
        <Image Name="img" Source="{Binding Path=Src}"/>
      </Grid>
    

     

    public event PropertyChangedEventHandler PropertyChanged;
        BitmapImage bitmap;
        private ImageSource _Src;
    
        public ImageSource Src
        {
          get { return _Src; }
          set 
          {
            _Src = value;
            if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs("Src"));
            ((BitmapImage)_Src).DownloadCompleted += new EventHandler(MainWindow_DownloadCompleted);
          }
        }
    
        void MainWindow_DownloadCompleted(object sender, EventArgs e)
        {
          PropertyChanged(this, new PropertyChangedEventArgs("Src"));
          ((BitmapImage)_Src).DownloadCompleted -= MainWindow_DownloadCompleted;
        }
        
        public MainWindow()
        {
          InitializeComponent();
          this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }
    
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
          grid.DataContext = this;
          bitmap = new BitmapImage(new Uri("http://i.imgur.com/fwV0Q.png"));
          Src = bitmap;
        }
    

    Alan-SY
    • Proposed as answer by Sheldon _Xiao Friday, February 25, 2011 8:19 AM
    • Marked as answer by Sheldon _Xiao Wednesday, March 2, 2011 3:26 PM
    Tuesday, February 8, 2011 7:30 PM
  • Hi Janc1,

    If your issue persists, please let us know.

    If your issue has been resolved, please remember to mark replies as the answer.

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 10, 2011 5:52 AM
  • Mr. Xiao, this solution above is not suitable for my problem. My scenarion is WPF with MVVM, I  bind on Image control property type of Uri. So I try make a converter which download image from web site, create from him object type of BitmapImage and this instace return back as Image Source.

     

    Problem is if listbox have 200 - 300 items (images) app freezing.  I create another question about this problem.

    Here is link; http://social.msdn.microsoft.com/Forums/en/wpf/thread/173cc002-3acf-47cc-9ce5-5fecb5c0c20b.

     

    Problem is I must refresh every 10 seconds listbox items from new fresh data. (something like contact list on skype for example).

     

    Something like this:

    private void RefreshContactsData(IEnumerable<KeyValuePair<string, User>> user)
       {  
        //User is property which is binded on listbox 
        //Clear old data
        Users.Clear();
    
        foreach (var freshUser in freshUsers)
        {
          //add new data
          Users.Add(freshUser.Value);
        }
    
      }
    
    

    User class consist:

    • Uri ProfilePhoto {get;set;} -> this property is bind on Image control, and I want use on this binding converter which download image from web
    • String Nick{get;set}
    • String Status{get;set;} //offline, online, chatting
    • String ChatRoom{get;set;}

    This solution has one problem -> app freezing

     

    I know my english is bad, do you understand me now what is my goal?

    Thursday, February 10, 2011 10:34 AM
    • Proposed as answer by Sheldon _Xiao Friday, February 25, 2011 8:19 AM
    • Marked as answer by Sheldon _Xiao Wednesday, March 2, 2011 3:28 PM
    Thursday, February 17, 2011 9:21 AM