最近纠结蓝牙打印的问题,想着图片先转为二进制发给打印机,找了好多资料,终于成功了,贴出来共享一下数组
先是图片转换为二进制的:spa
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();
再是二进制转回为图片,并显示在页面上(也能帮助检验你上个步骤转换的对不对):code
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控件,用来显示图片,你们都懂的哈orm