Code: Select all
static async Task ConvertBitmapImageToSoftwareBitmapAsync(BitmapImage bitmapImage)
{
try
{
if (bitmapImage.PixelWidth == 0 || bitmapImage.PixelHeight == 0)
{
Debug.WriteLine($"[WARNING] The width and height are not valid.");
Debugger.Break();
}
InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, memoryStream);
// Create dummy SoftwareBitmap (since BitmapImage doesn't have direct pixel access)
SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, bitmapImage.PixelWidth, bitmapImage.PixelHeight, BitmapAlphaMode.Premultiplied);
encoder.SetSoftwareBitmap(softwareBitmap);
await encoder.FlushAsync();
return softwareBitmap;
}
catch (Exception ex)
{
Debug.WriteLine($"[ERROR] {ex.Message}");
return null;
}
}