When trying to convert a picturebox to a byte array, I get a wrong array length
The PictureBox is 352 * 288 = 101376 pixels, converting to a byte array should give an array length of 101376 * 3 = 304128 bytes. I try to convert to an array using the following function:
private byte[] ImgToByteArray(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
I call the function like so:
Byte[] LogImg = ImgToByteArray(LogPictureBox.Image);
Int LogSize = LogImg.Length
The returned value of LogSize = 1070. I find this weird, it should be 304128.
I don't understand it by using breakpoints, I validated the LogPictureBox.Image size to be 352 * 288. So what am I doing wrong here?
I also tried a function with memory stream method method to copy image to array like below, but is also resulted in a wrong size
Public Function ImgToByteArray(ByVal img As Image) As Byte()
Using mStream As New MemoryStream()
img.Save(mStream, img.RawFormat)
Return mStream.ToArray()
End Using
End Function
// called this like
Byte[] LogImg=imgToByteArray((Bitmap)LogPictureBox.Image);
As people suggested color depth might be the issue, I coded LogPictureBox to be 24bits p pixel Which equals RGB, still isn't working. Also worth mentioning is that a correct array size (304128) can be retrieved by:
Bitmap logBmp = (Bitmap)LogPictureBox.Image;
int logBmpW = logBmp.Width;
int logBmpH = logBmp.Height;
var lockedLog = logBmp.LockBits(new Rectangle(0, 0, logBmpW, logBmpH), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
var logData = new int[lockedLog.Height * System.Math.Abs(lockedLog.Stride)];
// typical marshal code following bitmap to array etc still halted the code
I've been hoping that the first 2 functions would work, for the image manipulation I want to do. As I need to update the bitmap array. I've been trying other methods too, (just above here is part of that) but it didn't work either. Then I get into threading issues it seems. Maybe that's a problem with earlier two functions too.
I've got a webcam triggering a newframe event, (Aforge version 2.2.4) and its this event that's not only updating its own picturebox, but it also needs to set another logging picturebox, apparently threads cause issues here.
The accurate value of bytes/pixel can not compute like that.
It depends on color depth. A pixel could be represented by just a bit. For a 256 color image, a byte per pixel is needed. Additional depth requires more storage, high-color uses 32 bits/4 bytes per pixel.
I test several Image size in 352x288 and return different value with your code. You can use another image with same size to test what I said, please try.
Edit: Maybe you can try in different format by this code, then check the length, different filetype will also have different consequence:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}