【发布时间】:2015-05-17 00:44:56
【问题描述】:
我在使用 SharpAVI.dll 将图像转换为视频时遇到了一些问题。
通过使用 SharpAVI 网站上的文档,我设法使用随机生成的字节数组生成了一个视频文件:
所以我想我下一步要做的是拍摄一个图像,创建一个位图图像,将位图转换为字节数组,然后简单地将字节数组保存到视频文件的每一帧。当我运行该程序时,我没有收到任何错误或任何东西,并且会生成适当文件大小的视频文件,但是视频文件不可读且无法打开。我真的很难理解为什么这不起作用。任何帮助将不胜感激!
我的代码:
private void GenerateSingleImageVideo()
{
string imagePath = textBoxImagePath.Text;
Bitmap thisBitmap;
//generate bitmap from image file
using (Stream BitmapStream = System.IO.File.Open(imagePath, FileMode.Open))
{
Image img = Image.FromStream(BitmapStream);
thisBitmap = new Bitmap(img);
}
//convert the bitmap to a byte array
byte[] byteArray = BitmapToByteArray(thisBitmap);
//creates the writer of the file (to save the video)
var writer = new AviWriter(textBoxFileName.Text + ".avi")
{
FramesPerSecond = int.Parse(textBoxFrameRate.Text),
EmitIndex1 = true
};
var stream = writer.AddVideoStream();
stream.Width = thisBitmap.Width;
stream.Height = thisBitmap.Height;
stream.Codec = KnownFourCCs.Codecs.Uncompressed;
stream.BitsPerPixel = BitsPerPixel.Bpp32;
int numberOfFrames = ((int.Parse(textBoxFrameRate.Text)) * (int.Parse(textBoxVideoLength.Text)));
int count = 0;
while (count <= numberOfFrames)
{
stream.WriteFrame(true, byteArray, 0, byteArray.Length);
count++;
}
writer.Close();
MessageBox.Show("Done");
}
private byte[] BitmapToByteArray(Bitmap img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
【问题讨论】:
标签: c# visual-studio-2010 video bitmap bytearray