如何在C#中将视频转换为字节数组?
作者:互联网
我正在使用c#.net紧凑框架3.5,我想将视频文件转换为字节数组,以便我可以将其上传到服务器上.
我以类似的方式进行图像上传,获得成功结果.
HttpWebRequest request;
request.ContentType = "image/jpeg";
request.ContentLength = byteArray.Length;
request.Method = "PUT";
imageToByteArray(img).CopyTo(byteArray, 0);
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(byteArray, 0, (int)Fs.Length);
requestStream.Flush();
requestStream.Close();
}
public byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
如何为视频文件执行此操作?
解决方法:
您应该一次一个地复制流,而不是将整个文件读入数组.否则,您将使用可能非常大量的内存,因为视频文件可能会变得非常大.
例如:
HttpWebRequest request;
request.Method = "PUT";
using(Stream requestStream = request.GetRequestStream())
using(Stream video = File.OpenRead("Path")) {
byte[] buffer = new byte[4096];
while(true) {
int bytesRead = video.Read(buffer, 0, buffer.Length);
if (bytesRead == 0) break;
requestStream.Write(buffer, 0, bytesRead);
}
}
标签:c,net,file-upload,compact-framework,windows-mobile 来源: https://codeday.me/bug/20190717/1485121.html