list数组合并问题

bj-alex 2013-05-24 01:32:52
                int SendedCount = 0;

long totalLength = 0;
List<byte[]> list = new List<byte[]>();

while (true)
{
byte[] data = TransferFiles.ReceiveVarData(client);
totalLength += data.Length;
if (data.Length == 0)
{
break;
}
else
{
SendedCount++;
list.Add(data);

}

}

if (totalLength>0)
{

byte[] imagebytes = new byte[totalLength];
imagebytes=list.ToArry();
}

最后一句,这么写行吗?
...全文
189 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
bj-alex 2013-05-24
  • 打赏
  • 举报
回复
引用 22 楼 hi_web 的回复:
[quote=引用 20 楼 Chinajiyong 的回复:] 不行的 这是list List<byte[]> list = new List<byte[]>(); 改成这样 List<byte> list = new List<byte>(); list.ToArray()
在此基础上将接收的方法list.Add(data); 修改为list.AddRange(data)[/quote] 问题解决。感谢各位了,可惜分太少了。
hi_web 2013-05-24
  • 打赏
  • 举报
回复
引用 20 楼 Chinajiyong 的回复:
不行的 这是list List<byte[]> list = new List<byte[]>(); 改成这样 List<byte> list = new List<byte>(); list.ToArray()
在此基础上将接收的方法list.Add(data); 修改为list.AddRange(data)
bj-alex 2013-05-24
  • 打赏
  • 举报
回复
引用 20 楼 Chinajiyong 的回复:
不行的 这是list List<byte[]> list = new List<byte[]>(); 改成这样 List<byte> list = new List<byte>(); list.ToArray()
但是,这里接收的是数组啊,byte[] data = TransferFiles.ReceiveVarData(client);
EnForGrass 2013-05-24
  • 打赏
  • 举报
回复
不行的 这是list List<byte[]> list = new List<byte[]>(); 改成这样 List<byte> list = new List<byte>(); list.ToArray()
hi_web 2013-05-24
  • 打赏
  • 举报
回复
list里面放的是数组,比如list[0]中存放的是一个50字节的数组,list[1]中也是50个字节....,以此类推,当然你把list用ToArray之后不会自动拼接成一个byte[]了,应该是byte[][]才对。
bidisty 2013-05-24
  • 打赏
  • 举报
回复
试试存到数据库前的数据能不能打开?
bj-alex 2013-05-24
  • 打赏
  • 举报
回复
            SqlCommand com = new SqlCommand("select top 1 ImageList from Picture",con);
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
                //imagebytes = (byte[])dr.GetValue(1);
                if (dr.GetValue(0) != DBNull.Value)
                {
                    imagebytes = (byte[])dr.GetValue(0);
                    MemoryStream ms = new MemoryStream(imagebytes);
                    Bitmap bmpt = new Bitmap(ms);
                    pictureBox1.Image = bmpt;
                }
但是我用上面的方法,无法打开图片。
bj-alex 2013-05-24
  • 打赏
  • 举报
回复
我没用list.ToArray();用的是版主的方法,将接收的数组合并到imgagebytes,然后存入数据库。
bj-alex 2013-05-24
  • 打赏
  • 举报
回复
引用 14 楼 bidisty 的回复:
byte[][] imagebytes = imagebytes=list.ToArry(); 你的结果是这样的,你认为这个imagebytes 怎么放到数据库里, 如果我没猜错的话,你要有个序列化的方法来存放到数据库里
啊?我直接就com.Parameters.Add("ImageList", SqlDbType.Image).Value = imagebytes; 插入数据库了啊,而且数据库里显示有<二进制数据>。
bidisty 2013-05-24
  • 打赏
  • 举报
回复
byte[][] imagebytes = imagebytes=list.ToArry(); 你的结果是这样的,你认为这个imagebytes 怎么放到数据库里, 如果我没猜错的话,你要有个序列化的方法来存放到数据库里
bj-alex 2013-05-24
  • 打赏
  • 举报
回复
是不是跟传输格式,存储格式,下载格式有关系?
bidisty 2013-05-24
  • 打赏
  • 举报
回复
放不了多维数组吧。你说呢
bj-alex 2013-05-24
  • 打赏
  • 举报
回复
数据库里为Imgae格式
bidisty 2013-05-24
  • 打赏
  • 举报
回复
imagebytes为byte[]元素的数组,你数据库用什么格式放这个数组?
bidisty 2013-05-24
  • 打赏
  • 举报
回复
public T[] ToArray() 以你的来看 返回为byte[][]
bj-alex 2013-05-24
  • 打赏
  • 举报
回复
改成byte[]imagebytes=list.ToArray();后,就报错:无法将类型“byte[][]”隐式转换为“byte” 。还有,这里,只是将数据存放到了imagebytes数组中,因为我传的是图片。是不是还需要转换成二进制,才能存入数据库?因为发现将imagebytes数组存入数据库后,无法打开了
#blackheart 2013-05-24
  • 打赏
  • 举报
回复
引用 6 楼 qwangq 的回复:
List<byte> list = new List<byte>(); list.AddRange(data); if(list.Count>0) { byte[] imagebytes = list.ToArray(); } 用这种方法,为什么不行呢?老报错。
不会啊。另外list靠动态扩展,性能不是很好
bj-alex 2013-05-24
  • 打赏
  • 举报
回复
List<byte> list = new List<byte>(); list.AddRange(data); if(list.Count>0) { byte[] imagebytes = list.ToArray(); } 用这种方法,为什么不行呢?老报错。
#blackheart 2013-05-24
  • 打赏
  • 举报
回复

if (totalLength > 0)
            {

                byte[] imagebytes = new byte[totalLength];
                Int32 startIndex = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    byte[] temp = list[i];
                    temp.CopyTo(imagebytes,startIndex);
                    startIndex += temp.Length;
                }
            }
bj-alex 2013-05-24
  • 打赏
  • 举报
回复
循环接收数据包,然后再合并,看到数据过来了,怎么就存不到imagebytes数组中呢?
加载更多回复(3)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧