Problem
I'm trying using ZXing.Net.Mobile to create barcode image in UWP project. I found this approach.
But in line var wb = result.ToBitmap() as WriteableBitmap;
I'm getting result as byte[], which doesn't have method ToBitmap()
.
Then I found this straight forward code
var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
var wb = writer.Write("12345678");
BarcodeImg.Source = wb;
but last lane throws error byte[] cannot be converted to ImageSource
In my naive mind I thought "Ok that should be easy". HA! Everywhere I look I find something similar to this answer.
BitmapImage
doesn't have methods BeginInit
and EndInit
.
Question
How I can convert byte[] to ImageSource or use ZXing to create one?
I'm stuck, again I'm not familiar with UPW. I'll be thankful for each tip.
Update
Ok at this moment situtation looks like this
private async void updateBarcodeImg(string code) {
var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
var wb = writer.Write(code) as Byte[];
try {
BarcodeImg.Source = await ImageFromBytes(wb);
} catch (Exception e) {
Debug.WriteLine(e.Message);
}
}
public async static Task<BitmapImage> ImageFromBytes(Byte[] bytes) {
BitmapImage image = new BitmapImage();
using (IRandomAccessStream stream = bytes.AsBuffer().AsStream().AsRandomAccessStream()) {
stream.Seek(0);
await image.SetSourceAsync(stream);
}
return image;
}
Update
And in line await image.SetSourceAsync(stream);
an exception is throwing "Exception from HRESULT: 0x88982F50". Google says that this is because stream is not set to position 0. But I do it a one line earlier.