2424using System . IO . Packaging ;
2525using UnsafeNativeMethods = MS . Win32 . PresentationCore . UnsafeNativeMethods ;
2626using SR = MS . Internal . PresentationCore . SR ;
27+ using MS . Internal . PresentationCore ;
28+ using System . Runtime . CompilerServices ;
2729
2830namespace System . Windows . Media . Imaging
2931{
@@ -655,7 +657,7 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
655657 ArgumentNullException . ThrowIfNull ( pixels ) ;
656658
657659 if ( pixels . Rank != 1 )
658- throw new ArgumentException ( SR . Collection_BadRank , " pixels" ) ;
660+ throw new ArgumentException ( SR . Collection_BadRank , nameof ( pixels ) ) ;
659661
660662 if ( offset < 0 )
661663 {
@@ -678,43 +680,13 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
678680
679681 int destBufferSize = checked ( elementSize * ( pixels . Length - offset ) ) ;
680682
683+ // Check whether offset is out of bounds manually
684+ if ( offset >= pixels . Length )
685+ throw new IndexOutOfRangeException ( ) ;
681686
682- if ( pixels is byte [ ] )
683- {
684- fixed ( void * pixelArray = & ( ( byte [ ] ) pixels ) [ offset ] )
685- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
686- }
687- else if ( pixels is short [ ] )
688- {
689- fixed ( void * pixelArray = & ( ( short [ ] ) pixels ) [ offset ] )
690- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
691- }
692- else if ( pixels is ushort [ ] )
693- {
694- fixed ( void * pixelArray = & ( ( ushort [ ] ) pixels ) [ offset ] )
695- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
696- }
697- else if ( pixels is int [ ] )
698- {
699- fixed ( void * pixelArray = & ( ( int [ ] ) pixels ) [ offset ] )
700- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
701- }
702- else if ( pixels is uint [ ] )
703- {
704- fixed ( void * pixelArray = & ( ( uint [ ] ) pixels ) [ offset ] )
705- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
706- }
707- else if ( pixels is float [ ] )
708- {
709- fixed ( void * pixelArray = & ( ( float [ ] ) pixels ) [ offset ] )
710- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
711- }
712- else if ( pixels is double [ ] )
713- {
714- fixed ( void * pixelArray = & ( ( double [ ] ) pixels ) [ offset ] )
715- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
716- }
717- }
687+ fixed ( byte * pixelArray = & Unsafe . AddByteOffset ( ref MemoryMarshal . GetArrayDataReference ( pixels ) , ( nint ) offset * elementSize ) )
688+ CriticalCopyPixels ( sourceRect , ( nint ) pixelArray , destBufferSize , stride ) ;
689+ }
718690
719691 /// <summary>
720692 /// CriticalCopyPixels
@@ -726,7 +698,7 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
726698 internal void CriticalCopyPixels ( Int32Rect sourceRect , IntPtr buffer , int bufferSize , int stride )
727699 {
728700 if ( buffer == IntPtr . Zero )
729- throw new ArgumentNullException ( " buffer" ) ;
701+ throw new ArgumentNullException ( nameof ( buffer ) ) ;
730702
731703 ArgumentOutOfRangeException . ThrowIfNegativeOrZero ( stride ) ;
732704
0 commit comments