2424using System . IO . Packaging ;
2525using UnsafeNativeMethods = MS . Win32 . PresentationCore . UnsafeNativeMethods ;
2626using SR = MS . Internal . PresentationCore . SR ;
27- using MS . Internal . PresentationCore ; // SecurityHelper
27+ using MS . Internal . PresentationCore ;
28+ using System . Runtime . CompilerServices ;
2829
2930namespace System . Windows . Media . Imaging
3031{
@@ -657,7 +658,7 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
657658 ArgumentNullException . ThrowIfNull ( pixels ) ;
658659
659660 if ( pixels . Rank != 1 )
660- throw new ArgumentException ( SR . Collection_BadRank , " pixels" ) ;
661+ throw new ArgumentException ( SR . Collection_BadRank , nameof ( pixels ) ) ;
661662
662663 if ( offset < 0 )
663664 {
@@ -680,43 +681,13 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
680681
681682 int destBufferSize = checked ( elementSize * ( pixels . Length - offset ) ) ;
682683
684+ // Check whether offset is out of bounds manually
685+ if ( offset >= pixels . Length )
686+ throw new IndexOutOfRangeException ( ) ;
683687
684- if ( pixels is byte [ ] )
685- {
686- fixed ( void * pixelArray = & ( ( byte [ ] ) pixels ) [ offset ] )
687- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
688- }
689- else if ( pixels is short [ ] )
690- {
691- fixed ( void * pixelArray = & ( ( short [ ] ) pixels ) [ offset ] )
692- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
693- }
694- else if ( pixels is ushort [ ] )
695- {
696- fixed ( void * pixelArray = & ( ( ushort [ ] ) pixels ) [ offset ] )
697- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
698- }
699- else if ( pixels is int [ ] )
700- {
701- fixed ( void * pixelArray = & ( ( int [ ] ) pixels ) [ offset ] )
702- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
703- }
704- else if ( pixels is uint [ ] )
705- {
706- fixed ( void * pixelArray = & ( ( uint [ ] ) pixels ) [ offset ] )
707- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
708- }
709- else if ( pixels is float [ ] )
710- {
711- fixed ( void * pixelArray = & ( ( float [ ] ) pixels ) [ offset ] )
712- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
713- }
714- else if ( pixels is double [ ] )
715- {
716- fixed ( void * pixelArray = & ( ( double [ ] ) pixels ) [ offset ] )
717- CriticalCopyPixels ( sourceRect , ( IntPtr ) pixelArray , destBufferSize , stride ) ;
718- }
719- }
688+ fixed ( byte * pixelArray = & Unsafe . AddByteOffset ( ref MemoryMarshal . GetArrayDataReference ( pixels ) , ( nint ) offset * elementSize ) )
689+ CriticalCopyPixels ( sourceRect , ( nint ) pixelArray , destBufferSize , stride ) ;
690+ }
720691
721692 /// <summary>
722693 /// CriticalCopyPixels
@@ -728,7 +699,7 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int
728699 internal void CriticalCopyPixels ( Int32Rect sourceRect , IntPtr buffer , int bufferSize , int stride )
729700 {
730701 if ( buffer == IntPtr . Zero )
731- throw new ArgumentNullException ( " buffer" ) ;
702+ throw new ArgumentNullException ( nameof ( buffer ) ) ;
732703
733704 ArgumentOutOfRangeException . ThrowIfNegativeOrZero ( stride ) ;
734705
0 commit comments