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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Plain Craft Launcher 2/Controls/MyImage.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Public Class MyImage
Inherits Image

'事件

Private _SourceData As String = ""

''' <summary>
''' 重写Image的Source属性
''' </summary>
Public Shadows Property Source As String
Get
Return _SourceData
End Get
Set(value As String)
If String.IsNullOrEmpty(value) Then Exit Property
_SourceData = value
MyBase.Source = New MyBitmap(_SourceData)
End Set
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
If String.IsNullOrEmpty(e.NewValue.ToString()) Then Exit Sub
CType(sender, MyImage).Source = e.NewValue.ToString()
End If
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,23 +64,28 @@ Public Class MyBitmap
BitmapCache.Add(FilePathOrResourceName, Pic)
End If
Else
Dim InputStream As Stream
If FilePathOrResourceName.StartsWithF("http") Then '在线图片(这里判断 https:\\ 或 http:\\ 会出问题)
Dim Client = New Net.WebClient()
InputStream = New MemoryStream(Client.DownloadData(FilePathOrResourceName))
Else
InputStream = New MemoryStream(File.ReadAllBytes(FilePathOrResourceName))
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
'判断是否为 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 If
Catch ex As Exception
Pic = My.Application.TryFindResource(FilePathOrResourceName)
Expand Down
6 changes: 1 addition & 5 deletions Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +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" />
Expand Down Expand Up @@ -478,7 +476,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 +910,6 @@
<Resource Include="Images\Heads\MCBBS.png" />
</ItemGroup>
<ItemGroup>

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