Potatox
Potatox

Reputation: 3

How do I cache an image

Every time the user presses a button my application gets the image from the web, but this slow since the image isn't getting cached. Any way I can cache the image so it doesn't download it over and over making things faster. This is my code so you know how my application works.

if (listBox1.SelectedIndex == 0)
    {


        richTextBox1.Text = "Explore any game with Dex";
        pictureBox1.Load("THE LINK ");
        ProtoxRe.lol = 0;

    }
    else if ((listBox1.SelectedIndex == 1))
    {
        richTextBox1.Text = "SOME TEXT";
        pictureBox1.Load("LINK");
        ProtoxRe.lol = 1;
    }
    

Upvotes: 0

Views: 407

Answers (1)

WSC
WSC

Reputation: 993

There's probably more sophisticated methods, and this assumes your image is not expected to change, but you can do something like this:

//Image class is from this library
using System.Drawing;

//The scope of this will depend on where you need to access it from
Image img;

private Image GetImage()
{
    if (img == null)
    {
        img = GetImageFromHttp();
    }
    return img;
}

This is very basic, and has a number of flaws; you're not caching it between sessions so every application start will require the http get, and you're not checking if the image on the web is different, but it's a starting point.

Upvotes: 1

Related Questions