C#: image to byte array

    private byte[] ImageToByteArray(Image img) 
    { 
        // stream to save the image to byte array 
        Stream ms = new MemoryStream(); 
        img.Save(ms, img.RawFormat); 
 
        // read to end 
        byte[] imgBytes = new byte[ms.Length-1]; 
        ms.Position = 0; 
        ms.Read(imgBytes, 0, imgBytes.Length); 
        ms.Close(); 
 
        return imgBytes; 
    }

Tags:

Leave a Reply

You must be logged in to post a comment.