7

I am using Mono for Android, I would like to save a bitmap to a byte array  So I can save it to a database.

Searching in here I found the following piece of code:

ByteArrayOutputStream bos = new ByteArrayOutputStream();  
bitmap.compress(CompressFormat.PNG, 0, bos);  
byte[] bitmapdata = bos.toByteArray();  

But  the "ByteArrayOutputStream"  class is not found.

Can somebody tell me what namespace to import that contains this class or any other way to solve this problem. 

Thank you for your time.

Sorin Botezatu
  • 83
  • 1
  • 2
  • 4

2 Answers2

28

You need to use MemoryStream in MonoDroid instead. Try this:

using (var stream = new MemoryStream()) {
    bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
    byte[] bitmapData = stream.ToArray();
}
Justin
  • 17,670
  • 38
  • 132
  • 201
bertusaurus
  • 634
  • 1
  • 6
  • 12
  • 3
    Will this memory stream leak memory because it is never disposed of? I think you want to wrap this `MemoryStream` in a `Using` statement – JKennedy Mar 17 '16 at 12:54
-2

import this package import java.io.ByteArrayOutputStream;

Venkata Krishna
  • 1,543
  • 1
  • 11
  • 19