I have to convert the pixels of a bitmap to short array. Therefore I want to:
- get the bytes
- convert the bytes to short
This is my source to get the bytes:
public byte[] BitmapToByte(Bitmap source)
{
using (var memoryStream = new MemoryStream())
{
source.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
return memoryStream.ToArray();
}
}
This is not returning the expected results. Is there another way to convert the data?
Please explain your problem properly. “I’m missing bytes” is not something that can be solved. What data do you expect, and what do you see?
Bitmap.Save()will return the data according to the specified format, which in all cases contains more than just the pixel data (headers describing width and height, color / palette data, and so on). If you just want an array of pixel data, you’d better look atBimap.LockBits():Now the
rgbValuesarray contains all pixels from the source bitmap, using three bytes per pixel. I don’t know why you want an array of shorts, but you must be able to figure that out from here.