从winforms picturebox中的url加载的图像是否存储在缓存中?
在winform应用程序的表单中,我必须显示存储在Web服务器上的图像(多个图像)。显示图像没有问题,因为我可以简单地将URL分配给picturebox从winforms picturebox中的url加载的图像是否存储在缓存中?,winforms,caching,picturebox,Winforms,Caching,Picturebox,在winform应用程序的表单中,我必须显示存储在Web服务器上的图像(多个图像)。显示图像没有问题,因为我可以简单地将URL分配给picturebox picturebox1.ImageLocation = "http://example.com/Image.jpg"; 该表单将被频繁打开多次,现在每次打开表单时,都会下载图像。没有必要增加交通量 是否可以告诉picturebox缓存图像(就像浏览器一样),以便下次请求相同的图像时,应该快速加载。 可能吗?您可以将图像存储在临时文件夹中,并在
picturebox1.ImageLocation = "http://example.com/Image.jpg";
该表单将被频繁打开多次,现在每次打开表单时,都会下载图像。没有必要增加交通量
是否可以告诉picturebox缓存图像(就像浏览器一样),以便下次请求相同的图像时,应该快速加载。
可能吗?您可以将图像存储在临时文件夹中,并在打开表单时首先检查临时文件夹。您可以将图像存储在临时文件夹中,并在打开表单时首先检查临时文件夹。使用
Image img=Image.FromFile(“…”)代码>
然后您可以将图像提供给PictureBox:
pictureBox1.Image=img代码>用
Image img=Image.FromFile(“…”)代码>
然后您可以将图像提供给PictureBox:
pictureBox1.Image=img代码>请尝试以下方法:
首先创建一个函数来检查文件是否存在。如果存在,则只需从本地路径加载文件,否则从URL下载文件并将其存储在本地
//Function to validate the local cache file
private Image load_image()
{
Image img=null;
if(!(File.Exists(@"d:\samp.png")))
{
using (HttpClient httpclient= new HttpClient())
{
var response = httpclient.GetAsync(@"https://i.imgur.com/Jb6lTp1.png");
if (!response.Result.IsSuccessStatusCode)
{
return img;
}
using (var fs= new FileStream(@"d:\samp.png",FileMode.CreateNew))
{
response.Result.Content.CopyToAsync(fs);
}
}
}
img = Image.FromFile(@"d:\samp.png");
return img;
}
//calling the function of click event
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = load_image();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
试着这样做:
首先创建一个函数来检查文件是否存在。如果存在,则只需从本地路径加载文件,否则从URL下载文件并将其存储在本地
//Function to validate the local cache file
private Image load_image()
{
Image img=null;
if(!(File.Exists(@"d:\samp.png")))
{
using (HttpClient httpclient= new HttpClient())
{
var response = httpclient.GetAsync(@"https://i.imgur.com/Jb6lTp1.png");
if (!response.Result.IsSuccessStatusCode)
{
return img;
}
using (var fs= new FileStream(@"d:\samp.png",FileMode.CreateNew))
{
response.Result.Content.CopyToAsync(fs);
}
}
}
img = Image.FromFile(@"d:\samp.png");
return img;
}
//calling the function of click event
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = load_image();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
我尝试了http嗅探器,发现每次打开表单时它都在下载图像。我在12个图片框中显示了12个图像。我尝试了http嗅探器,发现每次打开表单时它都在下载图像。我在12个图片框中展示12幅图像。