c# - Getting the bytes[] / stream of an Image or ImageSource in Xamarin.Forms? -
i've been digging xamarin.forms.labs learn more how images accessed , manipulated. saving image android/ios photo gallery works fine, retrieving image either, @ least user interaction.
the problem i'd able save files internally program. saving file doesn't seem problem - i've written interface/dependencyservice solution that.
what can't seem access bytes[] or stream of image data xamarin.forms image or imagesource. reading stream imagesource relatively straightforward static method, how these bytes out in order save file within program itself?
to frame this: i'm working on app right user takes/selects pictures include within form, , form posted website. being able save pictures, or access data in order transfer it, pretty key.
one hacky way write custom renderer image. renderer byte[]/stream
native control. here's rough implementation without error handling started:
public class myimage : image { public func<byte[]> getbytes { get; set; } }
here's ios renderer:
public class myimagerenderer : imagerenderer { protected override void onelementchanged(elementchangedeventargs<image> e) { base.onelementchanged(e); var newimage = e.newelement myimage; if (newimage != null) { newimage.getbytes = () => { return this.control.image.aspng().toarray(); }; } var oldimage = e.oldelement myimage; if (oldimage != null) { oldimage.getbytes = null; } } }
the android 1 little more involved:
public class myimagerenderer : imagerenderer { protected override void onelementchanged(elementchangedeventargs<image> e) { base.onelementchanged(e); var newimage = e.newelement myimage; if (newimage != null) { newimage.getbytes = () => { var drawable = this.control.drawable; var bitmap = bitmap.createbitmap(drawable.intrinsicwidth, drawable.intrinsicheight, bitmap.config.argb8888); drawable.draw(new canvas(bitmap)); using (var ms = new memorystream()) { bitmap.compress(bitmap.compressformat.png, 100, ms); return ms.toarray(); } }; } var oldimage = e.oldelement myimage; if (oldimage != null) { oldimage.getbytes = null; } } }
and finally, usage kind of obvious:
var bytes = myimage.getbytes?.invoke();
extending idea support streams should pretty straightforward. obvious caveat forms control (myimage
) retains reference renderer via getbytes()
, seems unavoidable.
Comments
Post a Comment