add:墨迹全屏保存 #79

This commit is contained in:
2025-07-16 09:49:41 +08:00
parent a6cd9c955b
commit 3e87ed4f9b
5 changed files with 96 additions and 2 deletions
+11
View File
@@ -2335,6 +2335,17 @@
FontFamily="Microsoft YaHei UI" FontWeight="Bold"
Toggled="ToggleSwitchAutoSaveStrokesAtScreenshot_Toggled" />
</ui:SimpleStackPanel>
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock Foreground="#fafafa" Text="墨迹全页面保存" VerticalAlignment="Center"
FontSize="14" Margin="0,0,16,0" />
<ui:ToggleSwitch OnContent="" OffContent=""
Name="ToggleSwitchSaveFullPageStrokes" IsOn="False"
FontFamily="Microsoft YaHei UI" FontWeight="Bold"
Toggled="ToggleSwitchSaveFullPageStrokes_Toggled" />
</ui:SimpleStackPanel>
<TextBlock Text="# 开启后自动保存和手动保存墨迹时将以全屏模式保存而非单独保存每条墨迹" TextWrapping="Wrap" Foreground="#a1a1aa" />
<Line HorizontalAlignment="Center" X1="0" Y1="0" X2="400" Y2="0" Stroke="#3f3f46"
StrokeThickness="1" Margin="0,4,0,4" />
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left">
@@ -6,6 +6,10 @@ using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;
using File = System.IO.File;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
namespace Ink_Canvas {
public partial class MainWindow : Window {
@@ -33,10 +37,78 @@ namespace Ink_Canvas {
else
//savePathWithName = savePath + @"\" + DateTime.Now.ToString("u").Replace(':', '-') + ".icstk";
savePathWithName = savePath + @"\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-fff") + ".icstk";
var fs = new FileStream(savePathWithName, FileMode.Create);
if (Settings.Automation.IsSaveFullPageStrokes)
{
// 全页面保存模式 - 保存整个墨迹页面的图像
var bitmap = new System.Drawing.Bitmap(
(int)System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
(int)System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
using (var g = System.Drawing.Graphics.FromImage(bitmap))
{
// 创建黑色或透明背景
System.Drawing.Color bgColor = Settings.Canvas.UsingWhiteboard
? System.Drawing.Color.White
: System.Drawing.Color.FromArgb(22, 41, 36); // 黑板背景色
g.Clear(bgColor);
// 将InkCanvas墨迹渲染到Visual
var visual = new DrawingVisual();
using (var dc = visual.RenderOpen())
{
// 创建一个VisualBrush,使用inkCanvas作为源
var visualBrush = new VisualBrush(inkCanvas);
// 绘制矩形并填充为inkCanvas的内容
dc.DrawRectangle(visualBrush, null, new Rect(0, 0, inkCanvas.ActualWidth, inkCanvas.ActualHeight));
}
// 创建适合墨迹画布尺寸的渲染位图
var rtb = new RenderTargetBitmap(
(int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight,
96, 96,
PixelFormats.Pbgra32);
rtb.Render(visual);
// 转换为GDI+ Bitmap并保存
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
using (var ms = new MemoryStream())
{
encoder.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
var imgBitmap = new System.Drawing.Bitmap(ms);
// 将生成的墨迹图像绘制到屏幕截图上
// 居中绘制,确保墨迹位于屏幕中央
int x = (bitmap.Width - imgBitmap.Width) / 2;
int y = (bitmap.Height - imgBitmap.Height) / 2;
g.DrawImage(imgBitmap, x, y);
// 保存为PNG
string imagePathWithName = Path.ChangeExtension(savePathWithName, "png");
bitmap.Save(imagePathWithName, System.Drawing.Imaging.ImageFormat.Png);
// 仍然保存墨迹文件以兼容旧版本
inkCanvas.Strokes.Save(fs);
}
}
// 显示提示
if (newNotice) ShowNotification("墨迹成功全页面保存至 " + Path.ChangeExtension(savePathWithName, "png"));
}
else
{
// 常规保存模式 - 仅保存墨迹对象
inkCanvas.Strokes.Save(fs);
if (newNotice) ShowNotification("墨迹成功保存至 " + savePathWithName);
}
fs.Close();
}
catch (Exception ex) {
ShowNotification("墨迹保存失败");
LogHelper.WriteLogToFile("墨迹保存失败 | " + ex.ToString(), LogHelper.LogType.Error);
+6
View File
@@ -1357,6 +1357,12 @@ namespace Ink_Canvas {
SaveSettingsToFile();
}
private void ToggleSwitchSaveFullPageStrokes_Toggled(object sender, RoutedEventArgs e) {
if (!isLoaded) return;
Settings.Automation.IsSaveFullPageStrokes = ToggleSwitchSaveFullPageStrokes.IsOn;
SaveSettingsToFile();
}
#endregion
#region Gesture
@@ -700,6 +700,8 @@ namespace Ink_Canvas {
ToggleSwitchAutoSaveStrokesAtScreenshot.IsOn = Settings.Automation.IsAutoSaveStrokesAtScreenshot;
ToggleSwitchSaveFullPageStrokes.IsOn = Settings.Automation.IsSaveFullPageStrokes;
SideControlMinimumAutomationSlider.Value = Settings.Automation.MinimumAutomationStrokeNumber;
AutoSavedStrokesLocation.Text = Settings.Automation.AutoSavedStrokesLocation;
+3
View File
@@ -364,6 +364,9 @@ namespace Ink_Canvas
[JsonProperty("autoDelSavedFilesDaysThreshold")]
public int AutoDelSavedFilesDaysThreshold = 15;
[JsonProperty("isSaveFullPageStrokes")]
public bool IsSaveFullPageStrokes = false;
}
public class Advanced