WinRT从视频中提取帧作为图像

时间:2013-03-02 19:45:59

标签: windows video windows-runtime

我正在构建涉及一些视频处理的Windows 8应用程序。现在我一直坚持从.mp4视频片段中提取一个帧作为BitmapImage。我在网上看到的两个例子是herehere,但是在尝试创建BitmapDecoder时我遇到了异常。我已粘贴到目前为止我已经得到的代码,但请注意,我不知道我在做什么。

    public async void Extract_Image_From_Video(StorageFile video_file)
    {

        // Create image
        BitmapImage image = new BitmapImage();

        // Open the video file as a stream
        IRandomAccessStream readStream = await video_file.OpenAsync(FileAccessMode.Read);

        // Breaks here 
        BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(readStream);

        BitmapFrame frame = await bmpDecoder.GetFrameAsync(0);

        BitmapTransform bmpTrans = new BitmapTransform();

        bmpTrans.InterpolationMode = BitmapInterpolationMode.Cubic;

        PixelDataProvider pixelDataProvider = await frame.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, bmpTrans, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);

        byte[] pixelData = pixelDataProvider.DetachPixelData();

        InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();

        BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ras);
        // write the pixel data to our stream
        enc.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, 200, 200, bmpDecoder.DpiX, bmpDecoder.DpiY, pixelData);

        await enc.FlushAsync();

        // this is critical and below does not work without it!
        ras.Seek(0);

        // Set to the image
        image.SetSource(ras);

        // PreviewImage is an image control defined in the xaml
        PreviewImage.Source = gridImage;
    }

1 个答案:

答案 0 :(得分:1)

我为此创建了一个c ++项目,可以由c#app使用。看看this