Revert "feat: 选区截图时保留屏幕笔迹 (#406)" (#418)

This reverts commit f7aa107a62.
This commit is contained in:
CJK_mkp
2026-03-28 17:23:27 +08:00
committed by GitHub
parent 837311b2b7
commit 91c2fa4eee
4 changed files with 17 additions and 280 deletions
+6 -123
View File
@@ -9,7 +9,6 @@ using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Ink;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
@@ -31,17 +30,15 @@ namespace Ink_Canvas
public Bitmap CameraImage;
public BitmapSource CameraBitmapSource;
public bool AddToWhiteboard;
public bool IncludeInk;
public ScreenshotResult(Rectangle area, List<Point> path = null, Bitmap cameraImage = null,
BitmapSource cameraBitmapSource = null, bool addToWhiteboard = false, bool includeInk = true)
BitmapSource cameraBitmapSource = null, bool addToWhiteboard = false)
{
Area = area;
Path = path;
CameraImage = cameraImage;
CameraBitmapSource = cameraBitmapSource;
AddToWhiteboard = addToWhiteboard;
IncludeInk = includeInk;
}
}
@@ -98,7 +95,7 @@ namespace Ink_Canvas
else if (screenshotResult.Value.Area.Width > 0 && screenshotResult.Value.Area.Height > 0)
{
// 屏幕截图
using (var originalBitmap = CaptureScreenAreaWithOptionalInk(screenshotResult.Value.Area, screenshotResult.Value.IncludeInk))
using (var originalBitmap = CaptureScreenArea(screenshotResult.Value.Area))
{
if (originalBitmap != null)
{
@@ -210,16 +207,7 @@ namespace Ink_Canvas
{
await Application.Current.Dispatcher.InvokeAsync(() =>
{
var selectorWindow = new ScreenshotSelectorWindow(shouldIncludeInk =>
{
if (inkCanvas == null)
{
return;
}
inkCanvas.Visibility = shouldIncludeInk ? Visibility.Visible : Visibility.Collapsed;
Dispatcher.Invoke(() => { }, DispatcherPriority.Render);
});
var selectorWindow = new ScreenshotSelectorWindow();
if (selectorWindow.ShowDialog() == true)
{
// 检查是否是摄像头截图
@@ -230,8 +218,7 @@ namespace Ink_Canvas
null, // 摄像头截图不需要路径
null, // 不再使用Bitmap
selectorWindow.CameraBitmapSource, // 摄像头BitmapSource
selectorWindow.ShouldAddToWhiteboard,
selectorWindow.ShouldIncludeInk
selectorWindow.ShouldAddToWhiteboard
);
}
else if (selectorWindow.CameraImage != null)
@@ -241,8 +228,7 @@ namespace Ink_Canvas
null, // 摄像头截图不需要路径
selectorWindow.CameraImage, // 摄像头图像
null,
selectorWindow.ShouldAddToWhiteboard,
selectorWindow.ShouldIncludeInk
selectorWindow.ShouldAddToWhiteboard
);
}
else
@@ -252,8 +238,7 @@ namespace Ink_Canvas
selectorWindow.SelectedPath,
null,
null,
selectorWindow.ShouldAddToWhiteboard,
selectorWindow.ShouldIncludeInk
selectorWindow.ShouldAddToWhiteboard
);
}
}
@@ -319,108 +304,6 @@ namespace Ink_Canvas
}
}
private Bitmap CaptureScreenAreaWithOptionalInk(Rectangle area, bool includeInk)
{
Bitmap bitmap = null;
StrokeCollection strokesForOverlay = null;
Point? inkCanvasTopLeftOnScreen = null;
System.Windows.Media.Matrix? dpiTransform = null;
var originalWindowVisibility = Visibility;
try
{
if (includeInk && inkCanvas != null && inkCanvas.Strokes.Count > 0)
{
strokesForOverlay = inkCanvas.Strokes.Clone();
var source = PresentationSource.FromVisual(inkCanvas);
if (source?.CompositionTarget != null)
{
dpiTransform = source.CompositionTarget.TransformToDevice;
}
inkCanvasTopLeftOnScreen = inkCanvas.PointToScreen(new Point(0, 0));
}
// 先隐藏主窗口再截取屏幕,确保基础截图不包含主线程 UI(含墨迹层)。
Visibility = Visibility.Hidden;
Dispatcher.Invoke(() => { }, DispatcherPriority.Render);
bitmap = CaptureScreenArea(area);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"准备截图时处理墨迹失败: {ex.Message}", LogHelper.LogType.Error);
bitmap?.Dispose();
return null;
}
finally
{
Visibility = originalWindowVisibility;
Dispatcher.Invoke(() => { }, DispatcherPriority.Render);
}
if (bitmap == null || !includeInk || strokesForOverlay == null || strokesForOverlay.Count == 0)
{
return bitmap;
}
try
{
OverlayInkStrokesOnBitmap(bitmap, area, strokesForOverlay, inkCanvasTopLeftOnScreen, dpiTransform);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"叠加墨迹到截图失败: {ex.Message}", LogHelper.LogType.Error);
}
return bitmap;
}
private void OverlayInkStrokesOnBitmap(
Bitmap bitmap,
Rectangle area,
StrokeCollection strokes,
Point? inkCanvasTopLeftOnScreen = null,
System.Windows.Media.Matrix? dpiTransform = null)
{
if (bitmap == null || strokes == null || strokes.Count == 0)
{
return;
}
var transform = dpiTransform ?? new System.Windows.Media.Matrix(1, 0, 0, 1, 0, 0);
var topLeft = inkCanvasTopLeftOnScreen ?? new Point(area.X, area.Y);
var offsetX = topLeft.X * transform.M11 - area.X;
var offsetY = topLeft.Y * transform.M22 - area.Y;
var drawingVisual = new DrawingVisual();
using (var drawingContext = drawingVisual.RenderOpen())
{
drawingContext.PushTransform(new TranslateTransform(offsetX, offsetY));
strokes.Draw(drawingContext);
drawingContext.Pop();
}
var renderBitmap = new RenderTargetBitmap(bitmap.Width, bitmap.Height, 96, 96, PixelFormats.Pbgra32);
renderBitmap.Render(drawingVisual);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
using (var memoryStream = new MemoryStream())
{
encoder.Save(memoryStream);
memoryStream.Position = 0;
using (var overlayBitmap = new Bitmap(memoryStream))
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CompositingMode = CompositingMode.SourceOver;
graphics.DrawImage(overlayBitmap, 0, 0, bitmap.Width, bitmap.Height);
}
}
}
/// <summary>
/// 将截图插入到画布
/// </summary>