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

一般通过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);
    }
}

保存的文件直接用操作系统的图像软件打开呢?先界定范围,到底是图片的读取问题还是图片写入的问题,如果是写入的问题,那你的代码不全,这个写入没啥毛病,多半是TCP传输数据的问题,另外你的Bytes2File不要吞掉异常,不然不容易发现问题

根据你的问题描述 尝试TCP协议传图,但是出现了有些图写到本地不完全的问题, 就说明可能是TCP传输的代码问题,应该把这部分代码放出来

系统默认写文件是先写入分页缓存后同步到硬盘,所以某些异常状态文件会不完整
所以一个简单的防止意外的手段段是,利用系统文件copy,rename等进行安全同步,保证文件完整

手段分成2步:
1.写入临时文件 xxxx.temp 或者写入到 系统temp文件
2.然后copy到你实际的目标文件,同时删除temp临时文件

这样做当出现意外时,你实际目标里不会出现文件,而不是存在一个文件名一样字节不一样的错误文件

另我们没看到这个byte []到底是怎么从tcp里来的,所以我们无法保证这个byte[]本身就正确。

看到题主说采用的是byte[]传的,那可能需要先把byte[]进行base64的编码,直接tcp估计不太行得通。