I need to do the following:
- Convert base64 image string into Bitmapimage
- from BitmapImage in (1), convert it to byteArray
The problem: how to solve the step(2)
Your help is greatly appreciated. Thanks
public async Task Base64StringToBitmap(string Base64source,string Filenm)
{
var bytes = Convert.FromBase64String(Base64source);
var ims = new InMemoryRandomAccessStream();
var dataWriter = new DataWriter(ims);
dataWriter.WriteBytes(bytes);
await dataWriter.StoreAsync();
ims.Seek(0);
//----- Create Bitmapimage ---------------
var bm = new BitmapImage();
bm.CreateOptions = BitmapCreateOptions.None;
bm.SetSource(ims);
// Update : added this
byte[] pixelBuffer = null;
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap wb = new WriteableBitmap(200, 300);
wb.SetSource(ims);
//-- Problem here :
Stream stm = wb.PixelBuffer.AsStream();
int len = (int)stm.Length;
byte[] pixels = new byte[len];
await stm.ReadAsync(pixels, 0, pixels.Length);
stm.CopyTo(ms);
pixelBuffer = ms.ToArray();
}
}