|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
| 5 | +using System.Windows.Media.Composition; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Runtime.InteropServices; |
5 | 8 | using System.IO; |
6 | 9 | using MS.Internal; |
7 | | -using System.Runtime.InteropServices; |
8 | | -using System.Windows.Media.Composition; |
9 | 10 | using MS.Win32; |
10 | 11 |
|
11 | 12 | using UnsafeNativeMethods = MS.Win32.PresentationCore.UnsafeNativeMethods; |
@@ -663,43 +664,13 @@ unsafe internal void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int |
663 | 664 |
|
664 | 665 | int destBufferSize = checked(elementSize * (pixels.Length - offset)); |
665 | 666 |
|
| 667 | + // Check whether offset is out of bounds manually |
| 668 | + if (offset >= pixels.Length) |
| 669 | + throw new IndexOutOfRangeException(); |
666 | 670 |
|
667 | | - if (pixels is byte[]) |
668 | | - { |
669 | | - fixed (void* pixelArray = &((byte[])pixels)[offset]) |
670 | | - CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride); |
671 | | - } |
672 | | - else if (pixels is short[]) |
673 | | - { |
674 | | - fixed (void* pixelArray = &((short[])pixels)[offset]) |
675 | | - CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride); |
676 | | - } |
677 | | - else if (pixels is ushort[]) |
678 | | - { |
679 | | - fixed (void* pixelArray = &((ushort[])pixels)[offset]) |
680 | | - CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride); |
681 | | - } |
682 | | - else if (pixels is int[]) |
683 | | - { |
684 | | - fixed (void* pixelArray = &((int[])pixels)[offset]) |
685 | | - CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride); |
686 | | - } |
687 | | - else if (pixels is uint[]) |
688 | | - { |
689 | | - fixed (void* pixelArray = &((uint[])pixels)[offset]) |
690 | | - CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride); |
691 | | - } |
692 | | - else if (pixels is float[]) |
693 | | - { |
694 | | - fixed (void* pixelArray = &((float[])pixels)[offset]) |
695 | | - CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride); |
696 | | - } |
697 | | - else if (pixels is double[]) |
698 | | - { |
699 | | - fixed (void* pixelArray = &((double[])pixels)[offset]) |
700 | | - CriticalCopyPixels(sourceRect, (IntPtr)pixelArray, destBufferSize, stride); |
701 | | - } |
702 | | -} |
| 671 | + fixed (byte* pixelArray = &Unsafe.AddByteOffset(ref MemoryMarshal.GetArrayDataReference(pixels), (nint)offset * elementSize)) |
| 672 | + CriticalCopyPixels(sourceRect, (nint)pixelArray, destBufferSize, stride); |
| 673 | + } |
703 | 674 |
|
704 | 675 | /// <summary> |
705 | 676 | /// CriticalCopyPixels |
|
0 commit comments