C# SharpAvi图像到ByteArray帧到AVI
使用SharpAVI.dll将图像转换为视频时遇到问题 我已经通过使用SharpAVI网站上的文档,使用随机生成的字节数组生成了一个视频文件: 所以我想下一步是拍摄图像,创建位图图像,将位图转换为字节数组,然后简单地将字节数组保存到视频文件的每一帧。当我运行程序时,我没有收到任何错误或任何东西,并且生成了一个适当文件大小的视频文件,但是该视频文件无法读取并且无法打开。我真的很难理解为什么这行不通。任何帮助都将不胜感激 我的代码:C# SharpAvi图像到ByteArray帧到AVI,c#,visual-studio-2010,video,bitmap,bytearray,C#,Visual Studio 2010,Video,Bitmap,Bytearray,使用SharpAVI.dll将图像转换为视频时遇到问题 我已经通过使用SharpAVI网站上的文档,使用随机生成的字节数组生成了一个视频文件: 所以我想下一步是拍摄图像,创建位图图像,将位图转换为字节数组,然后简单地将字节数组保存到视频文件的每一帧。当我运行程序时,我没有收到任何错误或任何东西,并且生成了一个适当文件大小的视频文件,但是该视频文件无法读取并且无法打开。我真的很难理解为什么这行不通。任何帮助都将不胜感激 我的代码: private void GenerateSingleImage
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[]));
}
private void generatesingelimagevideo()
{
字符串imagePath=textBoxImagePath.Text;
位图;
//从图像文件生成位图
使用(Stream BitmapStream=System.IO.File.Open(imagePath,FileMode.Open))
{
Image img=Image.FromStream(位图流);
thisBitmap=新位图(img);
}
//将位图转换为字节数组
字节[]byteArray=位图字节数组(此位图);
//创建文件的编写器(以保存视频)
var writer=新的AviWriter(textBoxFileName.Text+“.avi”)
{
FramesPerSecond=int.Parse(textBoxFrameRate.Text),
EmitIndex1=真
};
var stream=writer.AddVideoStream();
stream.Width=此位图.Width;
stream.Height=此位图.Height;
stream.Codec=KnownFourCCs.Codecs.Uncompressed;
stream.BitsPerPixel=BitsPerPixel.Bpp32;
int numberOfFrames=((int.Parse(textBoxFrameRate.Text))*(int.Parse(textBoxVideoLength.Text));
整数计数=0;
while(count您错误地认为应该将位图
对象传递给WriteFrame
方法。它要求像素数据采用从下到上的32bpp格式。请参阅中的示例
// Buffer for pixel data
var buffer = new byte[width * height * 4];
...
// Copy pixels from Bitmap assuming it has expected 32bpp pixel format
var bits = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);
bitmap.UnlockBits(bits);
您可以查看示例应用程序的代码作为参考
最好的例子是“不要发布链接,发布解决方案”。。。