7

I have svg xml that i can convert to ImageSource or FileImageSource by using XamSVG library in the PCL project of my xamarin.forms.

I want to convert the ImageSource / FileImageSource to byte array (to get the bitmap).

Is this possible ?

2
  • Just refer stackoverflow.com/questions/27532462/… Commented May 23, 2016 at 10:25
  • this link isn't what i need since i am not using the camera. all i have is image with imagesource that i want to get the bitmap (byte array) from it Commented May 23, 2016 at 13:02

3 Answers 3

6

ImageSource doesn't expose any mechanism to retrieve the original image source. Instead, you will need to manually keep a reference to the original source you use to create the image.

Sign up to request clarification or add additional context in comments.

2 Comments

I dont have original source - i have svg xml file that converts to imagefile, i need the byte array of the bitmap...
You will need to look at the XamSVG library to see if it produces an intermediary bitmap you can access, or modify it to produce one.
5

I've found the solution.

StreamImageSource streamImageSource  = (StreamImageSource) some image source...
System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
Task<Stream> task = streamImageSource.Stream(cancellationToken);
Stream stream = task.Result;

4 Comments

and what is "a"? (3rd line)
a is streamImageSource
I juest tried that and I get: "Unable to cast object of type 'Xamarin.Forms.FileImageSource' to type 'Xamarin.Forms.StreamImageSource'."
This is not a solution - it only works if the ImageSource is of type StreamImageSource. It is not away to create an output stream of any ImageSource
3

Another solution:

public static byte[] ReadFully(Stream input)
{
    using (MemoryStream ms = new MemoryStream())
    {
        input.CopyTo(ms);
        return ms.ToArray();
    }          
}

Stream and MemoryStream are System.IO classes.

Then use it like this:

byte[] TargetImageByte = ReadFully(_data.Source);

_data.source is MediaFile type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.