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
Error: "Cannot Modify the Return Value" C#
Using Linq to Group a List of Objects into a New Grouped List of List of Objects
Automatically Create an Enum Based on Values in a Database Lookup Table
How to Access a Variable from Another Script in Another Gameobject Through Getcomponent
Javascriptserializer.Deserialize - How to Change Field Names
Deep Null Checking, Is There a Better Way
Select Right Generic Method with Reflection
Mono Shared Library Under Linux Location
Routing with Multiple Parameters Using ASP.NET MVC
How to Determine the Owner of a Process in C#
Intersection of Multiple Lists with Ienumerable.Intersect()
How to Change the Timeout on a .Net Webclient Object
Difference Between 'Throw' and 'Throw New Exception()'