MyImage.vb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. Public Class MyImage
  2. Inherits Image
  3. #Region "公开属性"
  4. ''' <summary>
  5. ''' 网络图片的缓存有效期。
  6. ''' 在这个时间后,才会重新尝试下载图片。
  7. ''' </summary>
  8. Public FileCacheExpiredTime As New TimeSpan(7, 0, 0, 0) '7 天
  9. ''' <summary>
  10. ''' 是否允许将网络图片存储到本地用作缓存。
  11. ''' </summary>
  12. Public Property EnableCache As Boolean
  13. Get
  14. Return GetValue(EnableCacheProperty)
  15. End Get
  16. Set(value As Boolean)
  17. SetValue(EnableCacheProperty, value)
  18. End Set
  19. End Property
  20. Public Shared Shadows ReadOnly EnableCacheProperty As DependencyProperty = DependencyProperty.Register(
  21. "EnableCache", GetType(Boolean), GetType(MyImage), New PropertyMetadata(True))
  22. ''' <summary>
  23. ''' 与 Image 的 Source 类似。
  24. ''' 若输入以 http 开头的字符串,则会尝试下载图片然后显示;若 EnableCache 设为 True,图片还会保存为本地缓存。
  25. ''' 支持 WebP 格式的图片。
  26. ''' </summary>
  27. Public Shadows Property Source As String '覆写 Image 的 Source 属性
  28. Get
  29. Return _Source
  30. End Get
  31. Set(value As String)
  32. If value = "" Then value = Nothing
  33. If _Source = value Then Return
  34. _Source = value
  35. If Not IsInitialized Then Return '属性读取顺序修正:在完成 XAML 属性读取后再触发图片加载(#4868)
  36. Load()
  37. End Set
  38. End Property
  39. Private _Source As String = ""
  40. Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register(
  41. "Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback(
  42. Sub(sender, e) If sender IsNot Nothing Then CType(sender, MyImage).Source = e.NewValue.ToString())))
  43. ''' <summary>
  44. ''' 若 Source 是一个网络图片,该地址将作为第二图片源。
  45. ''' </summary>
  46. Public Property FallbackSource As String
  47. Get
  48. Return _FallbackSource
  49. End Get
  50. Set(value As String)
  51. _FallbackSource = value
  52. End Set
  53. End Property
  54. Private _FallbackSource As String = Nothing
  55. ''' <summary>
  56. ''' 正在下载网络图片时显示的本地图片。
  57. ''' </summary>
  58. Public Property LoadingSource As String
  59. Get
  60. Return _LoadingSource
  61. End Get
  62. Set(value As String)
  63. _LoadingSource = value
  64. End Set
  65. End Property
  66. Private _LoadingSource As String = "pack://application:,,,/images/Icons/NoIcon.png"
  67. #End Region
  68. ''' <summary>
  69. ''' 实际被呈现的图片地址。
  70. ''' </summary>
  71. Public Property ActualSource As String
  72. Get
  73. Return _ActualSource
  74. End Get
  75. Set(value As String)
  76. If value = "" Then value = Nothing
  77. If _ActualSource = value Then Return
  78. _ActualSource = value
  79. Try
  80. Dim Bitmap As ImageSource = If(value Is Nothing, Nothing, New MyBitmap(value)) '在这里先触发可能的文件读取,尽量避免在 UI 线程中读取文件
  81. RunInUi(Sub() MyBase.Source = Bitmap)
  82. Catch ex As Exception
  83. Logger.Warn(ex, $"加载图片失败({value})")
  84. Try
  85. If value.StartsWithF(PathTemp) Then FileUtils.Delete(value)
  86. Catch
  87. End Try
  88. End Try
  89. End Set
  90. End Property
  91. Private _ActualSource As String = Nothing
  92. Private Sub Load() _
  93. Handles Me.Initialized '属性读取顺序修正:在完成 XAML 属性读取后再触发图片加载(#4868)
  94. '空
  95. If Source Is Nothing Then
  96. ActualSource = Nothing
  97. Return
  98. End If
  99. '本地图片
  100. If Not Source.StartsWithF("http") Then
  101. ActualSource = Source
  102. Return
  103. End If
  104. '从缓存加载网络图片
  105. Dim EnableCache As Boolean = Me.EnableCache
  106. Dim TempPath As String = $"{PathTemp}MyImage\{GetHash(Source)}.png{If(EnableCache, "", GetUuid())}" '不启用缓存时在末尾加上随机字符串,避免冲突
  107. Dim TempFile = FileUtils.GetInfo(TempPath)
  108. If EnableCache AndAlso TempFile.Exists Then
  109. ActualSource = TempPath '显示缓存的图片
  110. If (Date.Now - TempFile.LastWriteTime) < FileCacheExpiredTime Then Return '无需刷新缓存
  111. Else
  112. ActualSource = LoadingSource '显示加载中图片
  113. End If
  114. RunInNewThread(
  115. Sub()
  116. Dim IsLocalFallback As Boolean = Not String.IsNullOrEmpty(FallbackSource) AndAlso Not FallbackSource.StartsWithF("http")
  117. Try
  118. '下载
  119. NetDownloadByLoader(
  120. If(FallbackSource IsNot Nothing AndAlso FallbackSource.StartsWithF("http") AndAlso Source <> FallbackSource,
  121. {Source, FallbackSource}, {Source}),
  122. TempPath, SimulateBrowserHeaders:=True)
  123. ActualSource = TempPath
  124. If Not EnableCache Then FileUtils.Delete(TempPath)
  125. Catch ex As Exception
  126. Try
  127. If TempPath IsNot Nothing Then FileUtils.Delete(TempPath)
  128. Catch
  129. End Try
  130. '加载本地备用图片
  131. If IsLocalFallback Then
  132. Try
  133. ActualSource = FallbackSource
  134. Logger.Warn(ex, $"下载图片失败,使用本地备用图片({Source})")
  135. Return
  136. Catch exx As Exception
  137. Logger.Error(ex, $"下载图片失败({Source})", LogBehavior.Toast)
  138. Logger.Error(exx, $"加载备用图片失败({FallbackSource})", LogBehavior.Toast)
  139. End Try
  140. Else
  141. Logger.Error(ex, $"下载图片失败({Source},备用:{FallbackSource})", LogBehavior.Toast)
  142. End If
  143. End Try
  144. End Sub, "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal)
  145. End Sub
  146. End Class