Loading

Converting a bitmap to a byte array

using a memory stream

This just asks the .Save() method to save the bytes to a memory stream, then you can get the written bytes to the stream:

 

代码
// Bitmap bytes have to be created via a direct memory copy of the bitmap
        private byte[] BmpToBytes_MemStream(Bitmap bmp)
        {
            MemoryStream ms 
= new MemoryStream();
            
// Save to memory using the Jpeg format
            bmp.Save(ms, ImageFormat.Jpeg);

            
// read to end
            byte[] bmpBytes = ms.GetBuffer();
            bmp.Dispose();
            ms.Close();

            
return bmpBytes;
        }

 

converting that back to a bitmap is easy.

 

代码
//Bitmap bytes have to be created using Image.Save()
private Image BytesToImg (byte[] bmpBytes)
{
    MemoryStream ms 
= new MemoryStream(bmpBytes);
    Image img 
= Image.FromStream(ms);
    
// Do NOT close the stream!
    
    
return img;
}

 

from:http://www.vbforums.com/showthread.php?t=358917

 

posted @ 2010-02-01 13:15  .net's  阅读(552)  评论(0)    收藏  举报