我用VB.net写的很好用的代码:
Dim data() As Byte
Dim myfilestream As New System.IO.FileStream(Application.StartupPath + "\HTemp.jpg", IO.FileMode.Open)
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
改成C#的代码:
System.IO.FileStream myfilestream=new System.IO.FileStream(Application.StartupPath + @"\usTemp.jpg",System.IO.FileMode.Open);
byte[] data=new byte[myfilestream.Length-1];
myfilestream.Read(data, 0,myfilestream.Length);//myfilestream.Length编译不过说(无法从“long”转换为“int”
)
myfilestream.Close(); 于是我就改!
System.IO.FileStream myfilestream=new System.IO.FileStream(Application.StartupPath + @"\usTemp.jpg",System.IO.FileMode.Open);
byte[] data=new byte[myfilestream.Length-1];
myfilestream.Read(data, 0,(int)myfilestream.Length);
myfilestream.Close();
编译通过了。但是。运行时说:偏移和长度已超出数组界限,或者计数大于从索引到源集合末尾的元素数。
的哪位知道怎么回事?
Dim data() As Byte
Dim myfilestream As New System.IO.FileStream(Application.StartupPath + "\HTemp.jpg", IO.FileMode.Open)
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
改成C#的代码:
System.IO.FileStream myfilestream=new System.IO.FileStream(Application.StartupPath + @"\usTemp.jpg",System.IO.FileMode.Open);
byte[] data=new byte[myfilestream.Length-1];
myfilestream.Read(data, 0,myfilestream.Length);//myfilestream.Length编译不过说(无法从“long”转换为“int”
)
myfilestream.Close(); 于是我就改!
System.IO.FileStream myfilestream=new System.IO.FileStream(Application.StartupPath + @"\usTemp.jpg",System.IO.FileMode.Open);
byte[] data=new byte[myfilestream.Length-1];
myfilestream.Read(data, 0,(int)myfilestream.Length);
myfilestream.Close();
编译通过了。但是。运行时说:偏移和长度已超出数组界限,或者计数大于从索引到源集合末尾的元素数。
的哪位知道怎么回事?
)
这样就可以了
myfilestream.Read(data, 0, (int)myfilestream.Length);
改为
myfilestream.Read(data, 0, data.Length-1);
即可!
不用减1