Skip to content

Commit 7b8d2cb

Browse files
author
LoneWandererProductions
committed
sync back changes
1 parent 64f9dd9 commit 7b8d2cb

1 file changed

Lines changed: 27 additions & 41 deletions

File tree

Imaging/ImageStreamMedia.cs

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ 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();
9897
bmp.CacheOption = BitmapCacheOption.OnLoad;
9998

10099
if (width > 0 && height > 0)
@@ -139,7 +138,7 @@ internal static Bitmap BitmapImageToBitmap(BitmapImage image)
139138
enc.Save(outStream);
140139
var bitmap = new Bitmap(outStream);
141140

142-
return bitmap;
141+
return new Bitmap(bitmap);
143142
}
144143

145144
/// <summary>
@@ -148,9 +147,7 @@ internal static Bitmap BitmapImageToBitmap(BitmapImage image)
148147
/// </summary>
149148
/// <param name="image">The bitmap.</param>
150149
/// <param name="lossless">if set to <c>true</c> [lossless].</param>
151-
/// <returns>
152-
/// BitmpaImage from Bitmap
153-
/// </returns>
150+
/// <returns>BitmpaImage from Bitmap</returns>
154151
/// <exception cref="ArgumentNullException">if Image is null</exception>
155152
/// The Image as
156153
/// <see cref="BitmapImage" />
@@ -201,77 +198,66 @@ internal static BitmapImage BitmapToBitmapImage(Bitmap image, bool lossless = fa
201198
/// Fasts the convert.
202199
/// </summary>
203200
/// <param name="bmp">The BMP.</param>
204-
/// <returns>
205-
/// BitmapImage from Bitmap.
206-
/// </returns>
201+
/// <returns>BitmapImage from Bitmap.</returns>
207202
private static BitmapImage FastConvert(Bitmap bmp)
208203
{
209-
// Ensure correct format for WPF
204+
// Ensure 32bpp ARGB
210205
if (bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
211206
{
212207
var converted = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
213-
using (var g = Graphics.FromImage(converted))
214-
{
215-
g.DrawImage(bmp, 0, 0);
216-
}
217-
218-
bmp.Dispose(); // replace original
208+
using var g = Graphics.FromImage(converted);
209+
g.DrawImage(bmp, 0, 0);
219210
bmp = converted;
220211
}
221212

222213
var rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
223-
var data = bmp.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
214+
var bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
224215

225216
try
226217
{
227-
var stride = Math.Abs(data.Stride);
228-
var bufferSize = stride * bmp.Height;
218+
// WPF erwartet Int32Rect und PixelFormats.Bgra32
219+
var wbRect = new Int32Rect(0, 0, bmp.Width, bmp.Height);
229220

230221
var wb = new WriteableBitmap(
231222
bmp.Width,
232223
bmp.Height,
233-
96,
234-
96,
224+
96, 96,
235225
PixelFormats.Bgra32,
236226
null);
237227

238-
wb.WritePixels(
239-
new Int32Rect(0, 0, bmp.Width, bmp.Height),
240-
data.Scan0,
241-
bufferSize,
242-
stride);
243-
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));
244230
wb.Freeze();
245231

246232
return WriteableBitmapToBitmapImage(wb);
247233
}
248234
finally
249235
{
250-
bmp.UnlockBits(data);
236+
bmp.UnlockBits(bmpData);
251237
}
252238
}
253239

254240
/// <summary>
255241
/// Writeables the bitmap to bitmap image.
256242
/// </summary>
257243
/// <param name="wb">The wb.</param>
258-
/// <returns>
259-
/// BitmapImage from Bitmap.
260-
/// </returns>
244+
/// <returns>BitmapImage from Bitmap.</returns>
261245
private static BitmapImage WriteableBitmapToBitmapImage(WriteableBitmap wb)
262246
{
247+
var encoder = new JpegBitmapEncoder(); // MUCH faster than PNG
248+
encoder.QualityLevel = 100; // visually lossless
249+
263250
using var ms = new MemoryStream();
264-
var enc = new BmpBitmapEncoder();
265-
enc.Frames.Add(BitmapFrame.Create(wb));
266-
enc.Save(ms);
251+
encoder.Frames.Add(BitmapFrame.Create(wb));
252+
encoder.Save(ms);
267253
ms.Position = 0;
268254

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;
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;
276262
}
277-
}
263+
}

0 commit comments

Comments
 (0)