MyImage.vb 4.3 KB

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