From 13dd7511f681dadd026a8ceb8120371dedb167bc Mon Sep 17 00:00:00 2001 From: ww-rm Date: Fri, 21 Mar 2025 20:35:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A2=84=E8=A7=88=E5=9B=BE?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpineViewer/MainForm.cs | 21 ++++++++++++++++++--- SpineViewer/Spine/Spine.cs | 36 ++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/SpineViewer/MainForm.cs b/SpineViewer/MainForm.cs index be03131..0ebcad9 100644 --- a/SpineViewer/MainForm.cs +++ b/SpineViewer/MainForm.cs @@ -223,6 +223,13 @@ namespace SpineViewer var outputDir = arguments.OutputDir; var width = arguments.PreviewWidth; var height = arguments.PreviewHeight; + // TODO: 增加填充参数 + var paddingL = 1u; + var paddingR = 1u; + var paddingT = 1u; + var paddingB = 1u; + + var tex = new SFML.Graphics.RenderTexture(width, height); int success = 0; int error = 0; @@ -241,11 +248,19 @@ namespace SpineViewer } var spine = spines[i]; + var tmp = spine.CurrentAnimation; + spine.CurrentAnimation = Spine.Spine.EMPTY_ANIMATION; + tex.SetView(spine.GetInitView(width, height, paddingL, paddingR, paddingT, paddingB)); + tex.Clear(SFML.Graphics.Color.Transparent); + tex.Draw(spine); + tex.Display(); + spine.CurrentAnimation = tmp; try { - var preview = spine.GetPreview(width, height); - var savePath = Path.Combine(outputDir, $"{spine.Name}.png"); - preview.SaveToFile(savePath); + using (var img = tex.Texture.CopyToImage()) + { + img.SaveToFile(Path.Combine(outputDir, $"{spine.Name}.png")); + } success++; } catch (Exception ex) diff --git a/SpineViewer/Spine/Spine.cs b/SpineViewer/Spine/Spine.cs index ef04b29..5a30ed5 100644 --- a/SpineViewer/Spine/Spine.cs +++ b/SpineViewer/Spine/Spine.cs @@ -49,9 +49,14 @@ namespace SpineViewer.Spine public const string EMPTY_ANIMATION = ""; /// - /// 预览图大小 + /// 预览图宽 /// - public static readonly Size PREVIEW_SIZE = new(256, 256); + public const uint PREVIEW_WIDTH = 256; + + /// + /// 预览图高 + /// + public const uint PREVIEW_HEIGHT = 256; /// /// 缩放最小值 @@ -366,7 +371,15 @@ namespace SpineViewer.Spine { if (preview is null) { - using var img = GetPreview((uint)PREVIEW_SIZE.Width, (uint)PREVIEW_SIZE.Height); + var tex = new SFML.Graphics.RenderTexture(PREVIEW_WIDTH, PREVIEW_HEIGHT); + tex.SetView(GetInitView(PREVIEW_WIDTH, PREVIEW_HEIGHT)); + var tmp = CurrentAnimation; + CurrentAnimation = EMPTY_ANIMATION; + tex.Draw(this); + CurrentAnimation = tmp; + tex.Display(); + + using var img = tex.Texture.CopyToImage(); img.SaveToMemory(out var imgBuffer, "bmp"); using var stream = new MemoryStream(imgBuffer); preview = new Bitmap(stream); @@ -376,23 +389,6 @@ namespace SpineViewer.Spine } private Image preview = null; - /// - /// 获取指定尺寸的预览图 - /// - public SFML.Graphics.Image GetPreview(uint width, uint height, uint paddingL = 1, uint paddingR = 1, uint paddingT = 1, uint paddingB = 1) - { - // XXX: 貌似无法使用 using 或者 Dispose 主动释放 tex 资源 - // 在批量添加的中途, 如果触发 GC? 会卡死, 目前未知原因 - var tex = new SFML.Graphics.RenderTexture(width, height); - tex.SetView(GetInitView(width, height, paddingL, paddingR, paddingT, paddingB)); - var tmp = CurrentAnimation; - CurrentAnimation = EMPTY_ANIMATION; - tex.Draw(this); - CurrentAnimation = tmp; - tex.Display(); - return tex.Texture.CopyToImage(); - } - /// /// 获取动画时长, 如果动画不存在则返回 0 ///