@@ -156,19 +156,20 @@ public virtual void SetPixels(QuantumType[] values)
156156 public virtual byte [ ] ? ToByteArray ( int x , int y , uint width , uint height , string mapping )
157157 {
158158 var nativeResult = IntPtr . Zero ;
159- byte [ ] ? result = null ;
159+
160+ var length = width * height * mapping . Length ;
161+ if ( length > int . MaxValue )
162+ throw new InvalidOperationException ( "The resulting array is too large." ) ;
160163
161164 try
162165 {
163166 nativeResult = NativeInstance . ToByteArray ( x , y , width , height , mapping ) ;
164- result = ByteConverter . ToArray ( nativeResult , ( int ) ( width * height * mapping . Length ) ) ;
167+ return ByteConverter . ToArray ( nativeResult , ( int ) length ) ;
165168 }
166169 finally
167170 {
168171 MagickMemory . Relinquish ( nativeResult ) ;
169172 }
170-
171- return result ;
172173 }
173174
174175 public virtual byte [ ] ? ToByteArray ( int x , int y , uint width , uint height , PixelMapping mapping )
@@ -190,10 +191,14 @@ public virtual void SetPixels(QuantumType[] values)
190191 {
191192 var nativeResult = IntPtr . Zero ;
192193
194+ var length = width * height * mapping . Length ;
195+ if ( length > int . MaxValue )
196+ throw new InvalidOperationException ( "The resulting array is too large." ) ;
197+
193198 try
194199 {
195200 nativeResult = NativeInstance . ToShortArray ( x , y , width , height , mapping ) ;
196- return ShortConverter . ToArray ( nativeResult , ( int ) ( width * height * mapping . Length ) ) ;
201+ return ShortConverter . ToArray ( nativeResult , ( int ) length ) ;
197202 }
198203 finally
199204 {
0 commit comments