inverted image in C# -
this piece of code gathered here.
private unsafe byte[] bmptobytes_unsafe(bitmap bmp) { bitmapdata bdata = bmp.lockbits(new rectangle(0,0,1000,1000), imagelockmode.readonly, pixelformat.format24bpprgb); // number of bytes in bitmap int bytecount = bdata.stride * bmp.height; byte[] bmpbytes = new byte[bytecount]; // copy locked bytes memory marshal.copy(bdata.scan0, bmpbytes, 0, bytecount); marshal. // don't forget unlock bitmap!! bmp.unlockbits(bdata); return bmpbytes; }
i have function gets byte above mentioned function , displays without further processing. inverted image @ output. can explain?
when "inverted", suppose mean upside down?
you can't rely on "l33t hax0r skillz" of person posting code. lacks vital information how bitmaps handled in memory.
when read data bitmap can't read in 1 chunk. data stored in lines, , lines may stored either top line first or bottom line first. also, there may padding between lines each line on word boundary.
the scan0
property pointer start of first line, , stride
property offset start of next line. width
property can used determine how data there in each line.
so, have copy data 1 line @ time:
int linesize = bdata.width * 3; int bytecount = linesize * bdata.height; byte[] bmpbytes = new byte[bytecount]; intptr scan = bdata.scan0; (int = 0; < bdata.height; i++) { marshal.copy(scan, bmpbytes[i * linesize], 0, linesize); scan += bdata.stride; }
Comments
Post a Comment