C # Bitmap types and byte[] types convert each other

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.