Bitmap = byte[]
Bitmap B = new ""); MemoryStream ms = new MemoryStream (); B.save (ms,system.drawing.imaging.imageformat.bmp); byte [] bytes= Ms. GetBuffer (); // byte[] bytes= Ms. ToArray (); These two sentences can be, as for the difference, there are explanations below
byte[] = Bitmap
byte [] bytelist=bytes; MemoryStream ms1 = new MemoryStream (bytelist); Bitmap BM = (Bitmap) Image.fromstream (MS1);
1, because if the byte is converted without BMP, the data will be lost when converting to byte;
2. MemoryStream's getbuffer is not to get the content stored by this stream, but rather to return the underlying byte array of the stream, possibly including some unused bytes at the time of the expansion.
If you use MemoryStream, you can not say the effect is similar. Because I used Java to re-develop the MemoryStream class, so and should pay attention to the problem is basically clear, wherein GetBuffer is, there are many people think that this method is to write MemoryStream bytes, in fact, this is not the case. Because MemoryStream uses byte[] to store data, there is an extended process when writing data, meaning that the length of the stream is not the length of its byte[] field. And GetBuffer returns is byte[] This field. Here is a partial implementation of GetBuffer, perhaps read this will be more clear: public byte[] GetBuffer () {
return this._buffer;
}
Here's how ToArray is implemented:
Public byte[] ToArray () {
Byte[] bs = new Byte[this._length-this._origin];
for (int i = this._origin, j = 0; i < this._length; i++)
Bs[j++] = This._buffer[i];
return BS;
}
Source: http://blog.csdn.net/tigertianx/article/details/7098490
C # Bitmap types and byte[] types convert each other