[英]How to convert Video to byte Array in C#?
I am using c# .net compact framework 3.5 and I want to convert a video file to byte array so that I may upload it on the server. 我正在使用c#.net紧凑框架3.5,我想将视频文件转换为字节数组,以便我可以将其上传到服务器上。
In the similar manner I am doing the image uploading which is getting the success result. 我以类似的方式进行图像上传,获得成功结果。
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();
}
How to do this for the video files? 如何为视频文件执行此操作?
You should copy the stream one block at a time instead of reading the entire file into an array. 您应该一次一个地复制流,而不是将整个文件读入数组。 Otherwise, you'll use a potentially very large amount of memory as video files can grow quite big.
否则,您将使用可能非常大量的内存,因为视频文件可能会变得非常大。
For example: 例如:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.