How to Convert Image to Byte Array

How to convert image to byte array

Sample code to change an image into a byte array

public byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
using (var ms = new MemoryStream())
{
imageIn.Save(ms,imageIn.RawFormat);
return ms.ToArray();
}
}

C# Image to Byte Array and Byte Array to Image Converter Class

Fastest way to convert Image to Byte array


So is there any other method to achieve this goal?

No. In order to convert an image to a byte array you have to specify an image format - just as you have to specify an encoding when you convert text to a byte array.

If you're worried about compression artefacts, pick a lossless format. If you're worried about CPU resources, pick a format which doesn't bother compressing - just raw ARGB pixels, for example. But of course that will lead to a larger byte array.

Note that if you pick a format which does include compression, there's no point in then compressing the byte array afterwards - it's almost certain to have no beneficial effect.

how to convert image to byte array in java?

BufferedImage consists of two main classes: Raster & ColorModel. Raster itself consists of two classes, DataBufferByte for image content while the other for pixel color.

if you want the data from DataBufferByte, use:

public byte[] extractBytes (String ImageName) throws IOException {
// open image
File imgPath = new File(ImageName);
BufferedImage bufferedImage = ImageIO.read(imgPath);

// get DataBufferBytes from Raster
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();

return ( data.getData() );
}

now you can process these bytes by hiding text in lsb for example, or process it the way you want.

Convert an Image to byte array in Angular (typescript)

Try Using





function convertDataURIToBinary(dataURI) {
var base64Index = dataURI.indexOf(';base64,') + ';base64,'.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));

for(i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}

function upload() {
const file = document.querySelector('input[type=file]').files[0];

const preview = document.getElementById('preview');
const reader = new FileReader();
let byteArray;

reader.addEventListener("loadend", function () {
// convert image file to base64 string
console.log('base64', reader.result);
preview.src = reader.result;
byteArray = convertDataURIToBinary(reader.result);
console.log('byte array', byteArray);
}, false);

if (file) {
reader.readAsDataURL(file);
}
}
<input type="file" id="file" accept="image/*" width="300" height="300" />

<submit onClick="upload()">Upload</submit>

<img id="preview"></img>

Convert PIL Image to byte array?

Thanks everyone for your help.

Finally got it resolved!!

import io

img = Image.open(fh, mode='r')
roi_img = img.crop(box)

img_byte_arr = io.BytesIO()
roi_img.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()

With this i don't have to save the cropped image in my hard disc and I'm able to retrieve the byte array from a PIL cropped image.



Related Topics



Leave a reply



Submit