Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I get error like this using SetBitmapAsync()?

I'm new to UWP app development and trying to build an image processing app. I was using the code in https://msdn.microsoft.com/en-us/windows/uwp/audio-video-camera/imaging However I got exception like this: SoftwareBitmapSource::SetBitmapAsync only supports SoftwareBitmap with positive width/height, bgra8 pixel format and pre-multiplied or no alpha. in code await source.SetBitmapAsync(sbitmap); I'm wondering if this method really has so many limitations and if so if there is any alternative I should use that has the least limitations. The code snippet is as following

FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
fileOpenPicker.FileTypeFilter.Add(".jpg");
fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;

var inputFile = await fileOpenPicker.PickSingleFileAsync();
if(inputFile != null)
{
    SoftwareBitmap sbitmap;
    using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
    {
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
        sbitmap = await decoder.GetSoftwareBitmapAsync();
    }
    var source = new SoftwareBitmapSource();
    await source.SetBitmapAsync(sbitmap);
    **Exception--->**imageControl.Source = source;
}
like image 517
litaoshen Avatar asked Oct 17 '25 09:10

litaoshen


1 Answers

This is a known issue. For now you can workaround this by using SoftwareBitmap.Convert to get a Bgra8 pre-multiplied SoftwareBitmap to display

SoftwareBitmap displayableImage = SoftwareBitmap.Convert(sbitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
like image 120
Trevor Baron Avatar answered Oct 19 '25 00:10

Trevor Baron