update: 加快startup图像合并过程

This commit is contained in:
edwin 2023-08-29 19:25:34 +08:00 committed by ZouJin
parent 8819235548
commit 0cdcfe56d9

View File

@ -64,7 +64,6 @@ namespace VPet_Simulator.Core
/// 反正一次性生成太多导致闪退 /// 反正一次性生成太多导致闪退
/// </summary> /// </summary>
public static int NowLoading = 0; public static int NowLoading = 0;
private static object nowLoadinglock = new object();
/// <summary> /// <summary>
/// 新建 PNG 动画 /// 新建 PNG 动画
/// </summary> /// </summary>
@ -172,53 +171,53 @@ namespace VPet_Simulator.Core
{ {
Thread.Sleep(100); Thread.Sleep(100);
} }
lock (nowLoadinglock) Interlocked.Increment(ref NowLoading);
NowLoading++;
//新方法:加载大图片 //新方法:加载大图片
//生成大文件加载非常慢,先看看有没有缓存能用 //生成大文件加载非常慢,先看看有没有缓存能用
Path = GraphCore.CachePath + $"\\{GraphCore.Resolution}_{Sub.GetHashCode(path)}_{paths.Length}.png"; Path = System.IO.Path.Combine(GraphCore.CachePath, $"{GraphCore.Resolution}_{Sub.GetHashCode(path)}_{paths.Length}.png");
Width = 500 * (paths.Length + 1); Width = 500 * (paths.Length + 1);
if (File.Exists(Path) || ((List<string>)GraphCore.CommConfig["Cache"]).Contains(path)) if (!File.Exists(Path) && !((List<string>)GraphCore.CommConfig["Cache"]).Contains(path))
{
for (int i = 0; i < paths.Length; i++)
{
FileInfo file = paths[i];
int time = int.Parse(file.Name.Split('.').Reverse().ToArray()[1].Split('_').Last());
Animations.Add(new Animation(this, time, -500 * i));
}
}
else
{ {
((List<string>)GraphCore.CommConfig["Cache"]).Add(path); ((List<string>)GraphCore.CommConfig["Cache"]).Add(path);
List<System.Drawing.Image> imgs = new List<System.Drawing.Image>(); int w = 0;
foreach (var file in paths) int h = 0;
imgs.Add(System.Drawing.Image.FromFile(file.FullName)); FileInfo firstImage = paths[0];
int w = imgs[0].Width; var img = System.Drawing.Image.FromFile(firstImage.FullName);
int h = imgs[0].Height; w = img.Width;
if (w > GraphCore.Resolution) h = img.Height;
if (w > 1000)
{ {
w = GraphCore.Resolution; w = 1000;
h = (int)(h * (GraphCore.Resolution / (double)imgs[0].Width)); h = (int)(h * (1000 / (double)img.Width));
} }
Bitmap joinedBitmap = new Bitmap(w * paths.Length, h);
var graph = Graphics.FromImage(joinedBitmap); using (Bitmap joinedBitmap = new Bitmap(w * paths.Length, h))
for (int i = 0; i < paths.Length; i++) using (Graphics graph = Graphics.FromImage(joinedBitmap))
{ {
FileInfo file = paths[i]; using (img)
int time = int.Parse(file.Name.Split('.').Reverse().ToArray()[1].Split('_').Last()); graph.DrawImage(img, 0, 0, w, h);
graph.DrawImage(imgs[i], w * i, 0, w, h); Parallel.For(1, paths.Length, i =>
Animations.Add(new Animation(this, time, -500 * i)); {
using (var img = System.Drawing.Image.FromFile(paths[i].FullName))
{
lock (graph)
graph.DrawImage(img, w * i, 0, w, h);
}
});
if (!File.Exists(Path))
joinedBitmap.Save(Path);
} }
if (!File.Exists(Path)) }
joinedBitmap.Save(Path); for (int i = 0; i < paths.Length; i++)
graph.Dispose(); {
joinedBitmap.Dispose(); var noExtFileName = System.IO.Path.GetFileNameWithoutExtension(paths[i].Name);
imgs.ForEach(x => x.Dispose()); int time = int.Parse(noExtFileName.Substring(noExtFileName.LastIndexOf('_') + 1));
Animations.Add(new Animation(this, time, -500 * i));
} }
//stream = new MemoryStream(File.ReadAllBytes(cp)); //stream = new MemoryStream(File.ReadAllBytes(cp));
IsReady = true; IsReady = true;
lock (nowLoadinglock) Interlocked.Decrement(ref NowLoading);
NowLoading--;
} }