我正在构建一个涉及一些视频处理的 Windows 8 应用程序。现在我坚持从 .mp4 视频剪辑中提取帧作为 BitmapImage。我一直在网上查看的两个示例是here和here,但是在尝试创建 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;
}