【问题标题】:How To Download Image by WebClient如何通过 WebClient 下载图像
【发布时间】:2013-06-07 16:30:50
【问题描述】:

我可以通过 webclient 下载图片并在图片框上成功绘制。但我的问题是如何避免使用缓存系统从网络重新下载相同的图像。但我不想把它保存到我的牧群磁盘中。

我的代码是:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim wc As New System.Net.WebClient
    Dim ImageInBytes() As Byte = wc.DownloadData(TextBox1.Text)
    Dim ImageStream As New IO.MemoryStream(ImageInBytes)

    PictureBox1.Image = New System.Drawing.Bitmap(ImageStream)
    PictureBox1.Refresh()
End Sub

【问题讨论】:

  • 为什么不将它存储在应用程序的主窗体中,只要应用程序正在运行?
  • 这是因为我必须多次使用相同的 url。如果我下载图片,我必须重新下载。我只想下载一次文件,如果我还需要图片,webclient从缓存中获取图片而不是再次下载图片。谢谢

标签: vb.net caching webclient picturebox


【解决方案1】:

我相信 WebClient 不支持使用缓存。

您可以尝试检查图像大小,如果下载不同,则使用您已经下载的那个。

此代码将获取图像大小,无需下载:

Dim wc As System.Net.WebClient = New System.Net.WebClient
wc.OpenRead("http://example.com/ex.png")
Dim bytes_total As Int64 = Convert.ToInt64(wc.ResponseHeaders("Content-Length"))

【讨论】:

  • 没有 ithink webclient 支持缓存策略,即 webclient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多