优化预览图获取
This commit is contained in:
@@ -223,6 +223,13 @@ namespace SpineViewer
|
|||||||
var outputDir = arguments.OutputDir;
|
var outputDir = arguments.OutputDir;
|
||||||
var width = arguments.PreviewWidth;
|
var width = arguments.PreviewWidth;
|
||||||
var height = arguments.PreviewHeight;
|
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 success = 0;
|
||||||
int error = 0;
|
int error = 0;
|
||||||
@@ -241,11 +248,19 @@ namespace SpineViewer
|
|||||||
}
|
}
|
||||||
|
|
||||||
var spine = spines[i];
|
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
|
try
|
||||||
{
|
{
|
||||||
var preview = spine.GetPreview(width, height);
|
using (var img = tex.Texture.CopyToImage())
|
||||||
var savePath = Path.Combine(outputDir, $"{spine.Name}.png");
|
{
|
||||||
preview.SaveToFile(savePath);
|
img.SaveToFile(Path.Combine(outputDir, $"{spine.Name}.png"));
|
||||||
|
}
|
||||||
success++;
|
success++;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -49,9 +49,14 @@ namespace SpineViewer.Spine
|
|||||||
public const string EMPTY_ANIMATION = "<Empty>";
|
public const string EMPTY_ANIMATION = "<Empty>";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 预览图大小
|
/// 预览图宽
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly Size PREVIEW_SIZE = new(256, 256);
|
public const uint PREVIEW_WIDTH = 256;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 预览图高
|
||||||
|
/// </summary>
|
||||||
|
public const uint PREVIEW_HEIGHT = 256;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 缩放最小值
|
/// 缩放最小值
|
||||||
@@ -366,7 +371,15 @@ namespace SpineViewer.Spine
|
|||||||
{
|
{
|
||||||
if (preview is null)
|
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");
|
img.SaveToMemory(out var imgBuffer, "bmp");
|
||||||
using var stream = new MemoryStream(imgBuffer);
|
using var stream = new MemoryStream(imgBuffer);
|
||||||
preview = new Bitmap(stream);
|
preview = new Bitmap(stream);
|
||||||
@@ -376,23 +389,6 @@ namespace SpineViewer.Spine
|
|||||||
}
|
}
|
||||||
private Image preview = null;
|
private Image preview = null;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取指定尺寸的预览图
|
|
||||||
/// </summary>
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取动画时长, 如果动画不存在则返回 0
|
/// 获取动画时长, 如果动画不存在则返回 0
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user