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 Public Class MyImage
Inherits Image
'事件
'自定义属性
Public Uuid As Integer = GetUuid() 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 Get
Return _Uri Return _SourceData
End Get End Get
Set(value As String) Set(value As String)
_Uri = value SetImage(value)
RefreshImage()
End Set End Set
End Property '显示文本 End Property
Public Shared ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( 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) Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
If Not IsNothing(sender) Then If Not IsNothing(sender) Then
CType(sender, MyImage)._Uri = e.NewValue.ToString() CType(sender, MyImage).SetImage(e.NewValue)
CType(sender, MyImage).RefreshImage()
End If End If
End Sub))) End Sub)))
Public Sub RefreshImage() Private Sub SetImage(source As String)
If Me Is Nothing Then Exit Sub If Me Is Nothing Then Exit Sub
Try If String.IsNullOrEmpty(source) Then Exit Sub
PanContent.Source = New MyBitmap(_Uri.ToString()) _SourceData = source
Catch ex As Exception MyBase.Source = New MyBitmap(_SourceData)
Log(ex, "刷新图片内容失败")
End Try
End Sub End Sub
End Class 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) BitmapCache.Add(FilePathOrResourceName, Pic)
End If End If
Else Else
Dim tempFile = PathTemp & "Cache\MyImage\" & GetHash(FilePathOrResourceName) If FilePathOrResourceName.StartsWithF("http") Then '在线图片(这里判断 https:\\ 或 http:\\ 会出问题)
If FilePathOrResourceName.StartsWith("http") Then Dim tempFile = PathTemp & "Cache\MyImage\" & GetHash(FilePathOrResourceName)
NetDownload(FilePathOrResourceName, tempFile, True) NetDownload(FilePathOrResourceName, tempFile, True)
FilePathOrResourceName = tempFile 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 End If
'使用这种自己接管 FileStream 的方法加载才能解除文件占用 '使用这种自己接管 FileStream 的方法加载才能解除文件占用
Using InputStream As New FileStream(FilePathOrResourceName, FileMode.Open) Using InputStream As New FileStream(FilePathOrResourceName, FileMode.Open)
'判断是否为 WebP 文件头 '判断是否为 WebP 文件头
Dim Header(1) As Byte Dim Header(1) As Byte
InputStream.Read(Header, 0, 2) InputStream.Read(Header, 0, 2)
InputStream.Seek(0, SeekOrigin.Begin) InputStream.Seek(0, SeekOrigin.Begin)
If Header(0) = 82 AndAlso Header(1) = 73 Then If Header(0) = 82 AndAlso Header(1) = 73 Then
'读取 WebP '读取 WebP
If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。") If Is32BitSystem Then Throw New Exception("不支持在 32 位系统下加载 WebP 图片。")
Dim FileBytes(InputStream.Length - 1) As Byte Dim FileBytes(InputStream.Length - 1) As Byte
InputStream.Read(FileBytes, 0, FileBytes.Length) InputStream.Read(FileBytes, 0, FileBytes.Length)
Dim Decoder As New Imazen.WebP.SimpleDecoder() Dim Decoder As New Imazen.WebP.SimpleDecoder()
Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length) Pic = Decoder.DecodeFromBytes(FileBytes, FileBytes.Length)
Else Else
Pic = New System.Drawing.Bitmap(InputStream) Pic = New System.Drawing.Bitmap(InputStream)
End If End If
End Using End Using
End If End If
Catch ex As Exception Catch ex As Exception
Pic = My.Application.TryFindResource(FilePathOrResourceName) Pic = My.Application.TryFindResource(FilePathOrResourceName)
If Pic Is Nothing Then If Pic Is Nothing Then
@@ -183,9 +183,6 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="Controls\IMyRadio.vb" /> <Compile Include="Controls\IMyRadio.vb" />
<Compile Include="Controls\MyImage.xaml.vb">
<DependentUpon>MyImage.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\MyComboBox.vb" /> <Compile Include="Controls\MyComboBox.vb" />
<Compile Include="Controls\MyComboBoxItem.vb" /> <Compile Include="Controls\MyComboBoxItem.vb" />
<Compile Include="Controls\MyCard.vb" /> <Compile Include="Controls\MyCard.vb" />
@@ -194,6 +191,7 @@
<Compile Include="Controls\MyIconTextButton.xaml.vb"> <Compile Include="Controls\MyIconTextButton.xaml.vb">
<DependentUpon>MyIconTextButton.xaml</DependentUpon> <DependentUpon>MyIconTextButton.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Controls\MyImage.vb" />
<Compile Include="Modules\Minecraft\ModComp.vb" /> <Compile Include="Modules\Minecraft\ModComp.vb" />
<Compile Include="Modules\Minecraft\ModJava.vb" /> <Compile Include="Modules\Minecraft\ModJava.vb" />
<Compile Include="Modules\Minecraft\ModMod.vb" /> <Compile Include="Modules\Minecraft\ModMod.vb" />
@@ -381,10 +379,6 @@
<Compile Include="Pages\PageSpeedRight.xaml.vb"> <Compile Include="Pages\PageSpeedRight.xaml.vb">
<DependentUpon>PageSpeedRight.xaml</DependentUpon> <DependentUpon>PageSpeedRight.xaml</DependentUpon>
</Compile> </Compile>
<Page Include="Controls\MyImage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Controls\MyExtraButton.xaml"> <Page Include="Controls\MyExtraButton.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>