question

jtorjo avatar image
0 Votes"
jtorjo asked ·

How to find out if SoftwareBitmap is fully black, when not on UI thread?

Hi,

I have a SoftwareBitmap, and I'd like to know if it's fully black. This is due to a bug on MediaPlayer, but that's a totally different story.

I don't know how to convert it to a WritableBitmap on a non UI thread -- so, long story short, is it possible to get access to all the bytes from the software bitmap? I know it's Bgra8, so I could simply test each 4 bytes to be 0xff, 0, 0, 0.

How do I do that?

uwp
· 5
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you want to get Bytes from SoftwareBitmap, you need to use SoftwareBitmap.CopyToBuffer() method, you can also use this method to convert SoftwareBitmap to WriteableBitmap, but this needs to be done in the UI thread. Can you explain why you don't want to proceed in the UI thread?

0 Votes 0 · ·
jtorjo avatar image jtorjo RichardZhang-MSFT ·

I have hundreds of bitmaps that I need to check - I want to do it in parallel. I can't do it in the main thread, that would be a huge bottleneck.

0 Votes 0 · ·

From your problem description, your SoftwareBitmap should be a Frame image obtained from MediaPlayer, which itself exists in the UI thread and cannot be called in non-UI threads. So I would like to know what the bug of the MediaPlayer you are referring to, and why you should detect pure black pictures. But for this problem is concerned, you cannot directly handle SoftwareBitmap in non-UI thread.

0 Votes 0 · ·
Show more comments

1 Answer

jtorjo avatar image
0 Votes"
jtorjo answered ·

I'll post this here for posterity. It IS possible

 int width = 960, height = 540;
 SoftwareBitmap soft = new SoftwareBitmap(BitmapPixelFormat.Bgra8, width, height);
 ...
 var buffer = new Windows.Storage.Streams.Buffer ( (uint)( 4 * width * height));
 soft.CopyToBuffer(buffer);
 
 DataReader dr = DataReader.FromBuffer(buffer);
 var bytes = new byte[4 * width * height];
 dr.ReadBytes(bytes);

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.