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

104 lines
3.6 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
2025-08-27 17:43:04 +08:00
namespace Ink_Canvas
{
public partial class MainWindow : Window
{
private void SaveScreenShot(bool isHideNotification, string fileName = null)
{
2025-05-25 09:29:48 +08:00
var savePath = Settings.Automation.IsSaveScreenshotsInDateFolders
? GetDateFolderPath(fileName)
: GetDefaultFolderPath();
CaptureAndSaveScreenshot(savePath, isHideNotification);
2025-08-27 17:43:04 +08:00
if (Settings.Automation.IsAutoSaveStrokesAtScreenshot)
2025-07-28 14:40:44 +08:00
SaveInkCanvasStrokes(false);
2025-05-25 09:29:48 +08:00
}
2025-08-27 17:43:04 +08:00
internal void SaveScreenShotToDesktop()
{
2025-05-25 09:29:48 +08:00
var desktopPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
$"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.png");
CaptureAndSaveScreenshot(desktopPath, false);
2025-08-27 17:43:04 +08:00
if (Settings.Automation.IsAutoSaveStrokesAtScreenshot)
2025-07-28 14:40:44 +08:00
SaveInkCanvasStrokes(false);
2025-05-25 09:29:48 +08:00
}
// 提取公共的截图和保存逻辑
2025-08-27 17:43:04 +08:00
private void CaptureAndSaveScreenshot(string savePath, bool isHideNotification)
{
2025-07-28 14:40:44 +08:00
var rc = SystemInformation.VirtualScreen;
2025-08-27 17:43:04 +08:00
2025-07-28 14:40:44 +08:00
using (var bitmap = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb))
2025-08-27 17:43:04 +08:00
using (var memoryGraphics = Graphics.FromImage(bitmap))
{
// 设置高质量渲染
memoryGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
memoryGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
memoryGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
memoryGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
2025-07-28 14:40:44 +08:00
memoryGraphics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
2025-08-27 17:43:04 +08:00
2025-07-12 09:00:51 +08:00
// 确保目录存在
2025-05-25 09:29:48 +08:00
var directory = Path.GetDirectoryName(savePath);
2025-08-27 17:43:04 +08:00
if (!Directory.Exists(directory))
{
2025-05-25 09:29:48 +08:00
Directory.CreateDirectory(directory);
}
2025-08-27 17:43:04 +08:00
// 使用PNG格式保存,确保透明度信息不丢失
2025-07-12 09:00:51 +08:00
bitmap.Save(savePath, ImageFormat.Png);
2025-05-25 09:29:48 +08:00
}
2025-08-27 17:43:04 +08:00
if (!isHideNotification)
{
2025-07-12 09:00:51 +08:00
ShowNotification($"截图成功保存至 {savePath}");
2025-05-25 09:29:48 +08:00
}
}
// 获取日期文件夹路径
2025-08-27 17:43:04 +08:00
private string GetDateFolderPath(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
{
2025-07-12 09:00:51 +08:00
fileName = DateTime.Now.ToString("HH-mm-ss");
2025-06-08 23:13:30 +08:00
}
2025-08-27 17:43:04 +08:00
2025-07-12 09:00:51 +08:00
var basePath = Settings.Automation.AutoSavedStrokesLocation;
var dateFolder = DateTime.Now.ToString("yyyyMMdd");
2025-08-27 17:43:04 +08:00
2025-07-12 09:00:51 +08:00
return Path.Combine(
2025-08-27 17:43:04 +08:00
basePath,
"Auto Saved - Screenshots",
dateFolder,
2025-07-12 09:00:51 +08:00
$"{fileName}.png");
2025-05-25 09:29:48 +08:00
}
2025-07-12 09:00:51 +08:00
// 获取默认文件夹路径
2025-08-27 17:43:04 +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-08-27 17:43:04 +08:00
if (!Directory.Exists(screenshotsFolder))
{
2025-05-25 09:29:48 +08:00
Directory.CreateDirectory(screenshotsFolder);
}
2025-08-27 17:43:04 +08:00
2025-05-25 09:29:48 +08:00
return Path.Combine(
2025-08-27 17:43:04 +08:00
screenshotsFolder,
2025-05-25 09:29:48 +08:00
$"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.png");
}
}
2025-07-12 09:00:51 +08:00
}