Marcelo C Fernandes Marcelo C Fernandes - 3 years ago 136
JSON Question

How use the Byte Array of a image?

So, i am getting the byte array of a LongRaw image from Oracle...
I am using a webapi to this. After get the array, how i use it on the Client-side ?
Do Its better i convert to base64string and pass this value converting just at the client side ?

cmd.InitialLONGFetchSize = -1;
var reader = cmd.ExecuteReader();

if (reader.Read())
// Fetch the LONG RAW
OracleBinary imgBinary = reader.GetOracleBinary(0);
// Get the bytes from the binary obj
byte[] imgBytes = imgBinary.IsNull ? null : imgBinary.Value;

//var imgString = Uri.EscapeDataString(Convert.ToBase64String(imgBytes));

lretorno.Load(reader, LoadOption.OverwriteChanges, "BUSCAFOTO");



var teste = lretorno.Tables[0].AsEnumerable().Select(row => new FotoEnvolvido
FOTO = (byte[])(row["FOTO"]),
//FOTO = Convert.ToString(row["FOTO"]),

return teste;

Answer Source

You can write a Web API Controller that returns the binary data of an image. Base64 strings impose a overhead of the amount of bytes that have to be transmitted. Please avoid this.

A sample controller can look like this example:

public class WebApiController : ApiController
    public async Task<HttpResponseMessage> Get(string id)
        var bytes = await GetBytesFromDataLayerAsync(id);

        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new MemoryStream(bytes);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType =
            new MediaTypeHeaderValue("image/jpeg");
        return result;

    private async Task<byte[]> GetBytesFromDataLayerAsync(string id)
        // put your Oracle logic here
        return ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download