Articles in this section
Category / Section

How to convert images to stream and byte array?

1 min read

This article explains you on how to convert image to byte array, stream to byte array, bytearray to stream and load them as image.

 

Import the Xamarin.Forms ImageEditor namespace as shown below,

 

Xaml

 

xmlns:imageeditor="clr-namespace:Syncfusion.SfImageEditor.XForms;assembly=Syncfusion.SfImageEditor.XForms"

 

 

  1. How to get the image as stream?

 

Create a Xamarin application and initialize SfImageEditor control in it. Provide the Source for the image as in the below code snippet.

 

Xaml

 

  < imageeditor:SfImageEditor Source="{Binding Source}" >
  </imageEditor:SfImageEditor>

 

Use the inbuilt ImageSaving event of the SfImageEditor to get the stream of the image being saved. The stream can be obtained from the ImageSavingEventArgs

Xaml

 

  <imageeditor:SfImageEditor Source="{Binding Source}" ImageSaving="ImageSavingEvent"/>

 

Add the corresponding ImageSaving event implementations in the adjacent .xaml.cs file.

 

C#

 

private void ImageSavingEvent(object sender, ImageSavingEventArgs args)
{
    var stream = args.Stream;
}

 

The above code snippet obtains the current stream of the image being saved.

 

  1. How to convert the image stream to byte array?

 

To convert the image stream as bytearray, use the stream obtained from the ImageSavingEventArgs from the ImageSaving event as in the below code snippet:

 

 
private byte[] GetImageStreamAsBytes(Stream input)
{
    var buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}
 
private void ImageSavingEvent(object sender, ImageSavingEventArgs args)
{
    args.Cancel = true; // Stop the image from saving to location
 
    var byteArray = GetImageStreamAsBytes(args.Stream);
}

 

 

For a better understanding of the conversion of images to streams, streams to bytearray, image to bytearray, bytearray to stream and finally show them as an Image, please refer the below sample

 

Sample Link:

 

https://www.syncfusion.com/downloads/support/directtrac/general/ze/HOWTOC~11143924326.zip

 

 

 

 

 

Conclusion

I hope you enjoyed learning how to convert images to stream and byte arrays

You can refer to our Xamarin .Forms Image Editor feature tour page to know about its other groundbreaking feature representations documentation and how to quickly get started for configuration specifications. 

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied