如何使用PictureBox播放视频

对于winform设计者来说,我们如果想实现和网页相同的效果,真心感觉很难。为此,今天我们谈谈winform注册窗体如何使用视频当做背景,我们用到的控件为PictureBox。PictureBox 控件可以显示来自位图、图标或者元文件,以及来自增强的元文件、JPEG 或 GIF 文件的图形。在讲述这个方法之前先看下效果,如图1所示:


图1 效果展示图

我们如果想用PictureBox有几种方法,第一种是使用提前设计好的Gif图片,第二种是使用一组图片然后使用定时器循环播放图片,第三种也就是我们今天要讲的直接播放视频。PictureBox播放视频用法的是mciSendString方法,mciSendString是用于播放多媒体文件的API指令,其可以播放MPEG、AVI、WAV、MP3等等,下面我们来介绍一下它的使用方法:

第一、打开媒体文件

首先我们在winform上拖放一个PictureBox控件,设置其Dock属性为Fill,使其全部铺满。我们定义一个新的PictureBox,然后将刚才拖放的控件赋值为新的PictureBox。由于我们所需要播放的文件放在程序根目录指定目录下,为了防止播放视频找不到,我们需要判断目录下的文件目录是否存在 

path = System.Windows.Forms.Application.StartupPath + @"\Background";

if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

我们定义一个string mciCommand;用于赋值我们需要操作的指令。打开多媒体指令为“open " + path + " alias MyAVI”,接着赋值对应的控件  mciCommand + " parent " + PlayScreen.Handle.ToInt32() + " style child";【注:PlayScreen为新定义的控件名称

第二、设置播放窗体的大小

我们取当前播放控件的宽和高,代码如:Rectangle r = PlayScreen.ClientRectangle;mciCommand = "put MyAVI window at 0 0 " + r.Width + " " + r.Height;这样我们就设置好了视频的大小。

第三、播放视频

在实现播放视频前我们先了解下mciSendString播放视频的几个操作。

a)、全屏播放:
mciSendString("play movie fullscreen",buf,sizeof(buf),NULL);
b)、暂停播放。
mciSendString("pause movie",buf,sizeof(buf),NULL);
c)、停止播放。
mciSendString("close movie",buf,sizeof(buf),NULL);

d)、播放视频

mciSendString("play movie ",buf,sizeof(buf),NULL);

我们既然想要做个背景视频,那么我们需要我们的视频一直循环播放,那只需要在play movie 后面加上repeat即可实现循环播放视频。

为了能我们再次启动窗体的时候,视频可以正常播放,那我们需要在窗体关闭前将我们播放的视频关闭。

这样我们就实现我们想要的效果,接着你就可以往上面添你所需要的控件,设计你理想的注册窗体。完整代码如下:

   	#region 背景视频播放
        private class LibWrap
        {
            [DllImport(("winmm.dll"), EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
            public static extern int mciSendString(string lpszCommand, string lpszReturnString, uint cchReturn, IntPtr hwndCallback);
        }
        private void PlayViedo()
        {
            PictureBox PlayScreen = new PictureBox();
            PlayScreen = this.pictureBox;
            string mciCommand;
            string path = string.Empty;
            path = System.Windows.Forms.Application.StartupPath + @"\Background";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += @"\bg.wmv";
            mciCommand = "open " + path + " alias MyAVI";
            mciCommand = mciCommand + " parent " + PlayScreen.Handle.ToInt32() + " style child";
            LibWrap.mciSendString(mciCommand, null, 0, IntPtr.Zero);
            Rectangle r = PlayScreen.ClientRectangle;
            mciCommand = "put MyAVI window at 0 0 " + r.Width + " " + r.Height;
            LibWrap.mciSendString(mciCommand, null, 0, IntPtr.Zero);
            // LibWrap.mciSendString("play MyAVI", null, 0, IntPtr.Zero);
            LibWrap.mciSendString("play MyAVI repeat", null, 0, IntPtr.Zero);
        }
        #endregion
  	#region  防止再次启动窗体无法播放视频,窗体关闭前关闭播放视频
        private void registers_FormClosing(object sender, FormClosingEventArgs e)
        {
            LibWrap.mciSendString("close MyAVI ", null, 0, IntPtr.Zero);
        }
        #endregion



  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
C#中,可以使用OpenCVSharp库来实现PictureBox播放视频的功能。以下是实现该功能的步骤: 1. 首先,需要安装OpenCVSharp库。可以在NuGet包管理器中搜索并安装OpenCVSharp4。 2. 在窗体中添加一个PictureBox控件,并设置其大小和位置。 3. 在代码中导入OpenCVSharp的命名空间。 4. 使用VideoCapture类打开视频文件,并使用Mat类读取每一帧图像。 5. 将读取到的图像转换为Bitmap格式,并将其赋值给PictureBox的Image属性。 6. 在需要的时候,可以使用Graphics类在PictureBox上绘制方框。 以下是示例代码: ```csharp using OpenCvSharp; using System; using System.Drawing; using System.Windows.Forms; namespace VideoPlayer { public partial class Form1 : Form { private VideoCapture capture; private Mat frame; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { capture = new VideoCapture("video.mp4"); frame = new Mat(); timer1.Interval = 33; // 设置定时器间隔为33毫秒,即每秒30帧 timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { capture.Read(frame); // 读取一帧图像 if (!frame.Empty()) { Bitmap bitmap = BitmapConverter.ToBitmap(frame); // 将Mat转换为Bitmap pictureBox1.Image = bitmap; // 显示图像 } } private void pictureBox1_Paint(object sender, PaintEventArgs e) { // 在PictureBox上绘制方框 e.Graphics.DrawRectangle(Pens.Red, new Rectangle(100, 100, 200, 200)); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值