You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

商用.NET Core+C#文件管理器:无需FFmpeg生成视频缩略图

可行的视频缩略图生成方案(无FFmpeg依赖)

方案1:Windows平台使用Media Foundation API(原生系统API,商用合规)

Media Foundation是Windows自带的多媒体处理框架,无需额外安装依赖,完全满足商用合规要求。可通过WinRT API调用其能力读取视频帧并生成缩略图。

实现代码示例:

using Windows.Media.Playback;
using Windows.Media.Core;
using Windows.Storage;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;

public async Task GenerateVideoThumbnail(string videoPath, string outputPath, int targetWidth = 200)
{
    var storageFile = await StorageFile.GetFileFromPathAsync(videoPath);
    var mediaPlayer = new MediaPlayer();
    mediaPlayer.Source = MediaSource.CreateFromStorageFile(storageFile);

    // 等待媒体资源加载完成
    await mediaPlayer.MediaOpened;

    // 跳转到视频第1秒位置截取帧(可根据需求调整时间点)
    mediaPlayer.Position = TimeSpan.FromSeconds(1);

    // 获取当前帧并转换为Bitmap保存
    var frameSurface = await mediaPlayer.PrepareSurfaceAsync();
    using (var stream = new MemoryStream())
    {
        frameSurface.Snapshot().Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        stream.Seek(0, SeekOrigin.Begin);
        
        using (var originalBitmap = new Bitmap(stream))
        {
            // 按比例缩放缩略图
            int targetHeight = (int)(originalBitmap.Height * ((float)targetWidth / originalBitmap.Width));
            using (var scaledBitmap = new Bitmap(targetWidth, targetHeight))
            using (var graphics = Graphics.FromImage(scaledBitmap))
            {
                graphics.DrawImage(originalBitmap, 0, 0, targetWidth, targetHeight);
                scaledBitmap.Save(outputPath);
            }
        }
    }
    mediaPlayer.Dispose();
}

注意:此方案仅适用于Windows系统,需通过NuGet安装Microsoft.Windows.SDK.Contracts包以在.NET Core中调用WinRT API。

方案2:跨平台结合原生组件+SkiaSharp

若需跨平台支持,可结合各平台原生视频解码能力+SkiaSharp(跨平台图形库)实现:

  • Linux:使用GStreamer的.NET绑定GStreamerSharp,依赖系统预装的GStreamer组件(多数Linux发行版默认包含,商用合规)
  • MacOS:使用AVFoundation的.NET绑定AVFoundationSharp调用原生视频解码API
  • Windows:复用方案1的Media Foundation逻辑

Linux平台GStreamer示例:

using Gst;
using System;

public void GenerateLinuxThumbnail(string videoPath, string outputPath)
{
    Application.Init();
    // 构建GStreamer管道:读取视频→解码→转码为PNG→保存文件
    var pipeline = Parse.Launch($"filesrc location={videoPath} ! decodebin ! videoconvert ! pngenc ! filesink location={outputPath}");
    
    pipeline.SetState(State.Playing);
    var bus = pipeline.Bus;
    Message msg;
    
    // 等待处理完成或出错
    do
    {
        msg = bus.TimedPop(Clock.TimeNone);
    } while (msg != null && msg.Type != MessageType.Eos && msg.Type != MessageType.Error);
    
    pipeline.SetState(State.Null);
    pipeline.Dispose();
}

方案3:利用DevExpress FileManager内置能力

若仅需在FileManager控件内显示视频缩略图,可直接启用控件的内置媒体预览功能:

  • 开启SettingsPreview.Enabled,设置PreviewMode为Media
  • 控件会自动调用系统原生媒体组件生成缩略图,无需额外编写代码,完全符合商用要求

ASP.NET Core配置示例:

@Html.DevExpress().FileManager(settings => {
    settings.Name = "videoFileManager";
    settings.SettingsPreview.Enabled = true;
    settings.SettingsPreview.PreviewMode = FileManagerPreviewMode.Media;
    // 绑定文件存储等其他配置
}).BindToFolder(Model.RootFolder).GetHtml()

内容的提问来源于stack exchange,提问作者gaurav khare

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 07:40:33