html和winform webBrowser控件交互并播放视频(包含转码)

本文介绍了如何在WindowsForm应用中使用webBrowser控件与HTML页面交互,包括设置Com的可访问性、通过window.external调用后台方法,以及利用FFmpeg进行视频转换和处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、 为了使网页能够与winform交互 将com的可访问性设置为真
   

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]

2、在webBrowser控件中设置可被html页面调用的类即:webBrowser1.ObjectForScripting = this;前端即可通过window.external访问this对象

3、html页面调用后台方法:window.external.方法名(); 此处的window.external相当于webBrowser1.ObjectForScripting

设计:

 后台代码:

注意:视频地址需要修改

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WebKit;
using System.IO;
using DXApplication1.Helper;

namespace DXApplication1
{
    //为了使网页能够与winform交互 将com的可访问性设置为真
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class webBrowser : DevExpress.XtraEditors.XtraForm
    {
        public webBrowser()
        {
            InitializeComponent();
           
        }

        private void webBrowser_Load(object sender, EventArgs e)
        {
            webBrowser1.ScriptErrorsSuppressed = true;//消除脚本错误 
            webBrowser1.Navigate(@"C:\source\repos\DXApplication1\DXApplication1\Html\HTMLPage2.html");
            webBrowser1.ObjectForScripting = this;
            
        }
        
        public string FileLode(string url)
        {
            //返回路径
            string result = url;
            if (!string.IsNullOrEmpty(url))
            {
                //保存文件名称
                string saveName = Guid.NewGuid().ToString() + ".mp4"; 
                string basePath = "UploadFile";
                string saveDir = DateTime.Now.ToString("yyyy-MM-dd");
                //获取应用程序的当前工作目录
                string MapPath = Directory.GetCurrentDirectory();
                // 文件上传后的保存路径
                string serverDir = Path.Combine(MapPath, basePath, saveDir);
                //保存文件完整路径
                string fileNme = Path.Combine(serverDir, saveName);
                //项目中是否存在文件夹,不存在创建                                 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值