88// ReSharper disable once CheckNamespace
99namespace NumSharp
1010{
11-
1211 [ SuppressMessage ( "ReSharper" , "SuggestVarOrType_SimpleTypes" ) ]
1312 [ SuppressMessage ( "ReSharper" , "UnusedMember.Global" ) ]
1413 public static class np_
@@ -59,11 +58,28 @@ public static unsafe NDArray ToNDArray(this System.Drawing.Bitmap image, bool fl
5958 }
6059 finally
6160 {
62- image . UnlockBits ( bmpData ) ;
61+ try
62+ {
63+ image . UnlockBits ( bmpData ) ;
64+ }
65+ catch ( ArgumentException )
66+ {
67+ //swallow
68+ }
6369 }
6470 else
6571 {
66- var ret = new NDArray ( new ArraySlice < byte > ( new UnmanagedMemoryBlock < byte > ( ( byte * ) bmpData . Scan0 , bmpData . Stride * bmpData . Height , ( ) => image . UnlockBits ( bmpData ) ) ) ) ;
72+ var ret = new NDArray ( new ArraySlice < byte > ( new UnmanagedMemoryBlock < byte > ( ( byte * ) bmpData . Scan0 , bmpData . Stride * bmpData . Height , ( ) =>
73+ {
74+ try
75+ {
76+ image . UnlockBits ( bmpData ) ;
77+ }
78+ catch ( ArgumentException )
79+ {
80+ //swallow
81+ }
82+ } ) ) ) ;
6783
6884 if ( flat )
6985 {
@@ -72,15 +88,15 @@ public static unsafe NDArray ToNDArray(this System.Drawing.Bitmap image, bool fl
7288 if ( bmpData . Stride / bmpData . Width == 4 ) //1byte-per-color
7389 {
7490 return ret . reshape ( 1 , bmpData . Height , bmpData . Width , bmpData . Stride / bmpData . Width ) //reshape
75- [ Slice . All , Slice . All , Slice . All , new Slice ( stop : 3 ) ] //slice
76- . flat ; //flatten
91+ [ Slice . All , Slice . All , Slice . All , new Slice ( stop : 3 ) ] //slice
92+ . flat ; //flatten
7793 }
7894
7995 if ( bmpData . Stride / bmpData . Width == 8 ) //2bytes-per-color
8096 {
8197 return ret . reshape ( 1 , bmpData . Height , bmpData . Width , bmpData . Stride / bmpData . Width ) //reshape
82- [ Slice . All , Slice . All , Slice . All , new Slice ( stop : 6 ) ] //slice
83- . flat ; //flatten
98+ [ Slice . All , Slice . All , Slice . All , new Slice ( stop : 6 ) ] //slice
99+ . flat ; //flatten
84100 }
85101
86102 throw new NotSupportedException ( $ "Given bbp ({ bmpData . Stride / bmpData . Width } ) is not supported.") ;
@@ -152,8 +168,8 @@ public static unsafe NDArray AsNDArray(this BitmapData bmpData, bool flat = fals
152168 if ( ret . shape [ 3 ] == 4 && discardAlpha )
153169 {
154170 return ret . reshape ( 1 , bmpData . Height , bmpData . Width , bmpData . Stride / bmpData . Width ) //reshape
155- [ Slice . All , Slice . All , Slice . All , new Slice ( stop : 3 ) ] //slice
156- . flat ; //flatten
171+ [ Slice . All , Slice . All , Slice . All , new Slice ( stop : 3 ) ] //slice
172+ . flat ; //flatten
157173 }
158174
159175 return ret ;
@@ -193,15 +209,13 @@ public static unsafe Bitmap ToBitmap(this NDArray nd, int width, int height, Pix
193209 if ( nd . shape [ 0 ] != 1 )
194210 throw new ArgumentException ( $ "ndarray has more than one picture in it ({ nd . shape [ 0 ] } ) based on the first dimension.") ;
195211
196-
197212 var bbp = nd . shape [ 3 ] ; //bytes per pixel.
198213 if ( bbp != extractFormatNumber ( ) )
199214 throw new ArgumentException ( $ "Given PixelFormat: { format } does not match the number of bytes per pixel in the 4th dimension of given ndarray.") ;
200215
201216 if ( bbp * width * height != nd . size )
202217 throw new ArgumentException ( $ "The expected size does not match the size of given ndarray. (expected: { bbp * width * height } , actual: { nd . size } )") ;
203218
204-
205219 var ret = new Bitmap ( width , height , format ) ;
206220 var bitdata = ret . LockBits ( new Rectangle ( 0 , 0 , width , height ) , ImageLockMode . WriteOnly , format ) ;
207221 try
@@ -214,7 +228,14 @@ public static unsafe Bitmap ToBitmap(this NDArray nd, int width, int height, Pix
214228 }
215229 finally
216230 {
217- ret . UnlockBits ( bitdata ) ;
231+ try
232+ {
233+ ret . UnlockBits ( bitdata ) ;
234+ }
235+ catch ( ArgumentException )
236+ {
237+ //swallow
238+ }
218239 }
219240
220241 return ret ;
0 commit comments