feat: 修改实现方式

This commit is contained in:
tangge233
2024-10-02 12:07:42 +08:00
parent 805a0e1544
commit a6aa7847c0
4 changed files with 44 additions and 48 deletions
@@ -1,32 +1,34 @@
Public Class MyImage
Inherits Image
'事件
'自定义属性
Public Uuid As Integer = GetUuid()
Private _Uri As String = ""
Public Property Source As String
Private _SourceData As String = ""
''' <summary>
''' 重写Image的Source属性
''' </summary>
Public Shadows Property Source As String
Get
Return _Uri
Return _SourceData
End Get
Set(value As String)
_Uri = value
RefreshImage()
SetImage(value)
End Set
End Property '显示文本
Public Shared ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback(
End Property
Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback(
Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
If Not IsNothing(sender) Then
CType(sender, MyImage)._Uri = e.NewValue.ToString()
CType(sender, MyImage).RefreshImage()
CType(sender, MyImage).SetImage(e.NewValue)
End If
End Sub)))
Public Sub RefreshImage()
Private Sub SetImage(source As String)
If Me Is Nothing Then Exit Sub
Try
PanContent.Source = New MyBitmap(_Uri.ToString())
Catch ex As Exception
Log(ex, "刷新图片内容失败")
End Try
If String.IsNullOrEmpty(source) Then Exit Sub
_SourceData = source
MyBase.Source = New MyBitmap(_SourceData)
End Sub
End Class
@@ -1,7 +0,0 @@
<Border x:Class="MyImage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FocusVisualStyle="{x:Null}" x:Name="PanBack"
ClipToBounds="True" UseLayoutRounding="False" SnapsToDevicePixels="False" Background="{StaticResource ColorBrushSemiTransparent}">
<Image x:Name="PanContent"/>
</Border>
+25 -18
View File
@@ -64,29 +64,36 @@ Public Class MyBitmap
BitmapCache.Add(FilePathOrResourceName, Pic)
End If
Else
Dim tempFile = PathTemp & "Cache\MyImage\" & GetHash(FilePathOrResourceName)
If FilePathOrResourceName.StartsWith("http") Then
If FilePathOrResourceName.StartsWithF("http") Then '在线图片(这里判断 https:\\ 或 http:\\ 会出问题)
Dim tempFile = PathTemp & "Cache\MyImage\" & GetHash(FilePathOrResourceName)
NetDownload(FilePathOrResourceName, tempFile, True)
FilePathOrResourceName = tempFile
ElseIf FilePathOrResourceName.StartsWithF("data:image/png;base64,") Then 'base64 图片
Dim base64Data = FilePathOrResourceName.Split(",")(1)
Dim imageData = Convert.FromBase64String(base64Data)
Using ms = New MemoryStream(imageData)
Pic = New System.Drawing.Bitmap(ms)
End Using
Return
End If
'使用这种自己接管 FileStream 的方法加载才能解除文件占用
Using InputStream As New FileStream(FilePathOrResourceName, FileMode.Open)
'判断是否为 WebP 文件头
Dim Header(1) As Byte
InputStream.Read(Header, 0, 2)
InputStream.Seek(0, SeekOrigin.Begin)
If Header(0) = 82 AndAlso Header(1) = 73 Then
'读取 WebP
If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。")
Dim FileBytes(InputStream.Length - 1) As Byte
InputStream.Read(FileBytes, 0, FileBytes.Length)
Dim Decoder As New Imazen.WebP.SimpleDecoder()
Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length)
Else
Pic = New System.Drawing.Bitmap(InputStream)
End If
End Using
End If
'判断是否为 WebP 文件头
Dim Header(1) As Byte
InputStream.Read(Header, 0, 2)
InputStream.Seek(0, SeekOrigin.Begin)
If Header(0) = 82 AndAlso Header(1) = 73 Then
'读取 WebP
If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。")
Dim FileBytes(InputStream.Length - 1) As Byte
InputStream.Read(FileBytes, 0, FileBytes.Length)
Dim Decoder As New Imazen.WebP.SimpleDecoder()
Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length)
Else
Pic = New System.Drawing.Bitmap(InputStream)
End If
End Using
End If
Catch ex As Exception
Pic = My.Application.TryFindResource(FilePathOrResourceName)
If Pic Is Nothing Then
@@ -183,9 +183,6 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Controls\IMyRadio.vb" />
<Compile Include="Controls\MyImage.xaml.vb">
<DependentUpon>MyImage.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\MyComboBox.vb" />
<Compile Include="Controls\MyComboBoxItem.vb" />
<Compile Include="Controls\MyCard.vb" />
@@ -194,6 +191,7 @@
<Compile Include="Controls\MyIconTextButton.xaml.vb">
<DependentUpon>MyIconTextButton.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\MyImage.vb" />
<Compile Include="Modules\Minecraft\ModComp.vb" />
<Compile Include="Modules\Minecraft\ModJava.vb" />
<Compile Include="Modules\Minecraft\ModMod.vb" />
@@ -381,10 +379,6 @@
<Compile Include="Pages\PageSpeedRight.xaml.vb">
<DependentUpon>PageSpeedRight.xaml</DependentUpon>
</Compile>
<Page Include="Controls\MyImage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Controls\MyExtraButton.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>