Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET-Core byte array to Image

how do I convert a byte[] to an Image in .NET Core?

I found this:

using (var ms = new MemoryStream(byteArrayIn))
{
    return Image.FromStream(ms);
}

but it seems like Image doesnt exist in .NET-Core.

like image 297
Moritz Schmidt Avatar asked Jun 13 '17 10:06

Moritz Schmidt


2 Answers

No indeed it is not released yet. you can use the library ImageSharp.

like image 67
Lenny32 Avatar answered Nov 15 '22 10:11

Lenny32


To return an image from a byte array, you can either:

  1. return base64

    Byte[] profilePicture = await _db.Players
                                     .Where(p => p.Id == playerId)
                                     .Select(p => p.ProfilePicture)
                                     .FirstOrDefaultAsync();
    
    return Ok(Convert.ToBase64String(profilePicture));
    

    And then you can use any online tool that converts base64 to image to test it.

  2. or return FileContentResult File(byte[] fileContents, string contentType)

    return File(profilePicture, "image/png");
    

    You should be able to test this from Postman or anything similar as the picture will show up in the body of the response there.

like image 23
Ahmed Mansour Avatar answered Nov 15 '22 08:11

Ahmed Mansour