我发现了这个:http://channel9.msdn.com/forums/TechOff/108813-Bitmap-to-byte-array/
假设您可以使用Memorystream和.Save方法,它看起来像这样:
System.Drawing.Bitmap bmp = GetTheBitmap();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, stream.Length);
Run Code Online (Sandbox Code Playgroud)
如果您需要访问像素信息,超缓慢但超级简单的方法是在Bitmap对象上调用GetPixel和SetPixel方法.
超快且不那么难的方法是调用Bitmap的LockBits方法并使用从其返回的BitmapData对象直接读取和写入Bitmap的字节数据.你可以像在伊利亚的例子中那样使用Marshal类来完成后一部分,或者你可以像这样跳过元帅的开销:
BitmapData data;
int x = 0; //or whatever
int y = 0;
unsafe
{
byte* row = (byte*)data.Scan0 + (y * data.Stride);
int columnOffset = x * 4;
byte B = row[columnOffset];
byte G = row[columnOffset + 1];
byte R = row[columnOffset + 2];
byte A = row[columnOffset + 3];
}
Run Code Online (Sandbox Code Playgroud)