Files
community/Ink Canvas/MainWindow_cs/MW_Screenshot.cs
T

85 lines
3.1 KiB
C#
Raw Normal View History

2025-06-08 23:13:30 +08:00
using System;
2025-07-28 14:40:44 +08:00
using System.Drawing;
2025-05-25 09:29:48 +08:00
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
2025-07-28 14:40:44 +08:00
using System.Windows.Forms;
2025-05-25 09:29:48 +08:00
namespace Ink_Canvas {
public partial class MainWindow : Window {
private void SaveScreenShot(bool isHideNotification, string fileName = null) {
var savePath = Settings.Automation.IsSaveScreenshotsInDateFolders
? GetDateFolderPath(fileName)
: GetDefaultFolderPath();
CaptureAndSaveScreenshot(savePath, isHideNotification);
if (Settings.Automation.IsAutoSaveStrokesAtScreenshot)
2025-07-28 14:40:44 +08:00
SaveInkCanvasStrokes(false);
2025-05-25 09:29:48 +08:00
}
private void SaveScreenShotToDesktop() {
var desktopPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
$"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.png");
CaptureAndSaveScreenshot(desktopPath, false);
if (Settings.Automation.IsAutoSaveStrokesAtScreenshot)
2025-07-28 14:40:44 +08:00
SaveInkCanvasStrokes(false);
2025-05-25 09:29:48 +08:00
}
// 提取公共的截图和保存逻辑
private void CaptureAndSaveScreenshot(string savePath, bool isHideNotification) {
2025-07-28 14:40:44 +08:00
var rc = SystemInformation.VirtualScreen;
2025-05-25 09:29:48 +08:00
2025-07-28 14:40:44 +08:00
using (var bitmap = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb))
using (var memoryGraphics = Graphics.FromImage(bitmap)) {
memoryGraphics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
2025-05-25 09:29:48 +08:00
2025-07-12 09:00:51 +08:00
// 确保目录存在
2025-05-25 09:29:48 +08:00
var directory = Path.GetDirectoryName(savePath);
if (!Directory.Exists(directory)) {
Directory.CreateDirectory(directory);
}
2025-07-12 09:00:51 +08:00
bitmap.Save(savePath, ImageFormat.Png);
2025-05-25 09:29:48 +08:00
}
if (!isHideNotification) {
2025-07-12 09:00:51 +08:00
ShowNotification($"截图成功保存至 {savePath}");
2025-05-25 09:29:48 +08:00
}
}
// 获取日期文件夹路径
private string GetDateFolderPath(string fileName) {
2025-07-12 09:00:51 +08:00
if (string.IsNullOrWhiteSpace(fileName)) {
fileName = DateTime.Now.ToString("HH-mm-ss");
2025-06-08 23:13:30 +08:00
}
2025-07-12 09:00:51 +08:00
var basePath = Settings.Automation.AutoSavedStrokesLocation;
var dateFolder = DateTime.Now.ToString("yyyyMMdd");
return Path.Combine(
basePath,
"Auto Saved - Screenshots",
dateFolder,
$"{fileName}.png");
2025-05-25 09:29:48 +08:00
}
2025-07-12 09:00:51 +08:00
// 获取默认文件夹路径
2025-05-25 09:29:48 +08:00
private string GetDefaultFolderPath() {
2025-07-12 09:00:51 +08:00
var basePath = Settings.Automation.AutoSavedStrokesLocation;
2025-05-25 09:29:48 +08:00
var screenshotsFolder = Path.Combine(basePath, "Auto Saved - Screenshots");
2025-07-12 09:00:51 +08:00
if (!Directory.Exists(screenshotsFolder)) {
2025-05-25 09:29:48 +08:00
Directory.CreateDirectory(screenshotsFolder);
}
return Path.Combine(
screenshotsFolder,
$"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.png");
}
}
2025-07-12 09:00:51 +08:00
}