欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)文字視頻生成器的示例代碼

 更新時(shí)間:2022年10月09日 10:15:57   作者:Csharp 小記  
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)簡(jiǎn)易的文字視頻生成器,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下

前言

簡(jiǎn)單的描述下寫這個(gè)軟件的背景吧。之前短視頻平臺(tái)很火的時(shí)候,相信很多人都想進(jìn)去分一杯羹,俺當(dāng)然也不能免俗,但是人丑家窮又沒才藝,咋辦呢?看到別人有只發(fā)文字啥的一些視頻加點(diǎn)背景音樂也能看,想著,Wo Cao?,這我也行啊, I Can I Up。但是讓我天天去找素材剪輯視頻啥的,那肯定干不來,畢竟程序員是需要加班的,所以,這個(gè)粗糙的程序就誕生了,當(dāng)然我也沒怎么用,發(fā)了兩篇覺得不好玩。就沒再玩了。

后來通過種種途徑吧,才知道短視頻背后的產(chǎn)業(yè)相當(dāng)復(fù)雜,一個(gè)視頻能不能火基本不在于視頻本身。

這個(gè)軟件主要是基于錄屏功能來實(shí)現(xiàn)的,不過是一鍵式的罷了,當(dāng)然實(shí)現(xiàn)錄屏我們用了第三方的插件:AForge。項(xiàng)目需要的DLL如下圖:

實(shí)現(xiàn)功能

利用錄屏功能錄制語句的生成過程,并保存成視頻格式

開發(fā)環(huán)境

開發(fā)工具: Visual Studio 2013

.NET Framework版本:4.5

實(shí)現(xiàn)代碼

  public class RecordingUtil
    {
        VideoFileWriter vfWriter = new VideoFileWriter();
        ScreenCaptureStream scStream = null;
 
        readonly Rectangle Rect;
        public RecordingUtil(Rectangle rect, int interval = 40)
        {
            Rect = rect;
            scStream = new ScreenCaptureStream(rect, interval);
            scStream.NewFrame += (s, e1) =>
            {
                vfWriter.WriteVideoFrame(e1.Frame);
            };
        }
 
        public void Start(string savePath, int Rate = 4000 * 1024)
        {
            vfWriter.Open(savePath, Rect.Width, Rect.Height, 25, VideoCodec.MPEG4, 4000 * 1024);
            scStream.Start();
        }
 
        public void Stop()
        {
            if (scStream != null && scStream.IsRunning)
            {
                scStream.Stop();
            }
            if (vfWriter.IsOpen)
            {
                vfWriter.Close();
            }
        }
 
    }
 private void btnCreate_Click(object sender, EventArgs e)
        {
 
            checkNull();
            btnCreate.Text = "正在生成";
            btnCreate.Enabled = false;
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "視頻文件|*.MP4";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                Point point = new Point(this.Location.X + 5, this.Location.Y + 25);
                Size size = new Size(splitContainer1.Panel1.Width % 2 == 1 ? splitContainer1.Panel1.Width - 1 : splitContainer1.Panel1.Width, splitContainer1.Panel1.Height % 2 == 1 ? splitContainer1.Panel1.Height - 1 : splitContainer1.Panel1.Height);
 
                Rectangle rect = new Rectangle(point, size);
                RecordingUtil Recording = new RecordingUtil(rect);
                Recording.Start(sfd.FileName);
                createText(txtWord.Text);
                Recording.Stop();
            }
            btnCreate.Text = "生 成";
            btnCreate.Enabled = true;
        
        }
 
        private void btnPreview_Click(object sender, EventArgs e)
        {
 
            checkNull();
            btnPreview.Text = "正在預(yù)覽";
            btnPreview.Enabled = false;
            createText(txtWord.Text);
            btnPreview.Text = "預(yù) 覽";
            btnPreview.Enabled = true;
        }
 
        private void checkNull()
        {
            if (string.IsNullOrWhiteSpace(txtWord.Text))
            {
                toolTip1.Hide(txtWord);
                toolTip1.Show("不可為空!", txtWord, 5, -60, 2000);
                return;
            }
        }
 
        private void createText(string text)
        {
            Graphics g = splitContainer1.Panel1.CreateGraphics();
            g.Clear(splitContainer1.Panel1.BackColor);
            Font font = new Font("華文行楷", 25);
            // Brush whiteBrush = new SolidBrush(Color.FromArgb(0, 192, 0));
            Brush whiteBrush = new SolidBrush(Color.Black);
            int x = 0, y = 0;
            string[] arr = txtWord.Text.Split('\n');
            for (int i = 0; i < arr.Length; i++)
            {
                x = 40 * i + 15;
                for (int j = 0; j < arr[i].Length; j++)
                {
                    y = 40 * j + 15;
                    g.DrawString(arr[i][j].ToString(), font, whiteBrush, x, y);
                    Delay(300);
                }
            }
        }
 
        private void Delay(double mm)
        {
            DateTime now = DateTime.Now;
            while (DateTime.Now.AddMilliseconds(-mm) <= now)
            {
                Application.DoEvents();
            }
        }

實(shí)現(xiàn)效果

以上就是C#實(shí)現(xiàn)文字視頻生成器的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#文字視頻生成器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論