boop_the_snoot
boop_the_snoot

Reputation: 3247

Fetching Web Image and showing in Image control after interval - not working

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

Upvotes: 0

Views: 54

Answers (1)

Clemens
Clemens

Reputation: 128060

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();

Upvotes: 2

Related Questions