Byte Array to Image Conversion

Converting byte array to image in C#

I found a solution that ended up working. It seems that when the initial ImageToByteArray was being done, it was only doing the "1st page" and not all 8. So I used the following code to convert the whole tiff image:

var tiffArray = File.ReadAllBytes(file); //The `file` is the actual `.tiff` file

I then used the following to convert back to an image(The response is a byte[] returned from our API):

using (MemoryStream ms = new MemoryStream(response))
{
ms.Position = 0;
Image returnImage = Image.FromStream(ms);

var splitImages = ImageHelper.Split(returnImage);//This is to split the pages within the tiff
}

I read that for the above to work(and I tested it), anything you do to the byte[]you are converting back to an image, must be done within the using, as anything after the using means the image is disposed.

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

conversion of byte array to image

Hope this code will help you:

byte[] data = item1.getBytes();
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
imageview.setImageBitmap(bmp);

Convert from byte array to Image

You can try fromarray function

import numpy as np
image = Image.fromarray(np.array(ba).reshape(498,1))

How to convert byte array to image in php?

Use pack to convert data into binary string, es:

$data = implode('', array_map(function($e) {
return pack("C*", $e);
}, $MemberImage));

// header here
// ...

// body
echo $data;


Related Topics



Leave a reply



Submit