我正在构建一个在HoloLens 2上运行的2DUWP应用程序。在该应用程序中,我使用RenderTargetBitmap渲染视频馈送UI对象的位图,以从它获得一个静止图像作为SoftwareBitmap对象。令人沮丧的是,当它在PC上以x86模式运行时,它工作得很好。然而,当我在Hololens 2 (ARM)上运行时,图像被一个伪像弄乱了,把图像搞乱了。它似乎被拆分成不匹配的行。我添加了将图像文件保存到设备上的文件夹中的功能,输出的图像看起来很好,但当它在应用程序UI中设置为imageSource时,它看起来完全搞砸了。
在我的VideoControl.xaml.cs代码中:
private static async Task SetPauseImageSource()
{
await Instance.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
SoftwareBitmap image = await Xaml2Bitmap.CreateBitmapFromElement(Instance, true);
var source = new SoftwareBitmapSource();
await source.SetBitmapAsync(image);
Instance.imagePause.Source = source;
});
}在我的Xaml2Bitmap.cs类中:
public static async Task<SoftwareBitmap> CreateBitmapFromElement(FrameworkElement uielement, bool saveFile = false, string filenamePrefix = "image")
{
SoftwareBitmap bitmap = null;
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(uielement);
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
bitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, (int)uielement.ActualWidth, (int)uielement.ActualHeight, BitmapAlphaMode.Ignore);
if (bitmap == null)
{
Debug.WriteLine("Bitmap is null");
}
if (saveFile)
{
var file = await SaveFile(filenamePrefix);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
logicalDpi,
logicalDpi,
pixelBuffer.ToArray());
await encoder.FlushAsync();
}
}
return bitmap;
}
private static async Task<StorageFile> SaveFile(string filenamePrefix)
{
var savePicker = new FileSavePicker();
savePicker.DefaultFileExtension = ".png";
savePicker.FileTypeChoices.Add(".png", new List<string> { ".png" });
savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
savePicker.SuggestedFileName = filenamePrefix + ".png";
// Prompt the user to select a file
var saveFile = await savePicker.PickSaveFileAsync();
return saveFile;
}

在这方面的任何帮助都将一如既往地受到感谢!
发布于 2020-09-25 03:04:40
这最终解决了它。我需要将位图文件保存到设备上,然后再读回来。不是很理想,但它是有效的。
public static async Task<string> CreateBitmapFromElement(FrameworkElement uielement)
{
SoftwareBitmap bitmap = null;
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(uielement);
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
bitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, (int)uielement.ActualWidth, (int)uielement.ActualHeight, BitmapAlphaMode.Ignore);
if (bitmap == null)
{
Debug.WriteLine("Bitmap is null");
}
var folder = KnownFolders.PicturesLibrary;
var file = await folder.CreateFileAsync("pause.bmp", CreationCollisionOption.ReplaceExisting);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
logicalDpi,
logicalDpi,
pixelBuffer.ToArray());
await encoder.FlushAsync();
await stream.WriteAsync(pixelBuffer);
}
return file.Name;
}
private static async Task SetImageSource()
{
await Instance.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
StorageFolder storageFolder = KnownFolders.PicturesLibrary;
string imagePath = await Xaml2Bitmap.CreateBitmapFromElement(Instance);
var file = await storageFolder.GetFileAsync(imagePath);
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
Instance.imagePause.Source = bitmapImage;
}
});
}https://stackoverflow.com/questions/64044440
复制相似问题