I have code that is supposed to take a picture and convert it to a bitmap, and it works flawlessly on Windows, both in Debug and Release mode. However, on mobile, it says that creating a bitmap with a memory stream is not supported on the platform. Perplexingly, it completely ignored my try-catch statement and just kicked the bucket. How do I get around this issue?
Blazor code:
<button @onclick="TakePicture">Take Picture</button>
<p>@CameraErrorMessage</p>
@code {
string CameraErrorMessage = string.Empty;
private async Task TakePicture()
{
if (MediaPicker.Default.IsCaptureSupported)
{
FileResult photo = await MediaPicker.Default.CapturePhotoAsync();
if (photo != null)
{
try
{
using Stream sourceStream = await photo.OpenReadAsync();
var BitmapImage = new System.Drawing.Bitmap(sourceStream); //line the error appears on
} catch (Exception ex)
{
CameraErrorMessage = "Getting Bitmap Failed:n";
foreach (DictionaryEntry DataPair in ex.Data)
{
CameraErrorMessage += DataPair.Value??string.Empty.ToString() + "n";
}
}
}
else
{
CameraErrorMessage = "Failed to take image.";
}
}
}
}