代码清理
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Ink_Canvas.Helpers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
@@ -8,93 +9,103 @@ using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Media.Imaging;
|
||||
using Ink_Canvas.Helpers;
|
||||
using Ink_Canvas.Helpers;
|
||||
using Application = System.Windows.Application;
|
||||
using Clipboard = System.Windows.Clipboard;
|
||||
using Size = System.Drawing.Size;
|
||||
|
||||
namespace Ink_Canvas {
|
||||
namespace Ink_Canvas
|
||||
{
|
||||
// 截图结果结构体
|
||||
public struct ScreenshotResult
|
||||
{
|
||||
public System.Drawing.Rectangle Area;
|
||||
public List<System.Windows.Point> Path;
|
||||
|
||||
|
||||
public ScreenshotResult(System.Drawing.Rectangle area, List<System.Windows.Point> path = null)
|
||||
{
|
||||
Area = area;
|
||||
Path = path;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class MainWindow : Window {
|
||||
private void SaveScreenShot(bool isHideNotification, string fileName = null) {
|
||||
|
||||
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)
|
||||
|
||||
if (Settings.Automation.IsAutoSaveStrokesAtScreenshot)
|
||||
SaveInkCanvasStrokes(false);
|
||||
}
|
||||
|
||||
private void SaveScreenShotToDesktop() {
|
||||
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)
|
||||
|
||||
if (Settings.Automation.IsAutoSaveStrokesAtScreenshot)
|
||||
SaveInkCanvasStrokes(false);
|
||||
}
|
||||
|
||||
// 提取公共的截图和保存逻辑
|
||||
private void CaptureAndSaveScreenshot(string savePath, bool isHideNotification) {
|
||||
private void CaptureAndSaveScreenshot(string savePath, bool isHideNotification)
|
||||
{
|
||||
var rc = SystemInformation.VirtualScreen;
|
||||
|
||||
|
||||
using (var bitmap = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb))
|
||||
using (var memoryGraphics = Graphics.FromImage(bitmap)) {
|
||||
using (var memoryGraphics = Graphics.FromImage(bitmap))
|
||||
{
|
||||
memoryGraphics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
|
||||
|
||||
|
||||
// 确保目录存在
|
||||
var directory = Path.GetDirectoryName(savePath);
|
||||
if (!Directory.Exists(directory)) {
|
||||
if (!Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
|
||||
bitmap.Save(savePath, ImageFormat.Png);
|
||||
}
|
||||
|
||||
if (!isHideNotification) {
|
||||
|
||||
if (!isHideNotification)
|
||||
{
|
||||
ShowNotification($"截图成功保存至 {savePath}");
|
||||
}
|
||||
}
|
||||
|
||||
// 获取日期文件夹路径
|
||||
private string GetDateFolderPath(string fileName) {
|
||||
if (string.IsNullOrWhiteSpace(fileName)) {
|
||||
private string GetDateFolderPath(string fileName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(fileName))
|
||||
{
|
||||
fileName = DateTime.Now.ToString("HH-mm-ss");
|
||||
}
|
||||
|
||||
|
||||
var basePath = Settings.Automation.AutoSavedStrokesLocation;
|
||||
var dateFolder = DateTime.Now.ToString("yyyyMMdd");
|
||||
|
||||
|
||||
return Path.Combine(
|
||||
basePath,
|
||||
"Auto Saved - Screenshots",
|
||||
dateFolder,
|
||||
basePath,
|
||||
"Auto Saved - Screenshots",
|
||||
dateFolder,
|
||||
$"{fileName}.png");
|
||||
}
|
||||
|
||||
// 获取默认文件夹路径
|
||||
private string GetDefaultFolderPath() {
|
||||
private string GetDefaultFolderPath()
|
||||
{
|
||||
var basePath = Settings.Automation.AutoSavedStrokesLocation;
|
||||
var screenshotsFolder = Path.Combine(basePath, "Auto Saved - Screenshots");
|
||||
|
||||
if (!Directory.Exists(screenshotsFolder)) {
|
||||
if (!Directory.Exists(screenshotsFolder))
|
||||
{
|
||||
Directory.CreateDirectory(screenshotsFolder);
|
||||
}
|
||||
|
||||
@@ -104,8 +115,10 @@ namespace Ink_Canvas {
|
||||
}
|
||||
|
||||
// 截图并复制到剪贴板
|
||||
private async Task CaptureScreenshotToClipboard() {
|
||||
try {
|
||||
private async Task CaptureScreenshotToClipboard()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 隐藏主窗口以避免截图包含窗口本身
|
||||
var originalVisibility = this.Visibility;
|
||||
this.Visibility = Visibility.Hidden;
|
||||
@@ -127,13 +140,13 @@ namespace Ink_Canvas {
|
||||
if (originalBitmap != null)
|
||||
{
|
||||
Bitmap finalBitmap = originalBitmap;
|
||||
|
||||
|
||||
// 如果有路径信息,应用形状遮罩
|
||||
if (screenshotResult.Value.Path != null && screenshotResult.Value.Path.Count > 0)
|
||||
{
|
||||
finalBitmap = ApplyShapeMask(originalBitmap, screenshotResult.Value.Path, screenshotResult.Value.Area);
|
||||
}
|
||||
|
||||
|
||||
// 将截图复制到剪贴板
|
||||
CopyBitmapToClipboard(finalBitmap);
|
||||
|
||||
@@ -148,7 +161,8 @@ namespace Ink_Canvas {
|
||||
ShowNotification("截图已取消");
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowNotification($"截图失败: {ex.Message}");
|
||||
this.Visibility = Visibility.Visible;
|
||||
}
|
||||
@@ -221,32 +235,41 @@ namespace Ink_Canvas {
|
||||
}
|
||||
|
||||
// 自动粘贴截图到画布
|
||||
private async Task AutoPasteScreenshot() {
|
||||
try {
|
||||
private async Task AutoPasteScreenshot()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 只在白板模式下自动粘贴
|
||||
if (currentMode == 1) {
|
||||
if (currentMode == 1)
|
||||
{
|
||||
await PasteImageFromClipboard();
|
||||
ShowNotification("截图已自动插入到画布");
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowNotification("截图已复制到剪贴板,可在白板模式下粘贴");
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowNotification($"自动粘贴截图失败: {ex.Message}");
|
||||
LogHelper.WriteLogToFile($"自动粘贴截图失败: {ex.Message}", LogHelper.LogType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
// 将Bitmap复制到剪贴板
|
||||
private void CopyBitmapToClipboard(Bitmap bitmap) {
|
||||
try {
|
||||
private void CopyBitmapToClipboard(Bitmap bitmap)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 将System.Drawing.Bitmap转换为WPF BitmapSource
|
||||
var bitmapSource = ConvertBitmapToBitmapSource(bitmap);
|
||||
|
||||
// 复制到剪贴板
|
||||
Clipboard.SetImage(bitmapSource);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowNotification($"复制到剪贴板失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
@@ -259,7 +282,7 @@ namespace Ink_Canvas {
|
||||
// 获取DPI缩放比例
|
||||
var dpiScale = GetDpiScale();
|
||||
var virtualScreen = SystemInformation.VirtualScreen;
|
||||
|
||||
|
||||
// 创建结果位图
|
||||
var resultBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);
|
||||
using (var resultGraphics = Graphics.FromImage(resultBitmap))
|
||||
@@ -278,11 +301,11 @@ namespace Ink_Canvas {
|
||||
// 将WPF坐标转换为实际屏幕坐标,然后相对于截图区域计算偏移
|
||||
double screenX = (path[i].X * dpiScale) + virtualScreen.Left;
|
||||
double screenY = (path[i].Y * dpiScale) + virtualScreen.Top;
|
||||
|
||||
|
||||
// 计算相对于截图区域的坐标
|
||||
float relativeX = (float)(screenX - area.X);
|
||||
float relativeY = (float)(screenY - area.Y);
|
||||
|
||||
|
||||
points[i] = new PointF(relativeX, relativeY);
|
||||
}
|
||||
|
||||
@@ -291,7 +314,7 @@ namespace Ink_Canvas {
|
||||
|
||||
// 设置裁剪区域为路径内部
|
||||
resultGraphics.SetClip(pathGraphics);
|
||||
|
||||
|
||||
// 在裁剪区域内绘制原始图像
|
||||
resultGraphics.DrawImage(bitmap, 0, 0);
|
||||
}
|
||||
@@ -318,8 +341,10 @@ namespace Ink_Canvas {
|
||||
}
|
||||
|
||||
// 将System.Drawing.Bitmap转换为WPF BitmapSource
|
||||
private BitmapSource ConvertBitmapToBitmapSource(Bitmap bitmap) {
|
||||
using (var memory = new MemoryStream()) {
|
||||
private BitmapSource ConvertBitmapToBitmapSource(Bitmap bitmap)
|
||||
{
|
||||
using (var memory = new MemoryStream())
|
||||
{
|
||||
bitmap.Save(memory, ImageFormat.Png);
|
||||
memory.Position = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user