99using System . Windows ;
1010using System . Windows . Controls ;
1111using System . Windows . Forms ;
12- using System . Windows . Ink ;
1312using System . Windows . Media ;
1413using System . Windows . Media . Imaging ;
1514using System . Windows . Threading ;
@@ -31,17 +30,15 @@ public struct ScreenshotResult
3130 public Bitmap CameraImage ;
3231 public BitmapSource CameraBitmapSource ;
3332 public bool AddToWhiteboard ;
34- public bool IncludeInk ;
3533
3634 public ScreenshotResult ( Rectangle area , List < Point > path = null , Bitmap cameraImage = null ,
37- BitmapSource cameraBitmapSource = null , bool addToWhiteboard = false , bool includeInk = true )
35+ BitmapSource cameraBitmapSource = null , bool addToWhiteboard = false )
3836 {
3937 Area = area ;
4038 Path = path ;
4139 CameraImage = cameraImage ;
4240 CameraBitmapSource = cameraBitmapSource ;
4341 AddToWhiteboard = addToWhiteboard ;
44- IncludeInk = includeInk ;
4542 }
4643 }
4744
@@ -98,7 +95,7 @@ private async Task CaptureScreenshotAndInsert()
9895 else if ( screenshotResult . Value . Area . Width > 0 && screenshotResult . Value . Area . Height > 0 )
9996 {
10097 // 屏幕截图
101- using ( var originalBitmap = CaptureScreenAreaWithOptionalInk ( screenshotResult . Value . Area , screenshotResult . Value . IncludeInk ) )
98+ using ( var originalBitmap = CaptureScreenArea ( screenshotResult . Value . Area ) )
10299 {
103100 if ( originalBitmap != null )
104101 {
@@ -210,16 +207,7 @@ private async Task CaptureFullScreenAndInsert()
210207 {
211208 await Application . Current . Dispatcher . InvokeAsync ( ( ) =>
212209 {
213- var selectorWindow = new ScreenshotSelectorWindow ( shouldIncludeInk =>
214- {
215- if ( inkCanvas == null )
216- {
217- return ;
218- }
219-
220- inkCanvas . Visibility = shouldIncludeInk ? Visibility . Visible : Visibility . Collapsed ;
221- Dispatcher . Invoke ( ( ) => { } , DispatcherPriority . Render ) ;
222- } ) ;
210+ var selectorWindow = new ScreenshotSelectorWindow ( ) ;
223211 if ( selectorWindow . ShowDialog ( ) == true )
224212 {
225213 // 检查是否是摄像头截图
@@ -230,8 +218,7 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
230218 null , // 摄像头截图不需要路径
231219 null , // 不再使用Bitmap
232220 selectorWindow . CameraBitmapSource , // 摄像头BitmapSource
233- selectorWindow . ShouldAddToWhiteboard ,
234- selectorWindow . ShouldIncludeInk
221+ selectorWindow . ShouldAddToWhiteboard
235222 ) ;
236223 }
237224 else if ( selectorWindow . CameraImage != null )
@@ -241,8 +228,7 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
241228 null , // 摄像头截图不需要路径
242229 selectorWindow . CameraImage , // 摄像头图像
243230 null ,
244- selectorWindow . ShouldAddToWhiteboard ,
245- selectorWindow . ShouldIncludeInk
231+ selectorWindow . ShouldAddToWhiteboard
246232 ) ;
247233 }
248234 else
@@ -252,8 +238,7 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
252238 selectorWindow . SelectedPath ,
253239 null ,
254240 null ,
255- selectorWindow . ShouldAddToWhiteboard ,
256- selectorWindow . ShouldIncludeInk
241+ selectorWindow . ShouldAddToWhiteboard
257242 ) ;
258243 }
259244 }
@@ -319,108 +304,6 @@ private Bitmap CaptureScreenArea(Rectangle area)
319304 }
320305 }
321306
322- private Bitmap CaptureScreenAreaWithOptionalInk ( Rectangle area , bool includeInk )
323- {
324- Bitmap bitmap = null ;
325- StrokeCollection strokesForOverlay = null ;
326- Point ? inkCanvasTopLeftOnScreen = null ;
327- System . Windows . Media . Matrix ? dpiTransform = null ;
328- var originalWindowVisibility = Visibility ;
329-
330- try
331- {
332- if ( includeInk && inkCanvas != null && inkCanvas . Strokes . Count > 0 )
333- {
334- strokesForOverlay = inkCanvas . Strokes . Clone ( ) ;
335-
336- var source = PresentationSource . FromVisual ( inkCanvas ) ;
337- if ( source ? . CompositionTarget != null )
338- {
339- dpiTransform = source . CompositionTarget . TransformToDevice ;
340- }
341-
342- inkCanvasTopLeftOnScreen = inkCanvas . PointToScreen ( new Point ( 0 , 0 ) ) ;
343- }
344-
345- // 先隐藏主窗口再截取屏幕,确保基础截图不包含主线程 UI(含墨迹层)。
346- Visibility = Visibility . Hidden ;
347- Dispatcher . Invoke ( ( ) => { } , DispatcherPriority . Render ) ;
348-
349- bitmap = CaptureScreenArea ( area ) ;
350- }
351- catch ( Exception ex )
352- {
353- LogHelper . WriteLogToFile ( $ "准备截图时处理墨迹失败: { ex . Message } ", LogHelper . LogType . Error ) ;
354- bitmap ? . Dispose ( ) ;
355- return null ;
356- }
357- finally
358- {
359- Visibility = originalWindowVisibility ;
360- Dispatcher . Invoke ( ( ) => { } , DispatcherPriority . Render ) ;
361- }
362-
363- if ( bitmap == null || ! includeInk || strokesForOverlay == null || strokesForOverlay . Count == 0 )
364- {
365- return bitmap ;
366- }
367-
368- try
369- {
370- OverlayInkStrokesOnBitmap ( bitmap , area , strokesForOverlay , inkCanvasTopLeftOnScreen , dpiTransform ) ;
371- }
372- catch ( Exception ex )
373- {
374- LogHelper . WriteLogToFile ( $ "叠加墨迹到截图失败: { ex . Message } ", LogHelper . LogType . Error ) ;
375- }
376-
377- return bitmap ;
378- }
379-
380- private void OverlayInkStrokesOnBitmap (
381- Bitmap bitmap ,
382- Rectangle area ,
383- StrokeCollection strokes ,
384- Point ? inkCanvasTopLeftOnScreen = null ,
385- System . Windows . Media . Matrix ? dpiTransform = null )
386- {
387- if ( bitmap == null || strokes == null || strokes . Count == 0 )
388- {
389- return ;
390- }
391-
392- var transform = dpiTransform ?? new System . Windows . Media . Matrix ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
393- var topLeft = inkCanvasTopLeftOnScreen ?? new Point ( area . X , area . Y ) ;
394- var offsetX = topLeft . X * transform . M11 - area . X ;
395- var offsetY = topLeft . Y * transform . M22 - area . Y ;
396-
397- var drawingVisual = new DrawingVisual ( ) ;
398- using ( var drawingContext = drawingVisual . RenderOpen ( ) )
399- {
400- drawingContext . PushTransform ( new TranslateTransform ( offsetX , offsetY ) ) ;
401- strokes . Draw ( drawingContext ) ;
402- drawingContext . Pop ( ) ;
403- }
404-
405- var renderBitmap = new RenderTargetBitmap ( bitmap . Width , bitmap . Height , 96 , 96 , PixelFormats . Pbgra32 ) ;
406- renderBitmap . Render ( drawingVisual ) ;
407-
408- var encoder = new PngBitmapEncoder ( ) ;
409- encoder . Frames . Add ( BitmapFrame . Create ( renderBitmap ) ) ;
410-
411- using ( var memoryStream = new MemoryStream ( ) )
412- {
413- encoder . Save ( memoryStream ) ;
414- memoryStream . Position = 0 ;
415- using ( var overlayBitmap = new Bitmap ( memoryStream ) )
416- using ( var graphics = Graphics . FromImage ( bitmap ) )
417- {
418- graphics . CompositingMode = CompositingMode . SourceOver ;
419- graphics . DrawImage ( overlayBitmap , 0 , 0 , bitmap . Width , bitmap . Height ) ;
420- }
421- }
422- }
423-
424307 /// <summary>
425308 /// 将截图插入到画布
426309 /// </summary>
0 commit comments