Say I have a bitmap image, is it possible to iterate through all the individual bytes in the image? If yes, how?
相关问题
- Views base64 encoded blob in HTML with PHP
- How to get the background from multiple images by
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
If you need to access the pixel information, the super-slow but super-easy way is to call the GetPixel and SetPixel methods on your Bitmap object.
The super-fast and not-that-hard way is to call the Bitmap's LockBits method and use the BitmapData object returned from it to read and write the Bitmap's byte data directly. You can do this latter part with the Marshal class as in Ilya's example, or you can skip the Marshal overhead like this:
I found this: http://channel9.msdn.com/forums/TechOff/108813-Bitmap-to-byte-array/
Saying that you could use a Memorystream and the .Save method it'd look like this:
Use LockBits member on Bitmap class to obtain BitmapData, then use Scan0 and Marshal.ReadByte to readbytes. Here is small example (it is not about correct brightness adjustment, though):
Another solution is to use LockBits and Marshal.Copy to convert your bitmap into an array. I needed this solution because I had two images that differed only in their color depth and the other proffered solutions don't handle that well (or are too slow).