locked
Convert BitmapImage to Byte Array

    Question

  • Hi, I need to do the following:

    1) Convert base64 image string into Bitmapimage

    2) Convert (1) BitmapImage to Byte Array.

    I search at great length and it seems I could not find one for WinRT which involve IRandomAccessStream

    The problem:

    How to solve the step(2). If below code is able to handle the task?

    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);

     How to covert this BitmapImage to Byte Array ?

    }

     

     

    Thursday, March 13, 2014 7:54 AM

All replies

  • You need to use a WriteableBitmap rather than a BitmapImage. You can then get the bitmap's pixels from WriteableBitmap.PixelBuffer and can copy the buffer to a byte[] with the CopyTo extension method.
    Thursday, March 13, 2014 2:00 PM
    Owner
  • Hi FireDance.

    1. May be this link helps you.
    2. BitmapImage to Byte Array with this link.

    Hope it helps.


    Christian Amado | Software Engineer | MCPD: Windows Phone Developer

    Thursday, March 13, 2014 2:22 PM
  • Note that Christian's link is for Windows Phone and won't work as is in a Windows Store app.
    Thursday, March 13, 2014 4:22 PM
    Owner
  • Note that Christian's link is for Windows Phone and won't work as is in a Windows Store app.
    You're right Rob :) 

    Christian Amado | Software Engineer | MCPD: Windows Phone Developer

    Thursday, March 13, 2014 4:31 PM
  • HI Rob,

    I use WriteableBitmap but I am not sure if I did it right as I still encountered problem.

    I need to pass the byte array to another function to write it to an image file.

    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);

         WriteableBitmap wb = new WriteableBitmap(400, 300);
         await wb.SetSourceAsync(ims);   

         // problem here:

         byte[] pixelBuffer = null;

         wb.PixelBuffer.CopyTo(pixelBuffer);

      // Pass pixelBuffer next:


    }

    Friday, March 14, 2014 1:20 AM
  • Why are you converting this to a WriteableBitmap? You already have the pixels so putting them in a WriteableBitmap and then extracting them doesn't achieve anything. You can just save your stream directly to a file. See Quickstart: Reading and writing files

    If your original is raw pixels rather than a bitmap file type then you can use a BitmapEncoder to convert it to a bmp, png, jpg, etc. I suspect you already have this in an encoded format though.

    --Rob

    Friday, March 14, 2014 3:56 AM
    Owner
  • 1) 1st Solution:

    Why I create a WriteableBitmap ?

    a) I want to save WriteableBitmap as Jpeg file inside a folder in AppData Localfolder
    b) It did not work. The problem started at

    WriteableBitmap wb = new WriteableBitmap(400, 300);

     // problem here:
     await wb.SetSourceAsync(ims);   <--- is ims an image bytes?

     byte[] pixelBuffer = null;
     wb.PixelBuffer.CopyTo(pixelBuffer);


    2) 2nd Solution base on below code:

    use the base64 image string which created base on jpeg file.

    a) After converting this base64, is ims an image bytes?

    b) Save this as image file in a folder inside the AppData Local folder.


    The problem:

    File created BUT no image. What seems to be the problem?


    public async Task Base64StringToBitmapTask(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);  <---------is this an image bytes after converting?


     var byt = new byte[ims.Size];
     var reader = new DataReader(ims.GetInputStreamAt(0));
     await reader.LoadAsync((uint)ims.Size);
     reader.ReadBytes(byt);

      byte[] pixelBuffer = byt.ToArray();

      await InsertImageInFolder(pixelBuffer, Filenm);

    }

    private async Task InsertImageInFolder(byte[] buffer, string filename)
    {

     IBuffer Ibr = buffer.AsBuffer();
              
     StorageFolder folder = ApplicationData.Current.LocalFolder;

     //-- create or get a folder
     var subfolder = await CreateFolder(g_storagefolder, "ProductImages");
               
     var myImageFilename = await subfolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

     StorageFile  sampleFile = await subfolder.GetFileAsync(filename);

     await FileIO.WriteBufferAsync(sampleFile, Ibr);

     MessageDialog msg = new MessageDialog("Ok", "Insert Image");
     await msg.ShowAsync();
           
    }

    Friday, March 14, 2014 5:40 AM