We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert images to stream and byte array?

Platform: Xamarin.Forms |
Control: SfImageEditor

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 SfImageEditor 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

 

 

 

 

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile