0

I have a custom UserControl with an Image control in it. I'm trying to fetch an image from web[network server] and show it in my control, refreshing the source using a dispatcher timer. Here is the code:

  void StartSourceRefresh()
    {
        if (timeinterval < 1) timeinterval = 1;
        tmrRefresh.Tick += new EventHandler(dispatcherTimer_Tick);
        tmrRefresh.Interval = new TimeSpan(0, 0, timeinterval); //in hour-minute-second
        tmrRefresh.Start();
    }

    public void ChangeImageSource(string newSource)
    {
        //newSource = "http://192.168.1.3/abc/imagetobeshown.png"

        WebImg.Source = null;

        if (newSource.Trim() == "")
            WebImg.Source = new BitmapImage(new Uri(@imagePlaceholder, UriKind.Absolute));
        else
        {
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.UriSource = new Uri(@newSource, UriKind.Absolute);
            image.EndInit();
            WebImg.Source = image;
        }
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
      ChangeImageSource(txtImgSrc.Text.Trim());
    }

the problem is the image wont change. Its showing the same one which was fetched at the first time. Timer is running fine. But the image just won't change. What am i doing wrong here?

Edit: Network Source gets refreshed after certain interval, so have to fetch the same source

boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44
  • 1
    See also [What's the use/meaning of the @ character in variable names in C#?](https://stackoverflow.com/questions/91817/whats-the-use-meaning-of-the-character-in-variable-names-in-c). – Clemens Jul 03 '17 at 10:21

1 Answers1

2

You are apparently reloading from the same image URL, which is cached by default.

Disable caching by setting BitmapCreateOptions.IgnoreImageCache:

var image = new BitmapImage();
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri(newSource);
image.EndInit();
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • the network source for the image gets refreshed after a certain interval, thus changing the image. so i have to fetch the same source – boop_the_snoot Jul 03 '17 at 10:10
  • That's why you should bypass caching. – Clemens Jul 03 '17 at 10:10
  • apparently ` new BitmapImage( new Uri(newSource), new RequestCachePolicy(RequestCacheLevel.BypassCache));` didn't work for me. But the other one worked just fine. – boop_the_snoot Jul 03 '17 at 10:23
  • after using BitmapCreateOptions.IgnoreImageCache, the image gets flickered for a micro-second or so. I guess when the cache is cleared, the image source becomes null for that period, if I'm not wrong. But is there any way to remove that flicker? – boop_the_snoot Jul 03 '17 at 10:28
  • The flickering is due to the asynchronous loading of the BitmapImage in a background thread. When you assign `WebImg.Source = image` the image is not yet loaded and hence the Image control shows an empty image. See [this answer](https://stackoverflow.com/a/16041810/1136211) for how to load an image asynchronously before assigning to the Source property. An alternative might be to attach a DownloadCompleted handler to the BitmapImage and assign the Source property in that handler. – Clemens Jul 03 '17 at 10:32