首页 文章

带压缩的AVI / WMV的C#位图

提问于
浏览
2

Prelude:

我打算用这个作为序言,我在工作时间里一直在学习C#,并且我一直在盯着代码坚持两天试图解决这个问题 . 我正在开发一些软件,用于通过USB连接到标准台式PC的可视化器,软件检测捕获设备并使用新帧事件将帧加载到位图中,然后将其显示在“图片框”中作为直播视频流 . 它所处的问题是试图加强记录流并保存到文件的能力,最好是WMV或压缩的AVI .

What's been tried:

我考虑过并研究过以下内容:

SharpAVI - 似乎无法正常压缩或保存帧,因为它似乎主要是查看现有的AVI文件 .

AForge.Video.VFW - 由于对将要使用该软件的个人的用户区域的限制,可以创建AVI文件但是太大而无法使用 .

AForge.Video.FFMPEG - 再次由于使用这个软件的人的考虑,我可以__40726_ s与可执行文件坐在输出文件夹中,不幸的是这个特定的DLL不能使用Costura Fody正确编译 .

AVIFile库包装器(来自代码项目) - 似乎无法通过新帧事件从Bitmaps正确压缩流 .

DirectShow - 似乎使用C,不幸的是,此时超出了我的技能水平 .

The Relevant Code Snippets:

目前的参考文献:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Resources;
using System.Drawing.Imaging;
using System.IO;
//Aforge Video DLL's
using AForge.Video;
using AForge.Video.VFW;
using AForge.Video.DirectShow;
//Aforge Image DLL's
using AForge.Imaging;
using AForge.Imaging.Formats;
using AForge.Imaging.Filters;
//AviLibrary
using AviFile;

全局变量:

#region Global Variables

    private FilterInfoCollection CaptureDevice; // list of available devices
    private VideoCaptureDevice videoSource;
    public System.Drawing.Image CapturedImage;

    bool toggleMic = false;

    bool toggleRec = false;
    //aforge
    AVIWriter aviWriter;
    Bitmap image;


    #endregion

显示流的代码

#region Displays the Stream

    void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        picBoxStream.SizeMode = PictureBoxSizeMode.Zoom;
        picBoxStream.Image = (Bitmap)eventArgs.Frame.Clone();// clones the bitmap


        if (toggleRec == true)
        {
            image = (Bitmap)eventArgs.Frame.Clone();
            aviWriter.AddFrame(image);
        }
    }

    #endregion

当前的录制流代码

#region Record Button

    private void btnRecord_Click(object sender, EventArgs e)
    {
        if (toggleRec == false)
        {
            saveAVI = new SaveFileDialog();
            saveAVI.Filter = "AVI Files (*.avi)|*.avi";

            if (saveAVI.ShowDialog() == DialogResult.OK)
            {
                aviWriter = new AVIWriter();
                aviWriter.Open(saveAVI.FileName, 1280, 720);

                toggleRec = true;
                Label lblRec = new Label();
            }

        }
        else if (toggleRec == true)
        {
            aviWriter.Close();
            toggleRec = false;
        }
    }

    #endregion

如果上面的代码看起来不太正确,我会进行代理,我一直在交换,更改和重新编码这三个部分以便找到一个有效的组合 . 这意味着它相当凌乱但我没有看到清理它的重点,直到我的代码工作 . 说实话,你能提供的任何帮助都会得到很好的回报,即使这是我想要做的事情也是无法完成的 .

先感谢您 .

EDIT: PostScript:

我将在星期一到星期五积极工作,所以如果我做出任何突破,我会相应地更新这个问题,这似乎是经常回答的问题,所以希望我们能够克服这些问题 .

2 回答

  • 0

    和我刚刚解决的问题一样:)这里是我的代码如何使用AviFile库dll制作avi文件:)

    private void MakeAvi(List<Bitmap> maps)
        {
            AviManager mana = new AviManager("local.avi", false);
    
            //false means do not show the diag of the Compression 
            //21 means the fps of the video
            //maplist[0] cover of the video  the maplist is the val you should insert 
            VideoStream avistream = mana.AddVideoStream(false, 21, maplist[0]);
    
            for (int i = 1; i < maps.Count; i++)
            {
                avistream.AddFrame(maplist[i]);
            }
    
            mana.Close();
            MessageBox.Show("AddOk");
        }
    
  • 0
    namespace ip_androidcam
    {
        public partial class Form1 : Form
        {   
            MJPEGStream stream;
            AVIWriter writer;
            bool toggleRec = false;
            public Form1()
            {
                InitializeComponent() ;
    
            }
    
            void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
            {
                Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();
                bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
                pictureBox1.Image = bmp;
    
                try
                {
                    if (toggleRec == true)
                    {
                        bmp = (Bitmap)eventArgs.Frame.Clone();
                        bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
                        writer.AddFrame(bmp);
                    }
                }
                catch (Exception e)
                {
                }
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    stream = new MJPEGStream(textBox1.Text);
                    stream.NewFrame +=stream_NewFrame;
                    stream.Start();
                }
                catch
                {
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    if (stream.IsRunning == true)
                    {
                        stream.Stop();
                    }
                }
                catch
                { 
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                try
                {
                    pictureBox1.Image = (Bitmap)pictureBox1.Image.Clone();
                    pictureBox1.Image.Save("D:\\Pictures\\pix-" + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss" + ".jpeg" + ImageFormat.Jpeg));
                }
                catch (Exception)
                {
                    MessageBox.Show("Capture Image First");
                }
    
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.ImageLocation = ofd.FileName;
                }
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                if (toggleRec == false)
                {
                    SaveFileDialog saveAVI = new SaveFileDialog();
                    saveAVI.Filter = "AVI Files (*.avi)|*.avi";
    
                    if (saveAVI.ShowDialog() == DialogResult.OK)
                    {
                         writer = new AVIWriter("XVID");
                         writer.Open(saveAVI.FileName , 480, 640);
                         toggleRec = true;
                         Label lblRec =new Label();
                    }
                }
    
    
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                try
                {
                    writer.Close();
                    MessageBox.Show("video recorded successfully");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
        }
    }
    

相关问题