Files
community/Ink Canvas/Helpers/PPTInkManager.cs
T

502 lines
19 KiB
C#
Raw Normal View History

2026-02-16 20:22:41 +08:00
using Microsoft.Office.Interop.PowerPoint;
2025-08-31 11:43:52 +08:00
using System;
2025-07-29 01:15:32 +08:00
using System.IO;
2025-09-13 19:35:36 +08:00
using System.Runtime.InteropServices;
2025-07-29 01:15:32 +08:00
using System.Security.Cryptography;
using System.Text;
using System.Windows.Ink;
namespace Ink_Canvas.Helpers
{
/// <summary>
2026-02-20 12:29:40 +08:00
/// PPT墨迹管理器 - 负责按幻灯片保存/加载墨迹、自动保存与内存管理。
2025-07-29 01:15:32 +08:00
/// </summary>
public class PPTInkManager : IDisposable
{
#region Properties
public bool IsAutoSaveEnabled { get; set; } = true;
public string AutoSaveLocation { get; set; } = "";
public StrokeCollection CurrentStrokes { get; private set; } = new StrokeCollection();
#endregion
#region Private Fields
private MemoryStream[] _memoryStreams;
2026-02-20 12:29:40 +08:00
private const int DefaultMaxSlides = 100;
private int _maxSlides = DefaultMaxSlides;
2025-07-29 01:15:32 +08:00
private string _currentPresentationId = "";
private readonly object _lockObject = new object();
2025-08-31 09:54:13 +08:00
private bool _disposed;
2025-08-03 16:46:33 +08:00
2025-07-29 01:15:32 +08:00
// 墨迹锁定机制,防止翻页时的墨迹冲突
private DateTime _inkLockUntil = DateTime.MinValue;
private int _lockedSlideIndex = -1;
private const int InkLockMilliseconds = 500;
2025-10-03 17:08:46 +08:00
2025-09-13 18:20:56 +08:00
// 添加快速切换保护机制
private DateTime _lastSwitchTime = DateTime.MinValue;
private int _lastSwitchSlideIndex = -1;
2026-02-20 12:29:40 +08:00
private const int MinSwitchIntervalMs = 100;
2025-10-03 17:08:46 +08:00
2026-02-20 12:29:40 +08:00
private long _totalMemoryUsage;
private const long MaxMemoryUsageBytes = 100 * 1024 * 1024; // 100MB
2025-09-21 07:52:11 +08:00
private DateTime _lastMemoryCleanup = DateTime.MinValue;
2026-02-20 12:29:40 +08:00
private const int MemoryCleanupIntervalMinutes = 5;
private const string StrokeFileExtension = ".icstk";
2025-07-29 01:15:32 +08:00
#endregion
#region Constructor
public PPTInkManager()
{
2026-02-20 12:29:40 +08:00
InitializeMemoryStreams(DefaultMaxSlides + 2);
2025-07-29 01:15:32 +08:00
}
2026-02-20 12:29:40 +08:00
private void InitializeMemoryStreams(int capacity)
2025-07-29 01:15:32 +08:00
{
2026-02-21 17:44:30 +08:00
if (_memoryStreams != null)
{
for (int i = 0; i < _memoryStreams.Length; i++)
{
try { _memoryStreams[i]?.Dispose(); } catch (Exception ex) { LogHelper.WriteLogToFile($"InitializeMemoryStreams 释放内存流 {i} 失败: {ex}", LogHelper.LogType.Warning); }
}
}
2026-02-20 12:29:40 +08:00
_memoryStreams = new MemoryStream[Math.Max(2, capacity)];
2025-07-29 01:15:32 +08:00
}
#endregion
#region Public Methods
2026-02-20 12:29:40 +08:00
2025-07-29 01:15:32 +08:00
/// <summary>
/// 初始化新的演示文稿
/// </summary>
public void InitializePresentation(Presentation presentation)
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2025-07-29 01:15:32 +08:00
if (presentation == null) return;
lock (_lockObject)
{
try
{
2026-02-20 12:29:40 +08:00
ClearAllStrokesInternal();
2025-08-23 19:16:19 +08:00
_inkLockUntil = DateTime.MinValue;
_lockedSlideIndex = -1;
2026-02-20 12:29:40 +08:00
_lastSwitchSlideIndex = -1;
_lastSwitchTime = DateTime.MinValue;
2025-08-31 11:43:52 +08:00
2025-07-29 01:15:32 +08:00
_currentPresentationId = GeneratePresentationId(presentation);
2025-08-03 16:46:33 +08:00
2025-09-13 19:35:36 +08:00
int slideCount = 0;
try
{
slideCount = presentation.Slides.Count;
}
catch (COMException comEx)
{
2026-02-20 12:29:40 +08:00
uint hr = (uint)comEx.HResult;
if (hr == 0x80048010) return;
2025-10-03 17:08:46 +08:00
throw;
2025-09-13 19:35:36 +08:00
}
2025-08-03 16:46:33 +08:00
2026-02-20 12:29:40 +08:00
int capacity = slideCount + 2;
_maxSlides = Math.Max(_maxSlides, slideCount);
_memoryStreams = new MemoryStream[capacity];
2025-07-29 01:15:32 +08:00
if (IsAutoSaveEnabled && !string.IsNullOrEmpty(AutoSaveLocation))
LoadSavedStrokes();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"初始化演示文稿墨迹管理失败: {ex}", LogHelper.LogType.Error);
}
}
}
/// <summary>
/// 保存当前页面的墨迹
/// </summary>
public void SaveCurrentSlideStrokes(int slideIndex, StrokeCollection strokes)
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2025-07-29 01:15:32 +08:00
if (slideIndex <= 0 || strokes == null) return;
lock (_lockObject)
{
try
{
2026-02-20 12:29:40 +08:00
if (!CanWriteInk(slideIndex)) return;
if (slideIndex >= _memoryStreams.Length) return;
2025-08-03 16:46:33 +08:00
2026-02-20 12:29:40 +08:00
ReplaceSlideStream(slideIndex, strokes);
CheckAndPerformMemoryCleanup();
2025-07-29 01:15:32 +08:00
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"保存第{slideIndex}页墨迹失败: {ex}", LogHelper.LogType.Error);
}
}
}
2025-09-13 18:13:31 +08:00
/// <summary>
2026-02-20 12:29:40 +08:00
/// 强制保存指定页墨迹到内存(不受锁定限制)。用于放映结束前保存当前画布到当前页。
2025-09-13 18:13:31 +08:00
/// </summary>
public void ForceSaveSlideStrokes(int slideIndex, StrokeCollection strokes)
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2025-09-13 18:13:31 +08:00
if (slideIndex <= 0 || strokes == null) return;
lock (_lockObject)
{
try
{
2026-02-20 12:29:40 +08:00
if (slideIndex >= _memoryStreams.Length) return;
ReplaceSlideStream(slideIndex, strokes);
LogHelper.WriteLogToFile($"已强制保存第{slideIndex}页墨迹", LogHelper.LogType.Trace);
2025-09-13 18:13:31 +08:00
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"强制保存第{slideIndex}页墨迹失败: {ex}", LogHelper.LogType.Error);
}
}
}
2025-07-29 01:15:32 +08:00
/// <summary>
/// 加载指定页面的墨迹
/// </summary>
public StrokeCollection LoadSlideStrokes(int slideIndex)
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2025-07-29 01:15:32 +08:00
if (slideIndex <= 0) return new StrokeCollection();
lock (_lockObject)
{
try
{
if (slideIndex < _memoryStreams.Length && _memoryStreams[slideIndex] != null && _memoryStreams[slideIndex].Length > 0)
{
_memoryStreams[slideIndex].Position = 0;
2026-02-20 12:29:40 +08:00
return new StrokeCollection(_memoryStreams[slideIndex]);
2025-07-29 01:15:32 +08:00
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"加载第{slideIndex}页墨迹失败: {ex}", LogHelper.LogType.Error);
}
}
return new StrokeCollection();
}
/// <summary>
/// 切换到指定页面并加载墨迹
/// </summary>
public StrokeCollection SwitchToSlide(int slideIndex, StrokeCollection currentStrokes = null)
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2025-07-29 01:15:32 +08:00
lock (_lockObject)
{
try
{
2025-09-13 18:20:56 +08:00
var now = DateTime.Now;
2026-02-20 12:29:40 +08:00
if (now - _lastSwitchTime < TimeSpan.FromMilliseconds(MinSwitchIntervalMs) && _lastSwitchSlideIndex == slideIndex)
2025-09-13 18:20:56 +08:00
{
2026-02-20 12:29:40 +08:00
LogHelper.WriteLogToFile($"快速切换保护:忽略重复请求 页{slideIndex}", LogHelper.LogType.Trace);
2025-09-13 18:20:56 +08:00
return LoadSlideStrokes(slideIndex);
}
2025-07-29 01:15:32 +08:00
LockInkForSlide(slideIndex);
2026-02-20 12:29:40 +08:00
StrokeCollection newStrokes = LoadSlideStrokes(slideIndex);
2025-09-13 18:20:56 +08:00
_lastSwitchTime = now;
_lastSwitchSlideIndex = slideIndex;
2025-08-23 19:16:19 +08:00
return newStrokes;
2025-07-29 01:15:32 +08:00
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"切换到第{slideIndex}页失败: {ex}", LogHelper.LogType.Error);
return new StrokeCollection();
}
}
}
/// <summary>
/// 保存所有墨迹到文件
/// </summary>
/// <param name="presentation">演示文稿对象</param>
/// <param name="currentSlideIndex">当前播放的页码,如果提供则使用此值保存位置,否则使用_lockedSlideIndex</param>
public void SaveAllStrokesToFile(Presentation presentation, int currentSlideIndex = -1)
2025-07-29 01:15:32 +08:00
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2025-07-29 01:15:32 +08:00
if (!IsAutoSaveEnabled || string.IsNullOrEmpty(AutoSaveLocation) || presentation == null) return;
lock (_lockObject)
{
try
{
2026-02-20 12:29:40 +08:00
string folderPath = GetPresentationFolderPath();
2025-07-29 01:15:32 +08:00
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
2026-02-20 12:29:40 +08:00
int positionToSave = currentSlideIndex > 0 ? currentSlideIndex : (_lockedSlideIndex > 0 ? _lockedSlideIndex : _lastSwitchSlideIndex);
if (positionToSave > 0)
2025-07-29 01:15:32 +08:00
{
2026-02-20 12:29:40 +08:00
try { File.WriteAllText(Path.Combine(folderPath, "Position"), positionToSave.ToString()); }
catch (Exception ex) { LogHelper.WriteLogToFile($"保存 Position 失败: {ex}", LogHelper.LogType.Warning); }
2025-07-29 01:15:32 +08:00
}
2025-09-13 19:35:36 +08:00
int slideCount = 0;
2026-02-20 12:29:40 +08:00
try { slideCount = presentation.Slides.Count; }
2025-09-13 19:35:36 +08:00
catch (COMException comEx)
{
2026-02-20 12:29:40 +08:00
if ((uint)comEx.HResult == 0x80048010) return;
2025-10-03 17:08:46 +08:00
throw;
2025-09-13 19:35:36 +08:00
}
2025-10-03 17:08:46 +08:00
2025-09-13 19:35:36 +08:00
for (int i = 1; i <= slideCount && i < _memoryStreams.Length; i++)
2025-07-29 01:15:32 +08:00
{
2026-02-20 12:29:40 +08:00
if (_memoryStreams[i] == null) continue;
try
2025-07-29 01:15:32 +08:00
{
2026-02-20 12:29:40 +08:00
if (_memoryStreams[i].Length > 8)
2025-07-29 01:15:32 +08:00
{
2026-02-20 12:29:40 +08:00
_memoryStreams[i].Position = 0;
byte[] buf = new byte[_memoryStreams[i].Length];
int read = _memoryStreams[i].Read(buf, 0, buf.Length);
if (read > 0)
2025-07-29 01:15:32 +08:00
{
2026-02-20 12:29:40 +08:00
string basePath = Path.Combine(folderPath, i.ToString("0000"));
File.WriteAllBytes(basePath + StrokeFileExtension, buf);
2025-07-29 01:15:32 +08:00
}
}
2026-02-20 12:29:40 +08:00
else
2025-07-29 01:15:32 +08:00
{
2026-02-20 12:29:40 +08:00
TryDeleteStrokeFile(folderPath, i);
2025-07-29 01:15:32 +08:00
}
}
2026-02-20 12:29:40 +08:00
catch (Exception ex)
{
LogHelper.WriteLogToFile($"保存第{i}页墨迹到文件失败: {ex}", LogHelper.LogType.Error);
}
2025-07-29 01:15:32 +08:00
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"保存墨迹到文件失败: {ex}", LogHelper.LogType.Error);
}
}
}
/// <summary>
/// 从文件加载已保存的墨迹
/// </summary>
public void LoadSavedStrokes()
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2025-07-29 01:15:32 +08:00
if (!IsAutoSaveEnabled || string.IsNullOrEmpty(AutoSaveLocation)) return;
lock (_lockObject)
{
try
{
2026-02-20 12:29:40 +08:00
string folderPath = GetPresentationFolderPath();
2025-07-29 01:15:32 +08:00
if (!Directory.Exists(folderPath)) return;
2026-02-20 12:29:40 +08:00
var dir = new DirectoryInfo(folderPath);
2025-07-29 01:15:32 +08:00
int loadedCount = 0;
2026-02-20 12:29:40 +08:00
foreach (FileInfo file in dir.GetFiles("*" + StrokeFileExtension))
2025-07-29 01:15:32 +08:00
{
2026-02-20 12:29:40 +08:00
string nameWithoutExt = Path.GetFileNameWithoutExtension(file.Name);
if (!int.TryParse(nameWithoutExt, out int slideIndex) || slideIndex <= 0) continue;
if (slideIndex >= _memoryStreams.Length) continue;
2025-07-29 01:15:32 +08:00
try
{
2026-02-20 12:29:40 +08:00
byte[] bytes = File.ReadAllBytes(file.FullName);
if (bytes.Length > 8)
2025-07-29 01:15:32 +08:00
{
2026-02-20 12:29:40 +08:00
_memoryStreams[slideIndex] = new MemoryStream(bytes);
_memoryStreams[slideIndex].Position = 0;
loadedCount++;
2025-07-29 01:15:32 +08:00
}
}
catch (Exception ex)
{
2026-02-20 12:29:40 +08:00
LogHelper.WriteLogToFile($"加载墨迹文件 {file.Name} 失败: {ex}", LogHelper.LogType.Error);
2025-07-29 01:15:32 +08:00
}
}
2026-02-20 12:29:40 +08:00
if (loadedCount > 0)
LogHelper.WriteLogToFile($"已从磁盘加载 {loadedCount} 页墨迹", LogHelper.LogType.Trace);
2025-07-29 01:15:32 +08:00
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"从文件加载墨迹失败: {ex}", LogHelper.LogType.Error);
}
}
}
/// <summary>
/// 清除所有墨迹
/// </summary>
public void ClearAllStrokes()
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2025-07-29 01:15:32 +08:00
lock (_lockObject)
{
2026-02-20 12:29:40 +08:00
ClearAllStrokesInternal();
2025-07-29 01:15:32 +08:00
}
}
public void LockInkForSlide(int slideIndex)
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2025-07-29 01:15:32 +08:00
_inkLockUntil = DateTime.Now.AddMilliseconds(InkLockMilliseconds);
_lockedSlideIndex = slideIndex;
}
public bool CanWriteInk(int currentSlideIndex)
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2026-02-20 12:29:40 +08:00
if (DateTime.Now >= _inkLockUntil) return true;
if (currentSlideIndex == _lockedSlideIndex) return true;
if (DateTime.Now - (_inkLockUntil.AddMilliseconds(-InkLockMilliseconds)) < TimeSpan.FromMilliseconds(50)) return true;
2025-09-13 19:29:17 +08:00
return false;
2025-07-29 01:15:32 +08:00
}
2025-09-13 18:52:23 +08:00
public void ResetLockState()
{
2026-02-21 17:44:30 +08:00
ThrowIfDisposed();
2025-09-13 18:52:23 +08:00
lock (_lockObject)
{
_inkLockUntil = DateTime.MinValue;
_lockedSlideIndex = -1;
_lastSwitchTime = DateTime.MinValue;
_lastSwitchSlideIndex = -1;
}
}
2025-09-21 07:52:11 +08:00
2026-02-20 12:29:40 +08:00
#endregion
#region Private Helpers
private void ClearAllStrokesInternal()
{
if (_memoryStreams != null)
{
for (int i = 0; i < _memoryStreams.Length; i++)
{
try { _memoryStreams[i]?.Dispose(); } catch (Exception ex) { LogHelper.WriteLogToFile($"释放内存流 {i} 失败: {ex}", LogHelper.LogType.Warning); }
finally { _memoryStreams[i] = null; }
}
_memoryStreams = new MemoryStream[_maxSlides + 2];
}
CurrentStrokes?.Clear();
LogHelper.WriteLogToFile("已清除所有墨迹", LogHelper.LogType.Trace);
}
private void ReplaceSlideStream(int slideIndex, StrokeCollection strokes)
{
try { _memoryStreams[slideIndex]?.Dispose(); } catch (Exception ex) { LogHelper.WriteLogToFile($"释放旧内存流失败: {ex}", LogHelper.LogType.Warning); }
var ms = new MemoryStream();
strokes.Save(ms);
ms.Position = 0;
_memoryStreams[slideIndex] = ms;
}
private void TryDeleteStrokeFile(string folderPath, int slideIndex)
{
try
{
string path = Path.Combine(folderPath, slideIndex.ToString("0000") + StrokeFileExtension);
if (File.Exists(path)) File.Delete(path);
}
2026-02-21 16:51:34 +08:00
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
2026-02-20 12:29:40 +08:00
}
2025-09-21 07:52:11 +08:00
private void CheckAndPerformMemoryCleanup()
{
try
{
var now = DateTime.Now;
2026-02-20 12:29:40 +08:00
if (now - _lastMemoryCleanup < TimeSpan.FromMinutes(MemoryCleanupIntervalMinutes)) return;
2025-10-03 17:08:46 +08:00
2025-09-21 07:52:11 +08:00
long currentMemoryUsage = 0;
if (_memoryStreams != null)
{
for (int i = 0; i < _memoryStreams.Length; i++)
2026-02-20 12:29:40 +08:00
if (_memoryStreams[i] != null) currentMemoryUsage += _memoryStreams[i].Length;
2025-09-21 07:52:11 +08:00
}
_totalMemoryUsage = currentMemoryUsage;
if (currentMemoryUsage > MaxMemoryUsageBytes)
{
2026-02-20 12:29:40 +08:00
LogHelper.WriteLogToFile($"墨迹内存超限 ({currentMemoryUsage / (1024 * 1024)}MB),执行清理", LogHelper.LogType.Warning);
2025-09-21 07:52:11 +08:00
CleanupInactiveSlideStrokes();
}
2026-02-20 12:29:40 +08:00
_lastMemoryCleanup = now;
2025-09-21 07:52:11 +08:00
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"内存清理检查失败: {ex}", LogHelper.LogType.Error);
}
}
private void CleanupInactiveSlideStrokes()
{
2026-02-20 12:29:40 +08:00
if (_memoryStreams == null) return;
int cleaned = 0;
long freed = 0;
for (int i = 0; i < _memoryStreams.Length; i++)
2025-09-21 07:52:11 +08:00
{
2026-02-20 12:29:40 +08:00
if (i == _lockedSlideIndex || i == _lastSwitchSlideIndex) continue;
if (_memoryStreams[i] != null)
2025-09-21 07:52:11 +08:00
{
2026-02-20 12:29:40 +08:00
long len = _memoryStreams[i].Length;
2026-02-21 16:51:34 +08:00
try { _memoryStreams[i].Dispose(); freed += len; cleaned++; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
2026-02-20 12:29:40 +08:00
finally { _memoryStreams[i] = null; }
2025-09-21 07:52:11 +08:00
}
}
2026-02-20 12:29:40 +08:00
if (cleaned > 0)
LogHelper.WriteLogToFile($"已清理 {cleaned} 页墨迹,释放 {freed / 1024}KB", LogHelper.LogType.Trace);
2025-09-21 07:52:11 +08:00
}
2025-07-29 01:15:32 +08:00
private string GeneratePresentationId(Presentation presentation)
{
try
{
2026-02-20 12:29:40 +08:00
string path = presentation.FullName;
2026-02-21 17:44:30 +08:00
string hash = HashHelper.GetFileHash(path);
2026-02-20 12:29:40 +08:00
return $"{presentation.Name}_{presentation.Slides.Count}_{hash}";
2025-07-29 01:15:32 +08:00
}
catch (Exception ex)
{
2026-02-20 12:29:40 +08:00
LogHelper.WriteLogToFile($"生成演示文稿 ID 失败: {ex}", LogHelper.LogType.Error);
2025-07-29 01:15:32 +08:00
return $"unknown_{DateTime.Now.Ticks}";
}
}
private string GetPresentationFolderPath()
{
return Path.Combine(AutoSaveLocation, "Auto Saved - Presentations", _currentPresentationId);
}
2026-02-20 12:29:40 +08:00
2025-07-29 01:15:32 +08:00
#endregion
#region Dispose
2026-02-20 12:29:40 +08:00
2025-07-29 01:15:32 +08:00
public void Dispose()
{
2026-02-20 12:29:40 +08:00
if (_disposed) return;
lock (_lockObject) { ClearAllStrokesInternal(); }
_disposed = true;
2026-02-21 17:44:30 +08:00
GC.SuppressFinalize(this);
}
private void ThrowIfDisposed()
{
if (_disposed) throw new ObjectDisposedException(nameof(PPTInkManager));
2025-07-29 01:15:32 +08:00
}
2026-02-20 12:29:40 +08:00
2025-07-29 01:15:32 +08:00
#endregion
}
}