diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs index dc25bdf08d11c..68689fff1bebb 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs @@ -664,32 +664,23 @@ public void Save(Stream outputStream) } } - private void CopyBitmapData(BitmapData sourceData, BitmapData targetData) + private unsafe void CopyBitmapData(BitmapData sourceData, BitmapData targetData) { - int offsetSrc = 0; - int offsetDest = 0; + byte* srcPtr = (byte*)sourceData.Scan0; + byte* destPtr = (byte*)targetData.Scan0; Debug.Assert(sourceData.Height == targetData.Height, "Unexpected height. How did this happen?"); + int height = Math.Min(sourceData.Height, targetData.Height); + long bytesToCopyEachIter = Math.Abs(targetData.Stride); - for (int i = 0; i < Math.Min(sourceData.Height, targetData.Height); i++) + for (int i = 0; i < height; i++) { - IntPtr srcPtr, destPtr; - if (IntPtr.Size == 4) - { - srcPtr = new IntPtr(sourceData.Scan0.ToInt32() + offsetSrc); - destPtr = new IntPtr(targetData.Scan0.ToInt32() + offsetDest); - } - else - { - srcPtr = new IntPtr(sourceData.Scan0.ToInt64() + offsetSrc); - destPtr = new IntPtr(targetData.Scan0.ToInt64() + offsetDest); - } - - UnsafeNativeMethods.CopyMemory(new HandleRef(this, destPtr), new HandleRef(this, srcPtr), Math.Abs(targetData.Stride)); - - offsetSrc += sourceData.Stride; - offsetDest += targetData.Stride; + Buffer.MemoryCopy(srcPtr, destPtr, bytesToCopyEachIter, bytesToCopyEachIter); + srcPtr += sourceData.Stride; + destPtr += targetData.Stride; } + + GC.KeepAlive(this); // finalizer mustn't deallocate data blobs while this method is running } private static bool BitmapHasAlpha(BitmapData bmpData) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs index 14093caee6f5e..00aa1233af043 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs @@ -7,9 +7,6 @@ namespace System.Drawing { internal class UnsafeNativeMethods { - [DllImport(ExternDll.Kernel32, SetLastError = true, ExactSpelling = true, EntryPoint = "RtlMoveMemory")] - public static extern void CopyMemory(HandleRef destData, HandleRef srcData, int size); - [DllImport(ExternDll.Kernel32, SetLastError = true)] public static extern int GetSystemDefaultLCID();