c# - 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){
}
}
解决方案
推荐阅读
- javascript - Angular 6:添加类似“https://www.npmjs.com/package/url-regex”的java脚本库
- javascript - 反应原生如何使用道具自定义样式
- java - 从具有多个引用光标的 Hibernate 调用存储过程
- c# - .NET Core - 将带有 Request-Id 的传入请求与出站 HTTP 请求挂钩
- r - 隐藏第 X 行之后的所有行
- android - Android Animated Vector Drawable 无法在 xml 中循环
- node.js - 使用自己的上下文调用另一个文件中的函数
- c# - 无角色登录
- lisp - Common Lisp - CCL,为什么在将全局函数传递给局部函数时会出现警告?
- angular - Ionic3 Angularfire 监听 Firestore 的变化