我正在尝试创建一个MediaComposition.我已经成功地将多个图像组合成一个视频;但是,创建的文件具有黑色背景.起初我以为这可能是因为这些文件是png文件,但是对于jpg来说,这个文件是相同的.以下是我如何保存图像:
public async Task<bool> Save(InkCanvas canvas,StorageFile file) { if (canvas != null && canvas.InkPresenter.StrokeContainer.GetStrokes().Count > 0) { if (file != null) { using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { await canvas.InkPresenter.StrokeContainer.SaveAsync(stream); } } Clear(canvas); return true; } return false; }
它保存图像很好,但背景是alpha.这意味着当我尝试将它们链接在一起成为媒体作品时,没有背景,它呈现为黑色.在创建MediaComposition以更正此问题时,我已尝试使用叠加层:
MediaClip overlayVideoClip = MediaClip.CreateFromColor(Colors.White,new TimeSpan(0,1,0)); MediaOverlay mo = new MediaOverlay(overlayVideoClip); MediaOverlayLayer mol = new MediaOverlayLayer(); mol.Overlays.Add(mo); composition.OverlayLayers.Add(mol);
但无济于事我的怀疑是,在这种情况下,我误会了术语叠加的含义.所以,我的问题是:是否可以在构图时覆盖视频,如果是,怎么办?或者,如果这需要在图像本身完成,我如何保存图像与背景?
编辑:
我已经取得进展(?)以下编译和运行,但创建一个坚实的黑色图像:
public async Task TestSave(InkCanvas canvas,StorageFile file) { RenderTargetBitmap rtb = new RenderTargetBitmap(); PixelFormats.Pbgra32); await rtb.RenderAsync(canvas); var pixelBuffer = await rtb.GetPixelsAsync(); using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId,stream); encoder.SetPixelData(BitmapPixelFormat.Bgra8,BitmapAlphaMode.Straight,(uint)rtb.PixelWidth,(uint)rtb.PixelHeight,96d,pixelBuffer.ToArray()); await encoder.FlushAsync(); } }
编辑:
我发现this的答案,哪种解决了使用Win2D库的问题;虽然它没有解决我的实际问题,但它让我放松了.希望有一个更好的解决方案.
解决方法
我唯一理解的是您正在尝试保存具有背景的图像.解决这个问题的建议是保存你所拥有的透明图像
<StackPanel x:Name="AreaWhichWillBeSavedToImage" Background=*Some Color*> <Image x:Name="theAlphaImage"> </StackPanel>
现在,如果您不希望图像在GUI上显示,请将其设置为隐藏.
然后,您可以使用您选择的颜色背景保存文件.
var bitmap = await SaveToFileAsync(AreaWhichWillBeSavedToImage,await StorageFile.GetFileFromPathAsync(Windows.ApplicationModel.Package.Current.InstalledLocation.Path + @"someimage.jpg")); async Task<RenderTargetBitmap> SaveToFileAsync(FrameworkElement uielement,StorageFile file) { if (file != null) { CachedFileManager.DeferUpdates(file); Guid encoderId = GetBitmapEncoder(file.FileType); try { using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { return await CaptureToStreamAsync(uielement,stream,encoderId); } } catch (Exception ex) { //DisplayMessage(ex.Message); } var status = await CachedFileManager.CompleteUpdatesAsync(file); } return null; } async Task<RenderTargetBitmap> CaptureToStreamAsync(FrameworkElement uielement,IRandomAccessStream stream,Guid encoderId) { try { var renderTargetBitmap = new RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(uielement); var pixels = await renderTargetBitmap.GetPixelsAsync(); var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi; var encoder = await BitmapEncoder.CreateAsync(encoderId,stream); encoder.SetPixelData( BitmapPixelFormat.Bgra8,BitmapAlphaMode.Ignore,(uint)renderTargetBitmap.PixelWidth,(uint)renderTargetBitmap.PixelHeight,logicalDpi,pixels.ToArray()); await encoder.FlushAsync(); return renderTargetBitmap; } catch (Exception ex) { //DisplayMessage(ex.Message); } return null; } Guid GetBitmapEncoder(string fileType) { Guid encoderId = BitmapEncoder.JpegEncoderId; switch (fileType) { case ".bmp": encoderId = BitmapEncoder.BmpEncoderId; break; case ".gif": encoderId = BitmapEncoder.GifEncoderId; break; case ".png": encoderId = BitmapEncoder.PngEncoderId; break; case ".tif": encoderId = BitmapEncoder.TiffEncoderId; break; } return encoderId; }