1
我在MonoDroid的编写一个程序来捕捉在屏幕上签名,然后将其保存为JPG文件。我可以捕获签名,当我尝试将其保存到文件时,问题就出现了。当用户想要保存图像,下面的代码运行:MonoDroid的保存绘制位图到JPG
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
}
}
的ImageView的该签名被绘制到设置从而(在活动的OnCreate法):
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);
问题是当我在图像编辑器中打开文件时,所有尺寸都可以,但它完全是黑色的。它应该是这个样子:
通过这似乎当使用PNG格式的工作确定的方式。我将图像保存在Android设备上,但在Windows PC上查看。
谢谢。
谢谢汤姆,它工作得很好! – 2012-08-22 09:23:15