前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#实现文字视频生成器的示例代码分享

C#实现文字视频生成器的示例代码分享

原创
作者头像
用户7718188
发布2022-11-06 20:24:15
5490
发布2022-11-06 20:24:15
举报
文章被收录于专栏:高级工程司

实现代码

1

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();          }      }   }

2

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 = "正在预览";           btnPreview.Enabled = false;           createText(txtWord.Text);           btnPreview.Text = "预 览";           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();           }       }

实现效果

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现代码
  • 实现效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档