@@ -94,6 +94,7 @@ public static BitmapImage GetBitmapImageFileStream(string path, int width = 0, i
9494 {
9595 using var flStream = new FileStream ( path , FileMode . Open , FileAccess . Read ) ;
9696 bmp . BeginInit ( ) ;
97+ bmp . Freeze ( ) ;
9798 bmp . CacheOption = BitmapCacheOption . OnLoad ;
9899
99100 if ( width > 0 && height > 0 )
@@ -138,7 +139,7 @@ internal static Bitmap BitmapImageToBitmap(BitmapImage image)
138139 enc . Save ( outStream ) ;
139140 var bitmap = new Bitmap ( outStream ) ;
140141
141- return new Bitmap ( bitmap ) ;
142+ return bitmap ;
142143 }
143144
144145 /// <summary>
@@ -147,7 +148,9 @@ internal static Bitmap BitmapImageToBitmap(BitmapImage image)
147148 /// </summary>
148149 /// <param name="image">The bitmap.</param>
149150 /// <param name="lossless">if set to <c>true</c> [lossless].</param>
150- /// <returns>BitmpaImage from Bitmap</returns>
151+ /// <returns>
152+ /// BitmpaImage from Bitmap
153+ /// </returns>
151154 /// <exception cref="ArgumentNullException">if Image is null</exception>
152155 /// The Image as
153156 /// <see cref="BitmapImage" />
@@ -198,66 +201,77 @@ internal static BitmapImage BitmapToBitmapImage(Bitmap image, bool lossless = fa
198201 /// Fasts the convert.
199202 /// </summary>
200203 /// <param name="bmp">The BMP.</param>
201- /// <returns>BitmapImage from Bitmap.</returns>
204+ /// <returns>
205+ /// BitmapImage from Bitmap.
206+ /// </returns>
202207 private static BitmapImage FastConvert ( Bitmap bmp )
203208 {
204- // Ensure 32bpp ARGB
209+ // Ensure correct format for WPF
205210 if ( bmp . PixelFormat != System . Drawing . Imaging . PixelFormat . Format32bppArgb )
206211 {
207212 var converted = new Bitmap ( bmp . Width , bmp . Height , System . Drawing . Imaging . PixelFormat . Format32bppArgb ) ;
208- using var g = Graphics . FromImage ( converted ) ;
209- g . DrawImage ( bmp , 0 , 0 ) ;
213+ using ( var g = Graphics . FromImage ( converted ) )
214+ {
215+ g . DrawImage ( bmp , 0 , 0 ) ;
216+ }
217+
218+ bmp . Dispose ( ) ; // replace original
210219 bmp = converted ;
211220 }
212221
213222 var rect = new System . Drawing . Rectangle ( 0 , 0 , bmp . Width , bmp . Height ) ;
214- var bmpData = bmp . LockBits ( rect , ImageLockMode . ReadOnly , System . Drawing . Imaging . PixelFormat . Format32bppArgb ) ;
223+ var data = bmp . LockBits ( rect , ImageLockMode . ReadOnly , System . Drawing . Imaging . PixelFormat . Format32bppArgb ) ;
215224
216225 try
217226 {
218- // WPF erwartet Int32Rect und PixelFormats.Bgra32
219- var wbRect = new Int32Rect ( 0 , 0 , bmp . Width , bmp . Height ) ;
227+ var stride = Math . Abs ( data . Stride ) ;
228+ var bufferSize = stride * bmp . Height ;
220229
221230 var wb = new WriteableBitmap (
222231 bmp . Width ,
223232 bmp . Height ,
224- 96 , 96 ,
233+ 96 ,
234+ 96 ,
225235 PixelFormats . Bgra32 ,
226236 null ) ;
227237
228- // Verwende die IntPtr-Überladung: WritePixels(Int32Rect, IntPtr buffer, int bufferSize, int stride)
229- wb . WritePixels ( wbRect , bmpData . Scan0 , Math . Abs ( bmpData . Stride ) * bmp . Height , Math . Abs ( bmpData . Stride ) ) ;
238+ wb . WritePixels (
239+ new Int32Rect ( 0 , 0 , bmp . Width , bmp . Height ) ,
240+ data . Scan0 ,
241+ bufferSize ,
242+ stride ) ;
243+
230244 wb . Freeze ( ) ;
231245
232246 return WriteableBitmapToBitmapImage ( wb ) ;
233247 }
234248 finally
235249 {
236- bmp . UnlockBits ( bmpData ) ;
250+ bmp . UnlockBits ( data ) ;
237251 }
238252 }
239253
240254 /// <summary>
241255 /// Writeables the bitmap to bitmap image.
242256 /// </summary>
243257 /// <param name="wb">The wb.</param>
244- /// <returns>BitmapImage from Bitmap.</returns>
258+ /// <returns>
259+ /// BitmapImage from Bitmap.
260+ /// </returns>
245261 private static BitmapImage WriteableBitmapToBitmapImage ( WriteableBitmap wb )
246262 {
247- var encoder = new JpegBitmapEncoder ( ) ; // MUCH faster than PNG
248- encoder . QualityLevel = 100 ; // visually lossless
249-
250263 using var ms = new MemoryStream ( ) ;
251- encoder . Frames . Add ( BitmapFrame . Create ( wb ) ) ;
252- encoder . Save ( ms ) ;
264+ var enc = new BmpBitmapEncoder ( ) ;
265+ enc . Frames . Add ( BitmapFrame . Create ( wb ) ) ;
266+ enc . Save ( ms ) ;
253267 ms . Position = 0 ;
254268
255- var bmp = new BitmapImage ( ) ;
256- bmp . BeginInit ( ) ;
257- bmp . CacheOption = BitmapCacheOption . OnLoad ;
258- bmp . StreamSource = ms ;
259- bmp . EndInit ( ) ;
260- bmp . Freeze ( ) ;
261- return bmp ;
269+ var img = new BitmapImage ( ) ;
270+ img . BeginInit ( ) ;
271+ img . CacheOption = BitmapCacheOption . OnLoad ;
272+ img . StreamSource = ms ;
273+ img . EndInit ( ) ;
274+ img . Freeze ( ) ;
275+ return img ;
262276 }
263- }
277+ }
0 commit comments