首页 > 解决方案 > java - 如何从android中的字节数组中创建位图图像?

问题描述

我有 ac# tcp 服务器,我将图像字节数组发送到我的 android 客户端。但我没有成功将数组转换为位图。

This is my c# code:






    new Thread(() =>{
                        //İmage converter    
                        ImageConverter imConv = new ImageConverter();
//memory streaö                        
MemoryStream ms = new MemoryStream();
//bitmap               
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format16bppRgb555);
                        Graphics g = Graphics.FromImage(bitmap);
                        g.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
                        Cursors.Default.Draw(g, new Rectangle(new Point(Cursor.Position.X, Cursor.Position.Y), new Size(Cursors.Default.Size.Width, Cursors.Default.Size.Height)));
                        bitmap.Save(ms, ImageFormat.Png);
                        Image img = Image.FromStream(ms);
                        byte[] imgArry = (byte[])imConv.ConvertTo(img,typeof(byte[]));
                        tcpManager.SendTo(socket,imgArry);
                        bitmap.Dispose();
                        ms.Close();
                        ms.Dispose();
                        Thread.Sleep(41);

                                }).Start();

这是我的 android 客户端代码,我成功获得了字节数组,但函数返回了一个空位图

@Override
 public void OnReceived(byte[] byteData) {
 try{
      final Bitmap bitmap = BitmapFactory.decodeByteArray(byteData, 0, byteData.length);
      runOnUiThread(new Runnable() {
                 @Override
                 public void run() {
                                    screenView.setImageBitmap(bitmap);
                               }
                           });

                       }catch (Exception ex){
                       }
                   }

标签: c#androidtcp

解决方案


推荐阅读