MyImage.vb 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. Public Class MyImage
  2. Inherits Image
  3. Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期
  4. ''' <summary>
  5. ''' 是否使用缓存
  6. ''' </summary>
  7. ''' <returns></returns>
  8. Public Property UseCache As Boolean
  9. Get
  10. Return GetValue(UseCacheProperty)
  11. End Get
  12. Set(value As Boolean)
  13. SetValue(UseCacheProperty, value)
  14. End Set
  15. End Property
  16. Public Shared ReadOnly UseCacheProperty As DependencyProperty = DependencyProperty.Register("UseCache", GetType(Boolean), GetType(MyImage), New PropertyMetadata(True))
  17. Private _SourceData As String = ""
  18. ''' <summary>
  19. ''' 重写Image的Source属性
  20. ''' </summary>
  21. Public Shadows Property Source As String
  22. Get
  23. Return _SourceData
  24. End Get
  25. Set(value As String)
  26. If String.IsNullOrEmpty(value) Then Exit Property
  27. _SourceData = value
  28. Try
  29. If Not value.StartsWithF("http") Then ' 本地资源直接使用
  30. MyBase.Source = New MyBitmap(_SourceData)
  31. Exit Property
  32. End If
  33. Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存
  34. Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png"
  35. If UseCache AndAlso File.Exists(TempFilePath) AndAlso (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下
  36. If Not NeedDownload Then
  37. MyBase.Source = New MyBitmap(TempFilePath)
  38. Exit Property
  39. End If
  40. If File.Exists(TempFilePath) Then '先显示着旧图片,下载新图片
  41. Rename(TempFilePath, TempFilePath & ".old")
  42. MyBase.Source = New MyBitmap(TempFilePath & ".old")
  43. End If
  44. ' 开一个线程下载在线图片
  45. RunInNewThread(Sub() PicLoader(_SourceData, TempFilePath), "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal)
  46. Catch ex As Exception
  47. Log(ex, "加载图片失败")
  48. End Try
  49. End Set
  50. End Property
  51. Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback(
  52. Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
  53. If Not IsNothing(sender) Then
  54. If String.IsNullOrEmpty(e.NewValue.ToString()) Then Exit Sub
  55. CType(sender, MyImage).Source = e.NewValue.ToString()
  56. End If
  57. End Sub)))
  58. Private Sub PicLoader(FileUrl As String, TempFilePath As String)
  59. Dim Retried As Boolean = False
  60. RetryStart:
  61. Try
  62. Dim UnCompleteFile As String = TempFilePath & ".dl" '加一个下载中的后缀,防止中途关闭程序下载中断但是没下完,从而导致第二次显示的是损坏的图片……
  63. NetDownload(FileUrl, UnCompleteFile)
  64. Rename(UnCompleteFile, TempFilePath)
  65. RunInUi(Sub()
  66. MyBase.Source = New MyBitmap(TempFilePath)
  67. End Sub)
  68. File.Delete(TempFilePath & ".old")
  69. Catch ex As Exception
  70. If Not Retried Then
  71. Retried = True
  72. GoTo RetryStart
  73. Else
  74. Log(ex, $"[MyImage] 下载图片失败")
  75. End If
  76. End Try
  77. End Sub
  78. End Class