@@ -39,32 +39,39 @@ internal static unsafe Image<TPixel> From32bppArgbSystemDrawingBitmap<TPixel>(Bi
3939 }
4040
4141 BitmapData data = bmp . LockBits ( fullRect , ImageLockMode . ReadWrite , bmp . PixelFormat ) ;
42- byte * sourcePtrBase = ( byte * ) data . Scan0 ;
42+ var image = new Image < TPixel > ( w , h ) ;
43+ try
44+ {
45+ byte * sourcePtrBase = ( byte * ) data . Scan0 ;
4346
44- long sourceRowByteCount = data . Stride ;
45- long destRowByteCount = w * sizeof ( Bgra32 ) ;
47+ long sourceRowByteCount = data . Stride ;
48+ long destRowByteCount = w * sizeof ( Bgra32 ) ;
4649
47- var image = new Image < TPixel > ( w , h ) ;
48- Configuration configuration = image . GetConfiguration ( ) ;
50+ Configuration configuration = image . GetConfiguration ( ) ;
4951
50- using ( IMemoryOwner < Bgra32 > workBuffer = Configuration . Default . MemoryAllocator . Allocate < Bgra32 > ( w ) )
51- {
52- fixed ( Bgra32 * destPtr = & workBuffer . GetReference ( ) )
52+ using ( IMemoryOwner < Bgra32 > workBuffer = Configuration . Default . MemoryAllocator . Allocate < Bgra32 > ( w ) )
5353 {
54- for ( int y = 0 ; y < h ; y ++ )
54+ fixed ( Bgra32 * destPtr = & workBuffer . GetReference ( ) )
5555 {
56- Span < TPixel > row = image . Frames . RootFrame . GetPixelRowSpan ( y ) ;
57-
58- byte * sourcePtr = sourcePtrBase + ( data . Stride * y ) ;
59-
60- Buffer . MemoryCopy ( sourcePtr , destPtr , destRowByteCount , sourceRowByteCount ) ;
61- PixelOperations < TPixel > . Instance . FromBgra32 (
62- configuration ,
63- workBuffer . GetSpan ( ) . Slice ( 0 , w ) ,
64- row ) ;
56+ for ( int y = 0 ; y < h ; y ++ )
57+ {
58+ Span < TPixel > row = image . Frames . RootFrame . GetPixelRowSpan ( y ) ;
59+
60+ byte * sourcePtr = sourcePtrBase + ( data . Stride * y ) ;
61+
62+ Buffer . MemoryCopy ( sourcePtr , destPtr , destRowByteCount , sourceRowByteCount ) ;
63+ PixelOperations < TPixel > . Instance . FromBgra32 (
64+ configuration ,
65+ workBuffer . GetSpan ( ) . Slice ( 0 , w ) ,
66+ row ) ;
67+ }
6568 }
6669 }
6770 }
71+ finally
72+ {
73+ bmp . UnlockBits ( data ) ;
74+ }
6875
6976 return image ;
7077 }
@@ -91,33 +98,36 @@ internal static unsafe Image<TPixel> From24bppRgbSystemDrawingBitmap<TPixel>(Bit
9198 }
9299
93100 BitmapData data = bmp . LockBits ( fullRect , ImageLockMode . ReadWrite , bmp . PixelFormat ) ;
94- byte * sourcePtrBase = ( byte * ) data . Scan0 ;
101+ var image = new Image < TPixel > ( w , h ) ;
102+ try
103+ {
104+ byte * sourcePtrBase = ( byte * ) data . Scan0 ;
95105
96- long sourceRowByteCount = data . Stride ;
97- long destRowByteCount = w * sizeof ( Bgr24 ) ;
106+ long sourceRowByteCount = data . Stride ;
107+ long destRowByteCount = w * sizeof ( Bgr24 ) ;
98108
99- var image = new Image < TPixel > ( w , h ) ;
100- Configuration configuration = image . GetConfiguration ( ) ;
109+ Configuration configuration = image . GetConfiguration ( ) ;
101110
102- using ( IMemoryOwner < Bgr24 > workBuffer = Configuration . Default . MemoryAllocator . Allocate < Bgr24 > ( w ) )
103- {
104- fixed ( Bgr24 * destPtr = & workBuffer . GetReference ( ) )
111+ using ( IMemoryOwner < Bgr24 > workBuffer = Configuration . Default . MemoryAllocator . Allocate < Bgr24 > ( w ) )
105112 {
106- for ( int y = 0 ; y < h ; y ++ )
113+ fixed ( Bgr24 * destPtr = & workBuffer . GetReference ( ) )
107114 {
108- Span < TPixel > row = image . Frames . RootFrame . GetPixelRowSpan ( y ) ;
115+ for ( int y = 0 ; y < h ; y ++ )
116+ {
117+ Span < TPixel > row = image . Frames . RootFrame . GetPixelRowSpan ( y ) ;
109118
110- byte * sourcePtr = sourcePtrBase + ( data . Stride * y ) ;
119+ byte * sourcePtr = sourcePtrBase + ( data . Stride * y ) ;
111120
112- Buffer . MemoryCopy ( sourcePtr , destPtr , destRowByteCount , sourceRowByteCount ) ;
113- PixelOperations < TPixel > . Instance . FromBgr24 (
114- configuration ,
115- workBuffer . GetSpan ( ) . Slice ( 0 , w ) ,
116- row ) ;
121+ Buffer . MemoryCopy ( sourcePtr , destPtr , destRowByteCount , sourceRowByteCount ) ;
122+ PixelOperations < TPixel > . Instance . FromBgr24 ( configuration , workBuffer . GetSpan ( ) . Slice ( 0 , w ) , row ) ;
123+ }
117124 }
118125 }
119126 }
120-
127+ finally
128+ {
129+ bmp . UnlockBits ( data ) ;
130+ }
121131 return image ;
122132 }
123133
@@ -131,27 +141,32 @@ internal static unsafe Bitmap To32bppArgbSystemDrawingBitmap<TPixel>(Image<TPixe
131141 var resultBitmap = new Bitmap ( w , h , PixelFormat . Format32bppArgb ) ;
132142 var fullRect = new Rectangle ( 0 , 0 , w , h ) ;
133143 BitmapData data = resultBitmap . LockBits ( fullRect , ImageLockMode . ReadWrite , resultBitmap . PixelFormat ) ;
134- byte * destPtrBase = ( byte * ) data . Scan0 ;
144+ try
145+ {
146+ byte * destPtrBase = ( byte * ) data . Scan0 ;
135147
136- long destRowByteCount = data . Stride ;
137- long sourceRowByteCount = w * sizeof ( Bgra32 ) ;
148+ long destRowByteCount = data . Stride ;
149+ long sourceRowByteCount = w * sizeof ( Bgra32 ) ;
138150
139- using ( IMemoryOwner < Bgra32 > workBuffer = image . GetConfiguration ( ) . MemoryAllocator . Allocate < Bgra32 > ( w ) )
140- {
141- fixed ( Bgra32 * sourcePtr = & workBuffer . GetReference ( ) )
151+ using ( IMemoryOwner < Bgra32 > workBuffer = image . GetConfiguration ( ) . MemoryAllocator . Allocate < Bgra32 > ( w ) )
142152 {
143- for ( int y = 0 ; y < h ; y ++ )
153+ fixed ( Bgra32 * sourcePtr = & workBuffer . GetReference ( ) )
144154 {
145- Span < TPixel > row = image . Frames . RootFrame . GetPixelRowSpan ( y ) ;
146- PixelOperations < TPixel > . Instance . ToBgra32 ( configuration , row , workBuffer . GetSpan ( ) ) ;
147- byte * destPtr = destPtrBase + ( data . Stride * y ) ;
148-
149- Buffer . MemoryCopy ( sourcePtr , destPtr , destRowByteCount , sourceRowByteCount ) ;
155+ for ( int y = 0 ; y < h ; y ++ )
156+ {
157+ Span < TPixel > row = image . Frames . RootFrame . GetPixelRowSpan ( y ) ;
158+ PixelOperations < TPixel > . Instance . ToBgra32 ( configuration , row , workBuffer . GetSpan ( ) ) ;
159+ byte * destPtr = destPtrBase + ( data . Stride * y ) ;
160+
161+ Buffer . MemoryCopy ( sourcePtr , destPtr , destRowByteCount , sourceRowByteCount ) ;
162+ }
150163 }
151164 }
152165 }
153-
154- resultBitmap . UnlockBits ( data ) ;
166+ finally
167+ {
168+ resultBitmap . UnlockBits ( data ) ;
169+ }
155170
156171 return resultBitmap ;
157172 }
0 commit comments