feat: MyImage 控件

This commit is contained in:
tangge233
2024-10-01 23:12:40 +08:00
parent 76bf3c40d0
commit 94476384bd
5 changed files with 68 additions and 21 deletions
@@ -0,0 +1,7 @@
<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>
@@ -0,0 +1,32 @@
Public Class MyImage
'自定义属性
Public Uuid As Integer = GetUuid()
Private _Uri As String = ""
Public Property Source As String
Get
Return _Uri
End Get
Set(value As String)
_Uri = value
RefreshImage()
End Set
End Property '显示文本
Public Shared 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()
End If
End Sub)))
Public Sub RefreshImage()
If Me Is Nothing Then Exit Sub
Try
PanContent.Source = New MyBitmap(_Uri.ToString())
Catch ex As Exception
Log(ex, "刷新图片内容失败")
End Try
End Sub
End Class
+21 -16
View File
@@ -64,24 +64,29 @@ 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.StartsWith("http") Then
NetDownload(FilePathOrResourceName, tempFile, True)
FilePathOrResourceName = tempFile
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
@@ -8,6 +8,7 @@
<StackPanel Name="PanMain" Margin="25,25,25,10" Grid.IsSharedSizeScope="True"> <StackPanel Name="PanMain" Margin="25,25,25,10" Grid.IsSharedSizeScope="True">
<StackPanel x:Name="PanCustom"> <StackPanel x:Name="PanCustom">
</StackPanel> </StackPanel>
<local:MyImage Source="https://cdn.modrinth.com/data/AANobbMI/295862f4724dc3f78df3447ad6072b2dcd3ef0c9_96.webp"/>
<local:MyCard Margin="0,0,0,15" Title="快照版提示" x:Name="PanHint"> <local:MyCard Margin="0,0,0,15" Title="快照版提示" x:Name="PanHint">
<local:MyIconButton LogoScale="1.1" Logo="F1 M2,0 L0,2 8,10 0,18 2,20 10,12 18,20 20,18 12,10 20,2 18,0 10,8 2,0Z" <local:MyIconButton LogoScale="1.1" Logo="F1 M2,0 L0,2 8,10 0,18 2,20 10,12 18,20 20,18 12,10 20,2 18,0 10,8 2,0Z"
Height="20" Margin="10" HorizontalAlignment="Right" VerticalAlignment="Top" Height="20" Margin="10" HorizontalAlignment="Right" VerticalAlignment="Top"
@@ -183,6 +183,9 @@
<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" />
@@ -191,9 +194,6 @@
<Compile Include="Controls\MyIconTextButton.xaml.vb"> <Compile Include="Controls\MyIconTextButton.xaml.vb">
<DependentUpon>MyIconTextButton.xaml</DependentUpon> <DependentUpon>MyIconTextButton.xaml</DependentUpon>
</Compile> </Compile>
<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,6 +381,10 @@
<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>
@@ -478,7 +482,6 @@
<DependentUpon>FormMain.xaml</DependentUpon> <DependentUpon>FormMain.xaml</DependentUpon>
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Page Include="Modules\Minecraft\MyLocalModItem.xaml"> <Page Include="Modules\Minecraft\MyLocalModItem.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
@@ -913,7 +916,6 @@
<Resource Include="Images\Heads\MCBBS.png" /> <Resource Include="Images\Heads\MCBBS.png" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Resource Include="Images\Blocks\NeoForge.png" /> <Resource Include="Images\Blocks\NeoForge.png" />