c# - System.Drawing.Bitmap contains more data than expected -
I have a way to copy data from a system. Drawing Bitmap that looks like this:
var readLock = image.LockBits (new rectangle (0, image, image, image. Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); Byte [] data = new byte [3 * image. With * image height]; If (data.long! = ReadLock.Stride * readLock.Height) throw a new invalid operation exception ("wrong number of bytes"); Martial Copy (readLock.Scan0, data, 0, data. Lang); Image.UnlockBits (readLock); It is very easy, and it works for most of my images, although it affects the exception of a very small image (14x14). In the unsuccessful case, Stride is 44, 42 (14 * 3) is not expected.
The pixel format format is 24 bpRGB, so the image should have three bytes for each pixel. Where are these extra bites coming from, and how can I deal with them while processing image data?
To keep an eye on someone, I'm generating normal data from the altitude, so I should be able to get every pixel and its neighbors correctly).
Align every pixel line of bitmap , this is the reason that Long is always width * bytes-per-pixel . You should ignore any additional bytes. This means that if you are working with the byte arrays with belt data, then you will not always be able to copy all the image data into a single martial.copy () call. Each line in pixels starts with readLock.Scan0 + y * readLock.Stride and contains readLock.Width * bytes-per-pixel meaningful bytes. Solution: const int BYTES_PER_PIXEL = 3; Var data = new byte [readLock.Width * readLock.Hyight * BYTES_PER_PIXEL]; If (readLock.Stride == readLock.Width * BYTES_PER_PIXEL) {Marshall.Copy (readLock.Scan0, data, 0, data. Length); } Else {for (int y = 0; y & lt; readLock.Height; ++ y) {IntPtr startOfLine = (IntPtr) ((long) readLock.Scan0 + (readLock.Stride * y)); Int dataoffet = y * readLock.Width * BYTES_PER_PIXEL; Marshall.Copy (StartOffline, Data, Data Offset, Reid Lock Wideth * BYTES_PER_PIXEL); }}
Comments
Post a Comment