@@ -62,20 +62,20 @@ private static bool IsUseVideoBgDynamicColorUpdate
6262 private FFmpegMediaSource ? _currentFFmpegMediaSource ;
6363#endif
6464
65+ private const float CanvasBaseDpi = 96f ;
66+
6567 private CanvasVirtualImageSource ? _currentCanvasVirtualImageSource ;
6668 private CanvasBitmap ? _currentCanvasBitmap ;
6769 private CanvasDevice ? _currentCanvasDevice ;
6870 private volatile CanvasDrawingSession ? _currentCanvasDrawingSession ;
69- private readonly int _currentCanvasWidth ;
70- private readonly int _currentCanvasHeight ;
71- private readonly float _currentCanvasDpi ;
72- private readonly Rect _currentCanvasDrawArea ;
71+ private volatile float _currentCanvasWidth ;
72+ private volatile float _currentCanvasHeight ;
73+ private Rect _currentCanvasDrawArea ;
7374 private readonly MediaPlayerElement ? _currentMediaPlayerFrame ;
7475 private readonly Grid _currentMediaPlayerFrameParentGrid ;
7576 private readonly ImageUI _currentImage ;
7677 private readonly Lock _currentLock = new ( ) ;
7778
78-
7979 internal MediaPlayerLoader (
8080 FrameworkElement parentUI ,
8181 Grid acrylicMask , Grid overlayTitleBar ,
@@ -88,14 +88,17 @@ internal MediaPlayerLoader(
8888 AcrylicMask = acrylicMask ;
8989 OverlayTitleBar = overlayTitleBar ;
9090
91- _currentMediaPlayerFrameParentGrid = mediaPlayerParentGrid ;
92- _currentMediaPlayerFrame = mediaPlayerCurrent ;
91+ _currentMediaPlayerFrameParentGrid = mediaPlayerParentGrid ;
92+ _currentMediaPlayerFrameParentGrid . SizeChanged += UpdateCanvasOnSizeChangeEvent ;
93+ _currentMediaPlayerFrame = mediaPlayerCurrent ;
9394
94- _currentCanvasWidth = ( int ) _currentMediaPlayerFrameParentGrid . ActualWidth ;
95- _currentCanvasHeight = ( int ) _currentMediaPlayerFrameParentGrid . ActualHeight ;
96- _currentCanvasDpi = 96f ;
95+ float actualWidth = ( float ) _currentMediaPlayerFrameParentGrid . ActualWidth ;
96+ float actualHeight = ( float ) _currentMediaPlayerFrameParentGrid . ActualHeight ;
97+ float scalingFactor = ( float ) WindowUtility . CurrentWindowMonitorScaleFactor ;
9798
98- _currentCanvasDrawArea = new Rect ( 0 , 0 , _currentCanvasWidth , _currentCanvasHeight ) ;
99+ _currentCanvasWidth = actualWidth * scalingFactor ;
100+ _currentCanvasHeight = actualHeight * scalingFactor ;
101+ _currentCanvasDrawArea = new Rect ( 0f , 0f , _currentCanvasWidth , _currentCanvasHeight ) ;
99102
100103 _currentImage = mediaPlayerParentGrid . AddElementToGridRowColumn ( new ImageUI
101104 {
@@ -115,6 +118,7 @@ public void Dispose()
115118 {
116119 try
117120 {
121+ _currentMediaPlayerFrameParentGrid . SizeChanged -= UpdateCanvasOnSizeChangeEvent ;
118122 DisposeMediaModules ( ) ;
119123 }
120124 catch ( Exception ex )
@@ -137,29 +141,8 @@ public async Task LoadAsync(string filePath, bool isImageLoadF
137141 if ( IsUseVideoBgDynamicColorUpdate )
138142 {
139143 _currentCanvasDevice ??= CanvasDevice . GetSharedDevice ( ) ;
140- _currentCanvasVirtualImageSource ??= new CanvasVirtualImageSource ( _currentCanvasDevice ,
141- _currentCanvasWidth ,
142- _currentCanvasHeight ,
143- _currentCanvasDpi ,
144- CanvasAlphaMode . Ignore ) ;
145-
146- _currentImage . Source = _currentCanvasVirtualImageSource . Source ;
147-
148- byte [ ] temporaryBuffer = ArrayPool < byte > . Shared . Rent ( _currentCanvasWidth * _currentCanvasHeight * 4 ) ;
149- try
150- {
151- _currentCanvasBitmap ??= CanvasBitmap . CreateFromBytes ( _currentCanvasDevice ,
152- temporaryBuffer ,
153- _currentCanvasWidth ,
154- _currentCanvasHeight ,
155- Windows . Graphics . DirectX . DirectXPixelFormat . B8G8R8A8UIntNormalized ,
156- _currentCanvasDpi ,
157- CanvasAlphaMode . Ignore ) ;
158- }
159- finally
160- {
161- ArrayPool < byte > . Shared . Return ( temporaryBuffer ) ;
162- }
144+ CreateAndAssignCanvasVirtualImageSource ( ) ;
145+ CreateCanvasBitmap ( ) ;
163146
164147 _currentImage . Visibility = Visibility . Visible ;
165148 App . ToggleBlurBackdrop ( ) ;
@@ -250,6 +233,60 @@ Playing background video with FFmpeg!
250233 }
251234 }
252235
236+ private void UpdateCanvasOnSizeChangeEvent ( object sender , SizeChangedEventArgs e )
237+ {
238+ lock ( _currentLock )
239+ {
240+ float scalingFactor = ( float ) WindowUtility . CurrentWindowMonitorScaleFactor ;
241+ float newWidth = ( float ) ( e . NewSize . Width * scalingFactor ) ;
242+ float newHeight = ( float ) ( e . NewSize . Height * scalingFactor ) ;
243+
244+ LogWriteLine ( $ "Updating video canvas size from: { _currentCanvasWidth } x{ _currentCanvasHeight } to { newWidth } x{ newHeight } ", LogType . Debug , true ) ;
245+
246+ _currentCanvasWidth = newWidth ;
247+ _currentCanvasHeight = newHeight ;
248+ _currentCanvasDrawArea = new Rect ( 0 , 0 , _currentCanvasWidth , _currentCanvasHeight ) ;
249+
250+ _currentCanvasBitmap ? . Dispose ( ) ;
251+ _currentCanvasBitmap = null ;
252+ _currentCanvasVirtualImageSource = null ;
253+ CreateAndAssignCanvasVirtualImageSource ( ) ;
254+ CreateCanvasBitmap ( ) ;
255+ }
256+ }
257+
258+ private void CreateAndAssignCanvasVirtualImageSource ( )
259+ {
260+ _currentCanvasVirtualImageSource ??= new CanvasVirtualImageSource ( _currentCanvasDevice ,
261+ _currentCanvasWidth ,
262+ _currentCanvasHeight ,
263+ CanvasBaseDpi ) ;
264+
265+ _currentImage . Source = _currentCanvasVirtualImageSource . Source ;
266+ }
267+
268+ private void CreateCanvasBitmap ( )
269+ {
270+ int widthInt = ( int ) _currentCanvasWidth ;
271+ int heightInt = ( int ) _currentCanvasHeight ;
272+
273+ byte [ ] temporaryBuffer = ArrayPool < byte > . Shared . Rent ( widthInt * heightInt * 4 ) ;
274+ try
275+ {
276+ _currentCanvasBitmap ??= CanvasBitmap . CreateFromBytes ( _currentCanvasDevice ,
277+ temporaryBuffer ,
278+ widthInt ,
279+ heightInt ,
280+ Windows . Graphics . DirectX . DirectXPixelFormat . B8G8R8A8UIntNormalized ,
281+ CanvasBaseDpi ,
282+ CanvasAlphaMode . Ignore ) ;
283+ }
284+ finally
285+ {
286+ ArrayPool < byte > . Shared . Return ( temporaryBuffer ) ;
287+ }
288+ }
289+
253290 public void DisposeMediaModules ( )
254291 {
255292#if ! USEFFMPEGFORVIDEOBG
@@ -336,10 +373,10 @@ private async void FrameGrabberEvent(MediaPlayer mediaPlayer, object args)
336373 {
337374 lock ( _currentLock )
338375 {
339- if ( _currentCanvasVirtualImageSource is null )
340- {
341- return ;
342- }
376+ if ( _currentCanvasVirtualImageSource is null )
377+ {
378+ return ;
379+ }
343380
344381 if ( _currentCanvasDrawingSession is not null )
345382 {
@@ -355,7 +392,9 @@ private async void FrameGrabberEvent(MediaPlayer mediaPlayer, object args)
355392 lock ( _currentLock )
356393 {
357394 mediaPlayer . CopyFrameToVideoSurface ( _currentCanvasBitmap ) ;
358- _currentCanvasDrawingSession = _currentCanvasVirtualImageSource . CreateDrawingSession ( _currentDefaultColor , _currentCanvasDrawArea ) ;
395+ _currentCanvasDrawingSession = _currentCanvasVirtualImageSource
396+ . CreateDrawingSession ( _currentDefaultColor ,
397+ _currentCanvasDrawArea ) ;
359398 _currentCanvasDrawingSession . DrawImage ( _currentCanvasBitmap ) ;
360399 }
361400 }
0 commit comments