From a6aa7847c014f516aa74d5448024fa9948dad598 Mon Sep 17 00:00:00 2001 From: tangge233 Date: Wed, 2 Oct 2024 12:07:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/{MyImage.xaml.vb => MyImage.vb} | 34 ++++++++------- Plain Craft Launcher 2/Controls/MyImage.xaml | 7 --- .../Modules/Base/MyBitmap.vb | 43 +++++++++++-------- .../Plain Craft Launcher 2.vbproj | 8 +--- 4 files changed, 44 insertions(+), 48 deletions(-) rename Plain Craft Launcher 2/Controls/{MyImage.xaml.vb => MyImage.vb} (55%) delete mode 100644 Plain Craft Launcher 2/Controls/MyImage.xaml diff --git a/Plain Craft Launcher 2/Controls/MyImage.xaml.vb b/Plain Craft Launcher 2/Controls/MyImage.vb similarity index 55% rename from Plain Craft Launcher 2/Controls/MyImage.xaml.vb rename to Plain Craft Launcher 2/Controls/MyImage.vb index 9f4d934..f0c3d80 100644 --- a/Plain Craft Launcher 2/Controls/MyImage.xaml.vb +++ b/Plain Craft Launcher 2/Controls/MyImage.vb @@ -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 = "" + + ''' + ''' 重写Image的Source属性 + ''' + 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 diff --git a/Plain Craft Launcher 2/Controls/MyImage.xaml b/Plain Craft Launcher 2/Controls/MyImage.xaml deleted file mode 100644 index b0fa06a..0000000 --- a/Plain Craft Launcher 2/Controls/MyImage.xaml +++ /dev/null @@ -1,7 +0,0 @@ - - - \ No newline at end of file diff --git a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb index 8a0277a..33a3150 100644 --- a/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb +++ b/Plain Craft Launcher 2/Modules/Base/MyBitmap.vb @@ -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 diff --git a/Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj b/Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj index 323ad7d..85d3bd7 100644 --- a/Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj +++ b/Plain Craft Launcher 2/Plain Craft Launcher 2.vbproj @@ -183,9 +183,6 @@ Designer - - MyImage.xaml - @@ -194,6 +191,7 @@ MyIconTextButton.xaml + @@ -381,10 +379,6 @@ PageSpeedRight.xaml - - MSBuild:Compile - Designer - Designer MSBuild:Compile