Skip to content

Commit 2fa8170

Browse files
authored
Simplify constructor in CachedBitmap by removing duplicate type checks (#9333)
* replace ineffective array type-checking with simple arrayref pinning * Add missing using after automerge
1 parent b115680 commit 2fa8170

File tree

1 file changed

+5
-52
lines changed
  • src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging

1 file changed

+5
-52
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CachedBitmap.cs

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
3-
// See the LICENSE file in the project root for more information.
43

5-
using MS.Internal;
4+
using System.Runtime.InteropServices;
65
using MS.Win32.PresentationCore;
6+
using MS.Internal;
77

88
namespace System.Windows.Media.Imaging
99
{
@@ -113,7 +113,7 @@ int stride
113113
ArgumentNullException.ThrowIfNull(pixels);
114114

115115
if (pixels.Rank != 1)
116-
throw new ArgumentException(SR.Collection_BadRank, "pixels");
116+
throw new ArgumentException(SR.Collection_BadRank, nameof(pixels));
117117

118118
int elementSize = -1;
119119

@@ -131,55 +131,8 @@ int stride
131131

132132
int destBufferSize = elementSize * pixels.Length;
133133

134-
if (pixels is byte[])
135-
{
136-
fixed(void * pixelArray = (byte[])pixels)
137-
InitFromMemoryPtr(pixelWidth, pixelHeight, dpiX, dpiY,
138-
pixelFormat, palette,
139-
(IntPtr)pixelArray, destBufferSize, stride);
140-
}
141-
else if (pixels is short[])
142-
{
143-
fixed(void * pixelArray = (short[])pixels)
144-
InitFromMemoryPtr(pixelWidth, pixelHeight, dpiX, dpiY,
145-
pixelFormat, palette,
146-
(IntPtr)pixelArray, destBufferSize, stride);
147-
}
148-
else if (pixels is ushort[])
149-
{
150-
fixed(void * pixelArray = (ushort[])pixels)
151-
InitFromMemoryPtr(pixelWidth, pixelHeight, dpiX, dpiY,
152-
pixelFormat, palette,
153-
(IntPtr)pixelArray, destBufferSize, stride);
154-
}
155-
else if (pixels is int[])
156-
{
157-
fixed(void * pixelArray = (int[])pixels)
158-
InitFromMemoryPtr(pixelWidth, pixelHeight, dpiX, dpiY,
159-
pixelFormat, palette,
160-
(IntPtr)pixelArray, destBufferSize, stride);
161-
}
162-
else if (pixels is uint[])
163-
{
164-
fixed(void * pixelArray = (uint[])pixels)
165-
InitFromMemoryPtr(pixelWidth, pixelHeight, dpiX, dpiY,
166-
pixelFormat, palette,
167-
(IntPtr)pixelArray, destBufferSize, stride);
168-
}
169-
else if (pixels is float[])
170-
{
171-
fixed(void * pixelArray = (float[])pixels)
172-
InitFromMemoryPtr(pixelWidth, pixelHeight, dpiX, dpiY,
173-
pixelFormat, palette,
174-
(IntPtr)pixelArray, destBufferSize, stride);
175-
}
176-
else if (pixels is double[])
177-
{
178-
fixed(void * pixelArray = (double[])pixels)
179-
InitFromMemoryPtr(pixelWidth, pixelHeight, dpiX, dpiY,
180-
pixelFormat, palette,
181-
(IntPtr)pixelArray, destBufferSize, stride);
182-
}
134+
fixed (byte* pixelArray = &MemoryMarshal.GetArrayDataReference(pixels))
135+
InitFromMemoryPtr(pixelWidth, pixelHeight, dpiX, dpiY, pixelFormat, palette, (nint)pixelArray, destBufferSize, stride);
183136
}
184137

185138
/// <summary>

0 commit comments

Comments
 (0)