How can I convert an image to array of bytes using ImageSharp library?
Can ImageSharp library also suggest/provide RotateMode and FlipMode based on EXIF Orientation?
If you are looking to convert the raw pixels into a byte[]
you do the following.
var bytes = image.SavePixelData()
If you are looking to convert the encoded stream as a byte[]
(which I suspect is what you are looking for). You do this.
using (var ms = new MemoryStream())
{
image.Save(ms, imageFormat);
return ms.ToArray();
}
For those who look after 2020:
SixLabors seems to like change in naming and adding abstraction layers, so... Now to get a raw byte data you do the following steps.
MemoryGroup
of an image using GetPixelMemoryGroup()
method.GetPixelMemoryGroup()
returns a interface) and taking first element (if somebody tells me why they did that, i'll appreciate).System.Memory<TPixel>
get a Span
and then do stuff in old way.
(i prefer solution from @Majid comment)So the code looks something line this:
var _IMemoryGroup = image.GetPixelMemoryGroup();
var _MemoryGroup = _IMemoryGroup.ToArray()[0];
var PixelData = MemoryMarshal.AsBytes(_MemoryGroup.Span).ToArray();
ofc you don't have to split this into variables and you can do this in one line of code. I did it just for clarification purposes. This solution only viable as for 06 Sep 2020
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With