This commit is contained in:
CJK_mkp
2025-06-08 23:13:30 +08:00
parent 57317be766
commit c2f2f3a9af
38 changed files with 394 additions and 269 deletions
+64 -12
View File
@@ -1,6 +1,7 @@
using System;
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
namespace Ink_Canvas {
@@ -35,17 +36,49 @@ namespace Ink_Canvas {
using (var memoryGraphics = System.Drawing.Graphics.FromImage(bitmap)) {
memoryGraphics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, System.Drawing.CopyPixelOperation.SourceCopy);
// 确保目录存在
var directory = Path.GetDirectoryName(savePath);
if (!Directory.Exists(directory)) {
Directory.CreateDirectory(directory);
}
bitmap.Save(savePath, ImageFormat.Png);
try {
// 新增双重目录检查
Directory.CreateDirectory(directory); // 防止多线程场景下的竞争条件
bitmap.Save(savePath, ImageFormat.Png);
}
catch (Exception ex) when (ex is IOException ||
ex is UnauthorizedAccessException ||
ex is ExternalException) { // 新增GDI+异常捕获
// 改进备用路径处理
var docPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"Auto Saved - Screenshots",
DateTime.Now.ToString("yyyyMMdd"),
Path.GetFileNameWithoutExtension(savePath) + "_retry.png"); // 添加重试后缀
try {
var docDir = Path.GetDirectoryName(docPath);
Directory.CreateDirectory(docDir);
bitmap.Save(docPath, ImageFormat.Png);
savePath = docPath;
}
catch (Exception fallbackEx) {
// 最终错误处理
if (!isHideNotification) {
ShowNotification($"截图保存失败: {fallbackEx.Message}");
}
return;
}
}
}
if (!isHideNotification) {
ShowNotification($"截图成功保存至 {savePath}");
try {
ShowNotification($"截图成功保存至 {savePath}");
}
catch {
// 防止通知系统自身异常导致崩溃
}
}
}
@@ -57,20 +90,39 @@ namespace Ink_Canvas {
var basePath = Settings.Automation.AutoSavedStrokesLocation;
var dateFolder = DateTime.Now.ToString("yyyyMMdd");
return Path.Combine(
var fullPath = Path.Combine(
basePath,
"Auto Saved - Screenshots",
dateFolder,
$"{fileName}.png");
dateFolder);
try {
if (!Directory.Exists(fullPath)) {
Directory.CreateDirectory(fullPath);
}
}
catch (Exception) {
// 如果创建失败则使用文档目录
basePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
fullPath = Path.Combine(basePath, "Auto Saved - Screenshots", dateFolder);
Directory.CreateDirectory(fullPath);
}
return Path.Combine(fullPath, $"{fileName}.png");
}
// 获取默认文件夹路径
private string GetDefaultFolderPath() {
var basePath = Settings.Automation.AutoSavedStrokesLocation;
var screenshotsFolder = Path.Combine(basePath, "Auto Saved - Screenshots");
if (!Directory.Exists(screenshotsFolder)) {
try {
if (!Directory.Exists(screenshotsFolder)) {
Directory.CreateDirectory(screenshotsFolder);
}
}
catch (Exception) {
// 如果创建失败则使用文档目录
basePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
screenshotsFolder = Path.Combine(basePath, "Auto Saved - Screenshots");
Directory.CreateDirectory(screenshotsFolder);
}