Reputation: 13
I have image with dimensions 98 x 102. Size on disc 11 270 bytes. I need to get byte array from this image without service information. I tried to do next:
Bitmap bmp = new Bitmap(path);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Bmp);
byte[] result = ms.ToArray();
By this way i get array with image header, because length of get array is 11 270.
Is correctly that header size = 11270 - (98 * 102)? Because this result is 1274.
I'm trying to do this in console app.
If it is not correct, how i can do this?
Upvotes: 1
Views: 2596
Reputation: 5342
I'm assuming you need to get the RGB data from this bitmap. You can do this by locking/unlocking the data in the bitmap to obtain access to the raw data. Example code from MSDN https://msdn.microsoft.com/en-us/library/5ey6h79d(v=vs.110).aspx:
// Create bitmap.
Bitmap bmp = new Bitmap("c:\\photo.jpg");
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
// Set every third value to 255. A 24bpp bitmap will look red.
for (int counter = 2; counter < rgbValues.Length; counter += 3)
rgbValues[counter] = 255;
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
Upvotes: 2