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

432 lines
17 KiB
C#
Raw Normal View History

2025-08-03 16:46:33 +08:00
using Ink_Canvas.Helpers;
2025-06-08 23:13:30 +08:00
using System;
2025-07-31 10:27:19 +08:00
using System.Collections.Generic;
2025-07-28 14:40:44 +08:00
using System.Drawing;
2025-07-31 10:27:19 +08:00
using System.Drawing.Drawing2D;
2025-05-25 09:29:48 +08:00
using System.Drawing.Imaging;
using System.IO;
2025-07-30 19:56:22 +08:00
using System.Threading.Tasks;
2025-05-25 09:29:48 +08:00
using System.Windows;
2025-07-28 14:40:44 +08:00
using System.Windows.Forms;
2025-07-30 19:56:22 +08:00
using System.Windows.Media.Imaging;
2025-07-30 20:06:43 +08:00
using Application = System.Windows.Application;
2025-07-30 19:56:22 +08:00
using Clipboard = System.Windows.Clipboard;
2025-07-30 20:06:43 +08:00
using Size = System.Drawing.Size;
2025-05-25 09:29:48 +08:00
2025-08-03 16:46:33 +08:00
namespace Ink_Canvas
{
2025-07-31 10:27:19 +08:00
// 截图结果结构体
public struct ScreenshotResult
{
public System.Drawing.Rectangle Area;
public List<System.Windows.Point> Path;
2025-08-03 16:46:33 +08:00
2025-07-31 10:27:19 +08:00
public ScreenshotResult(System.Drawing.Rectangle area, List<System.Windows.Point> path = null)
{
Area = area;
Path = path;
}
}
2025-08-03 16:46:33 +08:00
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-03 16:46:33 +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-03 16:46:33 +08:00
private 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-03 16:46:33 +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-03 16:46:33 +08:00
private void CaptureAndSaveScreenshot(string savePath, bool isHideNotification)
{
2025-07-28 14:40:44 +08:00
var rc = SystemInformation.VirtualScreen;
2025-08-03 16:46:33 +08:00
2025-07-28 14:40:44 +08:00
using (var bitmap = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb))
2025-08-03 16:46:33 +08:00
using (var memoryGraphics = Graphics.FromImage(bitmap))
{
2025-08-10 11:58:58 +08:00
// 设置高质量渲染
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-03 16:46:33 +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-03 16:46:33 +08:00
if (!Directory.Exists(directory))
{
2025-05-25 09:29:48 +08:00
Directory.CreateDirectory(directory);
}
2025-08-03 16:46:33 +08:00
2025-08-10 11:58:58 +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-03 16:46:33 +08:00
if (!isHideNotification)
{
2025-07-12 09:00:51 +08:00
ShowNotification($"截图成功保存至 {savePath}");
2025-05-25 09:29:48 +08:00
}
}
// 获取日期文件夹路径
2025-08-03 16:46:33 +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-03 16:46:33 +08:00
2025-07-12 09:00:51 +08:00
var basePath = Settings.Automation.AutoSavedStrokesLocation;
var dateFolder = DateTime.Now.ToString("yyyyMMdd");
2025-08-03 16:46:33 +08:00
2025-07-12 09:00:51 +08:00
return Path.Combine(
2025-08-03 16:46:33 +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-03 16:46:33 +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-30 19:56:22 +08:00
2025-08-03 16:46:33 +08:00
if (!Directory.Exists(screenshotsFolder))
{
2025-05-25 09:29:48 +08:00
Directory.CreateDirectory(screenshotsFolder);
}
2025-07-30 19:56:22 +08:00
2025-05-25 09:29:48 +08:00
return Path.Combine(
2025-07-30 19:56:22 +08:00
screenshotsFolder,
2025-05-25 09:29:48 +08:00
$"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.png");
}
2025-07-30 19:56:22 +08:00
// 截图并复制到剪贴板
2025-08-03 16:46:33 +08:00
private async Task CaptureScreenshotToClipboard()
{
try
{
2025-07-30 19:56:22 +08:00
// 隐藏主窗口以避免截图包含窗口本身
var originalVisibility = this.Visibility;
this.Visibility = Visibility.Hidden;
// 等待窗口隐藏
await Task.Delay(200);
2025-07-30 20:06:43 +08:00
// 启动区域选择截图
2025-07-31 10:27:19 +08:00
var screenshotResult = await ShowScreenshotSelector();
2025-07-30 20:06:43 +08:00
// 恢复窗口显示
this.Visibility = originalVisibility;
2025-07-31 10:27:19 +08:00
if (screenshotResult.HasValue && screenshotResult.Value.Area.Width > 0 && screenshotResult.Value.Area.Height > 0)
2025-07-30 20:06:43 +08:00
{
// 截取选定区域
2025-07-31 10:27:19 +08:00
using (var originalBitmap = CaptureScreenArea(screenshotResult.Value.Area))
2025-07-30 20:06:43 +08:00
{
2025-07-31 10:27:19 +08:00
if (originalBitmap != null)
2025-07-30 20:06:43 +08:00
{
2025-07-31 10:27:19 +08:00
Bitmap finalBitmap = originalBitmap;
2025-08-10 11:58:58 +08:00
bool needDisposeFinalBitmap = false;
2025-08-03 16:46:33 +08:00
2025-08-10 11:58:58 +08:00
try
2025-07-31 10:27:19 +08:00
{
2025-08-10 11:58:58 +08:00
// 如果有路径信息,应用形状遮罩
if (screenshotResult.Value.Path != null && screenshotResult.Value.Path.Count > 0)
{
finalBitmap = ApplyShapeMask(originalBitmap, screenshotResult.Value.Path, screenshotResult.Value.Area);
needDisposeFinalBitmap = true; // 标记需要释放新创建的位图
}
// 将截图复制到剪贴板
CopyBitmapToClipboard(finalBitmap);
// 等待窗口完全显示后自动粘贴
await Task.Delay(100);
await AutoPasteScreenshot();
}
finally
{
// 如果创建了新的位图,需要释放它
if (needDisposeFinalBitmap && finalBitmap != originalBitmap)
{
finalBitmap.Dispose();
}
2025-07-31 10:27:19 +08:00
}
2025-07-30 20:06:43 +08:00
}
}
}
else
{
ShowNotification("截图已取消");
2025-07-30 19:56:22 +08:00
}
}
2025-08-03 16:46:33 +08:00
catch (Exception ex)
{
2025-07-30 19:56:22 +08:00
ShowNotification($"截图失败: {ex.Message}");
this.Visibility = Visibility.Visible;
}
}
2025-07-30 20:06:43 +08:00
// 显示截图区域选择器
2025-07-31 10:27:19 +08:00
private async Task<ScreenshotResult?> ShowScreenshotSelector()
2025-07-30 20:06:43 +08:00
{
2025-07-31 10:27:19 +08:00
ScreenshotResult? result = null;
2025-07-30 20:06:43 +08:00
try
{
await Application.Current.Dispatcher.InvokeAsync(() =>
{
var selectorWindow = new ScreenshotSelectorWindow();
if (selectorWindow.ShowDialog() == true)
{
2025-07-31 10:27:19 +08:00
result = new ScreenshotResult(
selectorWindow.SelectedArea.Value,
selectorWindow.SelectedPath
);
2025-07-30 20:06:43 +08:00
}
});
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"显示截图选择器失败: {ex.Message}", LogHelper.LogType.Error);
}
2025-07-31 10:27:19 +08:00
return result;
2025-07-30 20:06:43 +08:00
}
// 截取指定屏幕区域
2025-07-31 10:27:19 +08:00
private Bitmap CaptureScreenArea(System.Drawing.Rectangle area)
2025-07-30 20:06:43 +08:00
{
try
{
// 确保区域在有效范围内
var virtualScreen = SystemInformation.VirtualScreen;
// 调整区域边界,确保不超出屏幕范围
int x = Math.Max(area.X, virtualScreen.X);
int y = Math.Max(area.Y, virtualScreen.Y);
int right = Math.Min(area.Right, virtualScreen.Right);
int bottom = Math.Min(area.Bottom, virtualScreen.Bottom);
int width = Math.Max(1, right - x);
int height = Math.Max(1, bottom - y);
2025-08-10 11:58:58 +08:00
// 创建支持透明度的位图
2025-07-30 20:06:43 +08:00
var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
using (var graphics = Graphics.FromImage(bitmap))
{
// 设置高质量渲染
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
2025-08-10 11:58:58 +08:00
graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
2025-07-30 20:06:43 +08:00
// 截取屏幕区域
graphics.CopyFromScreen(x, y, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
}
LogHelper.WriteLogToFile($"成功截取区域: X={x}, Y={y}, Width={width}, Height={height}", LogHelper.LogType.Info);
return bitmap;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"截取屏幕区域失败: {ex.Message}", LogHelper.LogType.Error);
return null;
}
}
2025-07-30 19:56:22 +08:00
// 自动粘贴截图到画布
2025-08-03 16:46:33 +08:00
private async Task AutoPasteScreenshot()
{
try
{
2025-07-30 19:56:22 +08:00
// 只在白板模式下自动粘贴
2025-08-03 16:46:33 +08:00
if (currentMode == 1)
{
2025-07-30 19:56:22 +08:00
await PasteImageFromClipboard();
ShowNotification("截图已自动插入到画布");
2025-08-03 16:46:33 +08:00
}
else
{
2025-07-30 19:56:22 +08:00
ShowNotification("截图已复制到剪贴板,可在白板模式下粘贴");
}
}
2025-08-03 16:46:33 +08:00
catch (Exception ex)
{
2025-07-30 19:56:22 +08:00
ShowNotification($"自动粘贴截图失败: {ex.Message}");
LogHelper.WriteLogToFile($"自动粘贴截图失败: {ex.Message}", LogHelper.LogType.Error);
}
}
// 将Bitmap复制到剪贴板
2025-08-03 16:46:33 +08:00
private void CopyBitmapToClipboard(Bitmap bitmap)
{
try
{
2025-07-30 19:56:22 +08:00
// 将System.Drawing.Bitmap转换为WPF BitmapSource
var bitmapSource = ConvertBitmapToBitmapSource(bitmap);
// 复制到剪贴板
Clipboard.SetImage(bitmapSource);
}
2025-08-03 16:46:33 +08:00
catch (Exception ex)
{
2025-07-30 19:56:22 +08:00
ShowNotification($"复制到剪贴板失败: {ex.Message}");
}
}
2025-07-31 10:27:19 +08:00
// 应用形状遮罩到截图
private Bitmap ApplyShapeMask(Bitmap bitmap, List<System.Windows.Point> path, System.Drawing.Rectangle area)
{
try
{
2025-08-10 11:58:58 +08:00
// 验证路径参数
if (path == null || path.Count < 3)
{
LogHelper.WriteLogToFile("路径点数不足,无法应用形状遮罩", LogHelper.LogType.Warning);
return bitmap;
}
2025-07-31 10:55:01 +08:00
// 获取DPI缩放比例
var dpiScale = GetDpiScale();
var virtualScreen = SystemInformation.VirtualScreen;
2025-08-03 16:46:33 +08:00
2025-08-10 11:58:58 +08:00
// 创建结果位图,确保支持透明度
2025-07-31 10:55:01 +08:00
var resultBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);
2025-08-10 11:58:58 +08:00
// 首先将整个位图设置为透明
2025-07-31 10:55:01 +08:00
using (var resultGraphics = Graphics.FromImage(resultBitmap))
2025-07-31 10:27:19 +08:00
{
2025-08-10 11:58:58 +08:00
// 清除位图,设置为完全透明
resultGraphics.Clear(System.Drawing.Color.Transparent);
2025-07-31 10:27:19 +08:00
// 设置高质量渲染
2025-07-31 10:55:01 +08:00
resultGraphics.SmoothingMode = SmoothingMode.AntiAlias;
resultGraphics.CompositingQuality = CompositingQuality.HighQuality;
2025-08-10 11:58:58 +08:00
resultGraphics.CompositingMode = CompositingMode.SourceOver;
2025-07-31 10:27:19 +08:00
// 创建路径
using (var pathGraphics = new GraphicsPath())
{
2025-07-31 10:55:01 +08:00
// 转换WPF坐标到GDI+坐标,考虑DPI缩放和屏幕偏移
2025-07-31 10:27:19 +08:00
var points = new PointF[path.Count];
for (int i = 0; i < path.Count; i++)
{
2025-07-31 10:55:01 +08:00
// 将WPF坐标转换为实际屏幕坐标,然后相对于截图区域计算偏移
double screenX = (path[i].X * dpiScale) + virtualScreen.Left;
double screenY = (path[i].Y * dpiScale) + virtualScreen.Top;
2025-08-03 16:46:33 +08:00
2025-07-31 10:55:01 +08:00
// 计算相对于截图区域的坐标
float relativeX = (float)(screenX - area.X);
float relativeY = (float)(screenY - area.Y);
2025-08-03 16:46:33 +08:00
2025-08-10 11:58:58 +08:00
// 确保坐标在有效范围内
relativeX = Math.Max(0, Math.Min(relativeX, bitmap.Width - 1));
relativeY = Math.Max(0, Math.Min(relativeY, bitmap.Height - 1));
2025-07-31 10:55:01 +08:00
points[i] = new PointF(relativeX, relativeY);
2025-07-31 10:27:19 +08:00
}
2025-08-10 11:58:58 +08:00
// 添加路径 - 使用FillMode.Winding确保路径正确填充
pathGraphics.FillMode = FillMode.Winding;
2025-07-31 10:27:19 +08:00
pathGraphics.AddPolygon(points);
2025-08-10 11:58:58 +08:00
// 验证路径是否有效
if (!pathGraphics.IsVisible(0, 0) && pathGraphics.GetBounds().Width > 0 && pathGraphics.GetBounds().Height > 0)
{
// 设置裁剪区域为路径内部
resultGraphics.SetClip(pathGraphics);
// 在裁剪区域内绘制原始图像
resultGraphics.DrawImage(bitmap, 0, 0);
// 重置裁剪区域,确保后续操作不受影响
resultGraphics.ResetClip();
}
else
{
LogHelper.WriteLogToFile("生成的路径无效,返回原始图像", LogHelper.LogType.Warning);
// 如果路径无效,返回透明图像
return resultBitmap;
}
2025-07-31 10:27:19 +08:00
}
}
2025-07-31 10:55:01 +08:00
2025-08-10 11:58:58 +08:00
LogHelper.WriteLogToFile($"成功应用形状遮罩,路径点数: {path.Count}", LogHelper.LogType.Info);
2025-07-31 10:55:01 +08:00
return resultBitmap;
2025-07-31 10:27:19 +08:00
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"应用形状遮罩失败: {ex.Message}", LogHelper.LogType.Error);
2025-08-10 11:58:58 +08:00
// 返回完全透明的图像而不是原始图像
var transparentBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(transparentBitmap))
{
g.Clear(System.Drawing.Color.Transparent);
}
return transparentBitmap;
2025-07-31 10:27:19 +08:00
}
}
2025-07-31 10:55:01 +08:00
// 获取DPI缩放比例
private double GetDpiScale()
{
var source = PresentationSource.FromVisual(this);
if (source?.CompositionTarget != null)
{
return source.CompositionTarget.TransformToDevice.M11;
}
return 1.0; // 默认DPI
}
2025-07-30 19:56:22 +08:00
// 将System.Drawing.Bitmap转换为WPF BitmapSource
2025-08-03 16:46:33 +08:00
private BitmapSource ConvertBitmapToBitmapSource(Bitmap bitmap)
{
2025-08-10 11:58:58 +08:00
try
2025-08-03 16:46:33 +08:00
{
2025-08-10 11:58:58 +08:00
using (var memory = new MemoryStream())
{
// 使用PNG格式保存,确保透明度信息不丢失
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"转换位图失败: {ex.Message}", LogHelper.LogType.Error);
throw;
2025-07-30 19:56:22 +08:00
}
}
2025-05-25 09:29:48 +08:00
}
2025-07-12 09:00:51 +08:00
}