Mono for Android (4)-- 图片转为二进制,二进制转回图片

最近纠结蓝牙打印的问题,想着图片先转为二进制发给打印机,找了好多资料,终于成功了,贴出来共享一下

先是图片转换为二进制的:

Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Icon);
int size = bitmap.Width * bitmap.Height * 4;
// 创建一个字节数组输出流,流的大小为size   
System.IO.MemoryStream m = new System.IO.MemoryStream(size);
// 设置位图的压缩格式,质量为100%,并放入字节数组输出流中  
bitmap.Compress(Bitmap.CompressFormat.Png, 100, m);
// 将字节数组输出流转化为字节数组byte[]  
byte[] dBytes = m.ToArray();

再是二进制转回为图片,并显示在页面上(也能帮助检验你上个步骤转换的对不对):

 Bitmap bit2 = BitmapFactory.DecodeByteArray(imagedata, 0, imagedata.Length);
 Drawable drawable = new BitmapDrawable(bit2);

 ImageView imageview = FindViewById<ImageView>(Resource.Id.imageView2);
 imageview.SetImageDrawable(drawable);

ps:ImageView2是我画在页面上面的一个ImageView控件,用来显示图片,大家都懂的哈

 

 

 

posted @ 2013-11-21 17:39  ElzaJiang  阅读(1209)  评论(0编辑  收藏  举报