Reputation: 1451
I am writing a program in Monodroid to capture a signature on screen, then save it to a jpg file. I can capture the signature fine, the problem comes when I try to save it to file. When the user wants to save the image, the below code runs:
void buttonSave_Click(object sender, EventArgs e)
{
try
{
if (!m_locked)
{
MemoryStream stream = new MemoryStream();
m_bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
byte[] byteArray = stream.GetBuffer();
//string toSave = Convert.ToBase64String(byteArray);
//save it to file (test);
string path = "/mnt/sdcard/TestSig/";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string file = path + "signature.jpg";
FileOutputStream fo = new FileOutputStream(file);
fo.Write(byteArray);
}
}
catch (Exception ex)
{
//display message
}
}
The ImageView that the signature is drawn onto is set up thus (in the activity's OnCreate method):
m_imageView = (ImageView)FindViewById(Resource.Id.imageView);
m_imageView.SetBackgroundColor(Android.Graphics.Color.White);
Display d = WindowManager.DefaultDisplay;
m_dw = d.Width;
m_dh = d.Height;
m_bitmap = Bitmap.CreateBitmap((int)m_dw, (int)(m_dh * 0.5), Bitmap.Config.Argb8888);
m_canvas = new Canvas(m_bitmap);
m_paint = new Paint();
m_paint.Color = Color.Black;
m_imageView.SetImageBitmap(m_bitmap);
m_imageView.SetOnTouchListener(this);
The problem is when I open the file in an image editor, all the dimensions are OK but it is completely black. It should look something like this:
By the way this seems to work OK when the png format is used. I am saving the image on an Android device but viewing it on a Windows PC.
Thanks.
Upvotes: 1
Views: 1830
Reputation: 1511
In your setup of m_imageView, enable the drawing cache (and set it's background colour):
m_imageView.DrawingCacheEnabled = true;
m_imageView.DrawingCacheBackgroundColor = Color.White;
Then fill the MemoryStream with the contents of the cache:
m_imageView.GetDrawingCache(false).Compress(Bitmap.CompressFormatJpeg,100, stream);
The call to .GetDrawingCache() will return a Bitmap of what you see inside m_imageView.
Upvotes: 1