Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a byte array as an image using JavaScript

Tags:

I am trying to display an image (byte array) using purely JavaScript.

How can I achieve this in ASP.NET?

like image 470
Cute Bear Avatar asked Feb 27 '12 11:02

Cute Bear


People also ask

How do I save a byte array as a picture?

Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter). Finally, Write the image to using the write() method of the ImageIo class.

How do I display an image in a byte array?

//ImageConverter Class convert Image object to Byte Array. byte[] bytes = (byte[])(new ImageConverter()). ConvertTo(img, typeof(byte[])); //Convert Byte Array to Image and display in PictureBox.

What is byte array image?

Images are binary data - this is easily represented as byte arrays. The image in the sample is stored in the database as a BLOB - not a string or location, that is, it is binary data.

What is byte array in Javascript?

The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. It is an array of bytes, often referred to in other languages as a "byte array".


1 Answers

If you have the byte array, you first convert it to Base64String and then you place it on an img tag like that (for a PNG image):

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

Similar Stack Overflow questions:

  • Display bytes as images on an .aspx page

  • 'data:image/jpg;base64' and jQuery image preview in Internet Explorer

  • Convert from binary data to an image control in ASP.NET

like image 53
Aristos Avatar answered Sep 21 '22 15:09

Aristos