1
0

MyImage.vb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 开头的字符串,则会尝试下载图片然后显示,图片会保存为本地缓存。
  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 Exit Property
  34. _Source = value
  35. If Not IsInitialized Then Exit Property '属性读取顺序修正:在完成 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 Exit Property
  78. _ActualSource = value
  79. Try
  80. Dim Bitmap As MyBitmap = If(value Is Nothing, Nothing, New MyBitmap(value)) '在这里先触发可能的文件读取,尽量避免在 UI 线程中读取文件
  81. RunInUiWait(Sub() MyBase.Source = Bitmap)
  82. Catch ex As Exception
  83. Log(ex, $"加载图片失败({value})")
  84. Try
  85. If value.StartsWithF(PathTemp) AndAlso File.Exists(value) Then File.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. Exit Sub
  98. End If
  99. '本地图片
  100. If Not Source.StartsWithF("http") Then
  101. ActualSource = Source
  102. Exit Sub
  103. End If
  104. '从缓存加载网络图片
  105. Dim Url As String = Source
  106. Dim Retried As Boolean = False
  107. Dim TempPath As String = GetTempPath(Url)
  108. Dim TempFile As New FileInfo(TempPath)
  109. Dim EnableCache As Boolean = Me.EnableCache
  110. If EnableCache AndAlso TempFile.Exists Then
  111. ActualSource = TempPath
  112. If (Date.Now - TempFile.LastWriteTime) < FileCacheExpiredTime Then Exit Sub '无需刷新缓存
  113. End If
  114. RunInNewThread(
  115. Sub()
  116. Dim TempDownloadingPath As String = Nothing
  117. Try
  118. RetryStart:
  119. '下载
  120. ActualSource = LoadingSource '显示加载中图片
  121. TempDownloadingPath = TempPath & RandomInteger(0, 10000000)
  122. NetDownload(Url, TempDownloadingPath, True)
  123. If Url <> Source AndAlso Url <> FallbackSource Then
  124. '已经更换了地址
  125. File.Delete(TempDownloadingPath)
  126. ElseIf EnableCache Then
  127. '保存缓存并显示
  128. If File.Exists(TempPath) Then File.Delete(TempPath)
  129. Rename(TempDownloadingPath, TempPath)
  130. RunInUi(Sub() ActualSource = TempPath)
  131. Else
  132. '直接显示
  133. RunInUiWait(Sub() ActualSource = TempDownloadingPath)
  134. File.Delete(TempDownloadingPath)
  135. End If
  136. Catch ex As Exception
  137. Try
  138. If TempPath IsNot Nothing Then File.Delete(TempPath)
  139. If TempDownloadingPath IsNot Nothing Then File.Delete(TempDownloadingPath)
  140. Catch
  141. End Try
  142. If Not Retried Then
  143. '更换备用地址
  144. Log(ex, $"下载图片可重试地失败({Url})", LogLevel.Developer)
  145. Retried = True
  146. Url = If(FallbackSource, Source)
  147. '空
  148. If Url Is Nothing Then
  149. ActualSource = Nothing
  150. Exit Sub
  151. End If
  152. '本地图片
  153. If Not Url.StartsWithF("http") Then
  154. ActualSource = Url
  155. Exit Sub
  156. End If
  157. '从缓存加载网络图片
  158. TempPath = GetTempPath(Url)
  159. TempFile = New FileInfo(TempPath)
  160. If EnableCache AndAlso TempFile.Exists() Then
  161. ActualSource = TempPath
  162. If (Date.Now - TempFile.CreationTime) < FileCacheExpiredTime Then Exit Sub '无需刷新缓存
  163. End If
  164. '下载
  165. If Source = Url Then Thread.Sleep(1000) '延迟 1s 重试
  166. GoTo RetryStart
  167. Else
  168. Log(ex, $"下载图片失败({Url})", LogLevel.Hint)
  169. End If
  170. End Try
  171. End Sub, "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal)
  172. End Sub
  173. Public Shared Function GetTempPath(Url As String) As String
  174. Return $"{PathTemp}MyImage\{GetHash(Url)}.png"
  175. End Function
  176. End Class