哩哩酱 2022-10-11 13:19 采纳率: 100%
浏览 177
已结题

TCP收到byte[]数据 转图片花图问题,tiff图片加载显示问号?

尝试TCP协议传图,但是出现了有些图写到本地不完全的问题,TIFF格式图片在unity内不显示,采用的是 texture2D.LoadImage(bytes);具体代码如下:


  public static void LoadTexture2DByByte(byte[] bytes, Action<Texture2D> textureLoaded)
        {
            Texture2D texture2D = new Texture2D(1, 1, TextureFormat.ARGB32, false);
            Bytes2File(bytes, path, "image.jpg");
            var newBytes = File2Bytes(path);
            texture2D.LoadImage(newBytes);
            textureLoaded?.Invoke(texture2D);
        }
   /// <summary>
        /// 将文件转换为byte数组
        /// </summary>
        /// <param name="path">文件地址</param>
        /// <returns>转换后的byte数组</returns>
        public static byte[] File2Bytes(string path)
        {
            DateTime startTime = DateTime.Now;

            if (!Directory.Exists(path))
            {
                return new byte[0];
            }

            FileStream fileStream = new FileStream(path + @"\image.jpg", FileMode.Open, FileAccess.Read);
            fileStream.Seek(0, SeekOrigin.Begin);
            //创建文件长度的缓冲区
            byte[] bytes = new byte[fileStream.Length];
            //读取文件
            fileStream.Read(bytes, 0, (int)fileStream.Length);
            //释放文件读取流
            fileStream.Close();
            fileStream.Dispose();
            fileStream = null;

            return bytes;
        }

        /// <summary>
        /// 将byte数组转换为文件并保存到指定地址
        /// </summary>
        /// <param name="buff">byte数组</param>
        /// <param name="savepath">保存地址</param>
        public static void Bytes2File(byte[] buff, string savepath, string fileName)
        {
            try
            {
                //如果不存在就创建Enclosure文件夹 
                if (Directory.Exists(savepath) == false)
                {
                    Directory.CreateDirectory(savepath);
                }

                if (System.IO.File.Exists(savepath + @"\" +  fileName))
                {
                    System.IO.File.Delete(savepath + @"\" + fileName);
                }

                FileStream fs = new FileStream(savepath + @"\" + fileName, FileMode.CreateNew);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(buff, 0, buff.Length);
                bw.Close();
                fs.Close();

            }
            catch (Exception)
            {

            }
        }

不显示的jpg图片再保存一次就能显示了,先不考虑这个问题了 ,可能是图片的问题。
关于图片显示不全,客户端说发给我的图数据,他自己写到本地是正常的,所以可能是传输过程处理问题。
希望能有热心人士解决,大图和各种格式的图片显示问题。

  • 写回答

5条回答 默认 最新

  • 来灵 2022-10-11 15:12
    关注

    一般通过TCP传输图片,需要将图片流先用base64编码成字符串,编码后再发送给接收端。
    接收端收到图片数据(base64编码格式)后,将base64字符串再转换为图像流,然后保存成图片文件。
    C# 转换函数如下,供参考

    public string ConvertImageToBase64(Image file)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            file.Save(memoryStream, file.RawFormat);
            byte[] imageBytes = memoryStream.ToArray();
            return Convert.ToBase64String(imageBytes);
        }
    }
    
    public Image ConvertBase64ToImage(string base64String)
    {
        byte[] imageBytes = Convert.FromBase64String(base64String);
        using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {
            ms.Write(imageBytes, 0, imageBytes.Length);
            return Image.FromStream(ms, true);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 10月21日
  • 已采纳回答 10月13日
  • 修改了问题 10月11日
  • 创建了问题 10月11日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分