none
[UWP]C# BitmapImage to byte array RRS feed

  • Question

  • Hi,

       I am using the below code to convert a byte array to a BitmapImage:

    private async Task<BitmapImage> ByteArrayToBitmapImage(byte[] byteArray)
            {
                var bitmapImage = new BitmapImage();
                var stream = new InMemoryRandomAccessStream();
                await stream.WriteAsync(byteArray.AsBuffer());
                stream.Seek(0);
                bitmapImage.SetSource(stream);
                return bitmapImage;

            } 

    How can I convert an (Image Control) image.Source back to a byte array?

    Thanks

    Paul.


    Paul Wainwright


    Monday, August 1, 2016 10:59 AM

Answers

  • Hi pr_wainwright,

    There is no way to extract the image data from an ImageSource. You will need to keep track of the original source of the information and recreate the image in the WriteableBitmap from the original source. If you know you are going to need to do this you may be better off just using a WriteableBitmap as your ImageSource to begin with

    Please refer to Rob’s reply in this thread about “Convert ImageSource to WriteableBitmap in Metro Windows 8” for details.

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by pr_wainwright Thursday, August 4, 2016 2:01 PM
    Thursday, August 4, 2016 11:10 AM
    Moderator

All replies

  • Hi pr_wainwright,

    Welcome to the Developing Universal Windows apps forum!

    Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools  

    I have added tag for you this time, and you would need to add tag by yourself next time.

    You could get the file stream by “BitmapImage.UriSource” property value. Then you could convert it to byte array.

    Please refer to my following code for details:

    BitmapImage source = img.Source as BitmapImage;
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(source.UriSource);
                using (var inputStream = await file.OpenSequentialReadAsync())
                {
                    var readStream = inputStream.AsStreamForRead();
    
                    var byteArray = new byte[readStream.Length];
                    await readStream.ReadAsync(byteArray, 0, byteArray.Length);
                    return byteArray;
                }

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 2, 2016 8:02 AM
    Moderator
  • Xavier,

              Thanks for the reply and links. I am using the below code to load the Image control so the source file is not saved as a StrorgeFile

    private async void BtnImageAdd_Click(object sender, RoutedEventArgs e)
            {
                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                openPicker.ViewMode = PickerViewMode.Thumbnail;
                openPicker.FileTypeFilter.Clear();
                openPicker.FileTypeFilter.Add(".jpg");
                StorageFile file = await openPicker.PickSingleFileAsync();
                if (file != null)
                {
                  
                    using (IRandomAccessStream fs = await file.OpenAsync(FileAccessMode.Read))
                    {
                        // Set the image source to the selected bitmap.
                        BitmapImage imageBoxAddBitmapImage = new BitmapImage();
     
                        imageBoxAddBitmapImage.SetSource(fs);
                        imageBoxAdd.Source = imageBoxAddBitmapImage;

                    }
                }
            }

    Is there any other way I can get the byte array?

    Thanks

    Paul.


    Paul Wainwright

    Tuesday, August 2, 2016 9:49 AM
  • Hi pr_wainwright,

    There is no way to extract the image data from an ImageSource. You will need to keep track of the original source of the information and recreate the image in the WriteableBitmap from the original source. If you know you are going to need to do this you may be better off just using a WriteableBitmap as your ImageSource to begin with

    Please refer to Rob’s reply in this thread about “Convert ImageSource to WriteableBitmap in Metro Windows 8” for details.

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by pr_wainwright Thursday, August 4, 2016 2:01 PM
    Thursday, August 4, 2016 11:10 AM
    Moderator