[英]HoloLens 2 UWP bitmap image artifacting
我正在構建一個 2D UWP 應用程序以在 HoloLens 2 上運行。在應用程序中,我使用 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;
}
一如既往地感謝您對此的任何幫助!
這最終修復了它。 我需要將位圖文件保存到設備並重新讀取它。不理想但它有效。
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;
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.