How to create bitmap from byte array?
Guys thank you for your help. I think all of this answers works. However i think my byte array contains raw bytes. That's why all of those solutions didnt work for my code.
However i found a solution. Maybe this solution helps other coders who have problem like mine.
static byte[] PadLines(byte[] bytes, int rows, int columns) {
int currentStride = columns; // 3
int newStride = columns; // 4
byte[] newBytes = new byte[newStride * rows];
for (int i = 0; i < rows; i++)
Buffer.BlockCopy(bytes, currentStride * i, newBytes, newStride * i, currentStride);
return newBytes;
}
int columns = imageWidth;
int rows = imageHeight;
int stride = columns;
byte[] newbytes = PadLines(imageData, rows, columns);
Bitmap im = new Bitmap(columns, rows, stride,
PixelFormat.Format8bppIndexed,
Marshal.UnsafeAddrOfPinnedArrayElement(newbytes, 0));
im.Save("C:\\Users\\musa\\Documents\\Hobby\\image21.bmp");
This solutions works for 8bit 256 bpp (Format8bppIndexed). If your image has another format you should change PixelFormat
.
And there is a problem with colors right now. As soon as i solved this one i will edit my answer for other users.
*PS = I am not sure about stride value but for 8bit it should be equal to columns.
And also this function Works for me.. This function copies 8 bit greyscale image into a 32bit layout.
public void SaveBitmap(string fileName, int width, int height, byte[] imageData)
{
byte[] data = new byte[width * height * 4];
int o = 0;
for (int i = 0; i < width * height; i++)
{
byte value = imageData[i];
data[o++] = value;
data[o++] = value;
data[o++] = value;
data[o++] = 0;
}
unsafe
{
fixed (byte* ptr = data)
{
using (Bitmap image = new Bitmap(width, height, width * 4,
PixelFormat.Format32bppRgb, new IntPtr(ptr)))
{
image.Save(Path.ChangeExtension(fileName, ".jpg"));
}
}
}
}
Convert byte[] to Bitmap in Core 2.1
You'll need to put the bytes
into a MemoryStream
public static Bitmap ByteArrayToImage(byte[] source)
{
using (var ms = new MemoryStream(source))
{
return new Bitmap(ms);
}
}
The code above will use the Bitmap(Stream stream)
constructor.
create Bitmap from byteArray in android
You need a mutable Bitmap
in order to create the Canvas
.
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap); // now it should work ok
Edit: As Noah Seidman said, you can do it without creating a copy.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options);
Canvas canvas = new Canvas(bmp); // now it should work ok
How to convert Byte array into bitmap image?
byte[] bitmapdata; // let this be your byte array
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
I can convert a byte array to Bitmap but the same process in reverse produces a null
To convert Bitmap to Byte Array use the following code
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
To convert Byte Array to bitmap use following code
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, bitmapdata.length);
Android byte array to Bitmap How to
I've been using it like below in one of my projects and so far it's been pretty solid. I'm not sure how picky it is as far as it not being compressed as a PNG though.
byte[] bytesImage;
Bitmap bmpOld; // Contains original Bitmap
Bitmap bmpNew;
ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
bmpOld.compress(Bitmap.CompressFormat.PNG, 100, baoStream);
bytesImage = baoStream.toByteArray();
bmpNew = BitmapFactory.decodeByteArray(bytesImage, 0, bytesImage.length);
edit: I've adapted the code from this post to use RGB, so the code below should work for you. I haven't had a chance to test it yet so it may need some adjusting.
Byte[] bytesImage = {0,1,2, 0,1,2, 0,1,2, 0,1,2};
int intByteCount = bytesImage.length;
int[] intColors = new int[intByteCount / 3];
int intWidth = 2;
int intHeight = 2;
final int intAlpha = 255;
if ((intByteCount / 3) != (intWidth * intHeight)) {
throw new ArrayStoreException();
}
for (int intIndex = 0; intIndex < intByteCount - 2; intIndex = intIndex + 3) {
intColors[intIndex / 3] = (intAlpha << 24) | (bytesImage[intIndex] << 16) | (bytesImage[intIndex + 1] << 8) | bytesImage[intIndex + 2];
}
Bitmap bmpImage = Bitmap.createBitmap(intColors, intWidth, intHeight, Bitmap.Config.ARGB_8888);
Create a bitmap from a stored byte array and save to disk throws GDI+ exception when source of byte array was a jpg
You're disposing the MemoryStream
before the image is written, try disposing the stream after the file has been saved. That worked for me :)
If you want to get the image format, try detecting it from the first few bytes of the stream:
Wikipedia: file-signature-list
Related Topics
Dot Character '.' in MVC Web API 2 for Request Such as API/People/Staff.45287
When to Dispose Cancellationtokensource
Invert "If" Statement to Reduce Nesting
When Is It Better to Use String.Format VS String Concatenation
In Mvvm Should the Viewmodel or Model Implement Inotifypropertychanged
How to Check for Permissions to Write to a Directory or File
Regarding Usage of Task.Start() , Task.Run() and Task.Factory.Startnew()
How to Round to the Nearest 0.5
How To: Best Way to Draw Table in Console App (C#)
Coercing Floating-Point to Be Deterministic in .Net
Using 'Async' in a Console Application in C#
C# Regex Split - Commas Outside Quotes
Allowing Untrusted Ssl Certificates with Httpclient
Differencebetween File.Readlines() and File.Readalllines()
Why Can't C# Infer Type from This Seemingly Simple, Obvious Case
Static VS Non-Static Class Members
Why Does This Async Action Hang When I Try and Access the Result Property of My Task