C#屏幕录像机减小AVI视频文件的大小

时间:2018-11-28 02:46:12

标签: c# compression screenshot filesize

我目前正在尝试用C#制作屏幕录像机,到目前为止,它仍然可以工作,但是问题是,像20秒的视频这样简单的东西将占用大约1GB的空间。我已经设置好了,因此计时器使用此方法连续拍摄屏幕截图:

void takeScreenshot()
        {
            Rectangle bounds = Screen.FromControl(this).Bounds;
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    //Add screen to bitmap:
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }
                //Create and save screenshot:
                string name = path + "//screenshot-" + fileCount + ".jpeg";
                bitmap.Save(name, ImageFormat.Jpeg);
                inputImageSequence.Add(name);
                fileCount++;

                //Dispose of bitmap:
                bitmap.Dispose();
            }
        }

然后将这些图片存储在D://驱动器的临时文件夹中,然后完成所有照片并从中创建AVI视频,如下所示:

//Set bounds of video to screen size:
            Rectangle bounds = Screen.FromControl(this).Bounds;
            int width = bounds.Width;
            int height = bounds.Height;

            var framRate = 5;

            using (var vFWriter = new VideoFileWriter())
            {
                //Create new video file:
                vFWriter.Open(outputPath+"//video.avi", width, height, framRate, VideoCodec.Raw);

                //Make each screenshot into a video frame:
                foreach (var imageLocation in inputImageSequence)
                {
                    Bitmap imageFrame = System.Drawing.Image.FromFile(imageLocation) as Bitmap;
                    vFWriter.WriteVideoFrame(imageFrame);
                    imageFrame.Dispose();
                }
                vFWriter.Close();
            }
            //Delete the screenshots and temporary folder:
            DeletePath(path);

感谢大家对减少这种效率低下的任何帮助,对于这种编程我还是很陌生。

0 个答案:

没有答案