Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyImage 控件 #4868

Merged
merged 13 commits into from
Oct 13, 2024
7 changes: 7 additions & 0 deletions Plain Craft Launcher 2/Controls/MyImage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Border x:Class="MyImage"
tangge233 marked this conversation as resolved.
Show resolved Hide resolved
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>
32 changes: 32 additions & 0 deletions Plain Craft Launcher 2/Controls/MyImage.xaml.vb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 21 additions & 16 deletions Plain Craft Launcher 2/Modules/Base/MyBitmap.vb
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,29 @@ Public Class MyBitmap
BitmapCache.Add(FilePathOrResourceName, Pic)
End If
Else
Dim tempFile = PathTemp & "Cache\MyImage\" & GetHash(FilePathOrResourceName)
If FilePathOrResourceName.StartsWith("http") Then
NetDownload(FilePathOrResourceName, tempFile, True)
FilePathOrResourceName = tempFile
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
Expand Down
12 changes: 7 additions & 5 deletions Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@
<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" />
Expand All @@ -191,9 +194,6 @@
<Compile Include="Controls\MyIconTextButton.xaml.vb">
<DependentUpon>MyIconTextButton.xaml</DependentUpon>
</Compile>



<Compile Include="Modules\Minecraft\ModComp.vb" />
<Compile Include="Modules\Minecraft\ModJava.vb" />
<Compile Include="Modules\Minecraft\ModMod.vb" />
Expand Down Expand Up @@ -381,6 +381,10 @@
<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>
Expand Down Expand Up @@ -478,7 +482,6 @@
<DependentUpon>FormMain.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>

<Page Include="Modules\Minecraft\MyLocalModItem.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down Expand Up @@ -913,7 +916,6 @@
<Resource Include="Images\Heads\MCBBS.png" />
</ItemGroup>
<ItemGroup>

</ItemGroup>
<ItemGroup>
<Resource Include="Images\Blocks\NeoForge.png" />
Expand Down