From 4e9ed4630c1355bd1c0dac6a2c8dab698b331e30 Mon Sep 17 00:00:00 2001 From: Hakoyu Date: Wed, 1 Nov 2023 14:44:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E5=9B=BE=E5=83=8F=E4=BC=9A=E9=99=8D=E4=BD=8E=E5=88=86=E8=BE=A8?= =?UTF-8?q?=E7=8E=87=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VPet.ModMaker/Models/Expansions.cs | 11 +++++++++-- VPet.ModMaker/Models/Utils.cs | 7 ++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/VPet.ModMaker/Models/Expansions.cs b/VPet.ModMaker/Models/Expansions.cs index 13c607b..3af3701 100644 --- a/VPet.ModMaker/Models/Expansions.cs +++ b/VPet.ModMaker/Models/Expansions.cs @@ -79,14 +79,21 @@ public static class Extensions /// /// 图片资源 /// 路径 - public static void SaveToPng(this BitmapSource image, string path) + public static void SaveToPng(this BitmapImage image, string path) { if (image is null) return; if (path.EndsWith(".png") is false) path += ".png"; var encoder = new PngBitmapEncoder(); - encoder.Frames.Add(BitmapFrame.Create(image)); + var stream = image.StreamSource; + // 保存位置 + var position = stream.Position; + // 必须要重置位置, 否则EndInit将出错 + stream.Seek(0, SeekOrigin.Begin); + encoder.Frames.Add(BitmapFrame.Create(image.StreamSource)); + // 恢复位置 + stream.Seek(position, SeekOrigin.Begin); using var fs = new FileStream(path, FileMode.Create); encoder.Save(fs); } diff --git a/VPet.ModMaker/Models/Utils.cs b/VPet.ModMaker/Models/Utils.cs index 732b294..daa1ac5 100644 --- a/VPet.ModMaker/Models/Utils.cs +++ b/VPet.ModMaker/Models/Utils.cs @@ -53,11 +53,8 @@ public static class Utils bitmapImage.BeginInit(); try { - var ms = new MemoryStream(); - using var sr = new StreamReader(imagePath); - sr.BaseStream.CopyTo(ms); - bitmapImage.StreamSource = ms; - bitmapImage.DecodePixelWidth = DecodePixelWidth; + var bytes = File.ReadAllBytes(imagePath); + bitmapImage.StreamSource = new MemoryStream(bytes); } finally {