C# Unity UWP加载图像字节缺少颜色通道

C# Unity UWP加载图像字节缺少颜色通道,c#,unity3d,uwp,texture2d,windows-mixed-reality,C#,Unity3d,Uwp,Texture2d,Windows Mixed Reality,我正在加载一个图像字节并尝试将其应用于纹理2D 不要担心异步/等待/线程问题 UWP代码: StorageFile storageFile = StorageFile.GetFileFromPathAsync(filePath).AsTask().GetAwaiter().GetResult(); // get image size IRandomAccessStreamWithContentType random = storageFile.OpenReadAsync().AsTask().

我正在加载一个图像字节并尝试将其应用于
纹理2D

不要担心异步/等待/线程问题

UWP代码:

StorageFile storageFile = StorageFile.GetFileFromPathAsync(filePath).AsTask().GetAwaiter().GetResult();

// get image size
IRandomAccessStreamWithContentType random = storageFile.OpenReadAsync().AsTask().GetAwaiter().GetResult();
BitmapDecoder decoder = BitmapDecoder.CreateAsync(random).AsTask().GetAwaiter().GetResult();
BitmapFrame bitmapFrame = decoder.GetFrameAsync(0).AsTask().GetAwaiter().GetResult();
PixelDataProvider pixelData = bitmapFrame.GetPixelDataAsync().AsTask().GetAwaiter().GetResult();

return new Dictionary<string, object>
{
    {"bytes", pixelData.DetachPixelData()},
    {"width", (int) decoder.PixelWidth},
    {"height", (int) decoder.PixelHeight}
};
这就是图像显示的方式

原件:

在应用程序中(为白色的大正方形感到抱歉):

您的图像频道没有丢失,它们只是以不同的顺序排列

检查以下文件:

根据纹理的宽度、高度、数据格式和mipmapCount,传递的数据应具有填充整个纹理所需的大小;否则将抛出UnityException

解决方案:


TextureFormat.BGRA32
传递给您的
Texture2D
构造函数。

在UWP端,需要使用正确的参数从解码器获取像素。请遵循以下解决方案:

StorageFile storageFile = StorageFile.GetFileFromPathAsync(filePath).AsTask().GetAwaiter().GetResult();
IRandomAccessStreamWithContentType random = storageFile.OpenReadAsync().AsTask().GetAwaiter().GetResult();
BitmapDecoder decoder = BitmapDecoder.CreateAsync(random).AsTask().GetAwaiter().GetResult();

// here is the catch
PixelDataProvider pixelData = decoder.GetPixelDataAsync(
    BitmapPixelFormat.Rgba8, // <--- you must to get the pixels like this
    BitmapAlphaMode.Straight,
    new BitmapTransform(),
    ExifOrientationMode.RespectExifOrientation,
    ColorManagementMode.DoNotColorManage // <--- you must to set this too
).AsTask().GetAwaiter().GetResult();
StorageFile-StorageFile=StorageFile.getfilefrompathsync(filePath).AsTask().GetAwaiter().GetResult();
irandomaccesstreamwithcontenttype random=storageFile.OpenReadAsync().AsTask().GetAwaiter().GetResult();
BitmapDecoder=BitmapDecoder.CreateAsync(随机).AsTask().GetAwaiter().GetResult();
//这是陷阱
PixelDataProvider pixelData=decoder.GetPixelDataAsync(

BitmapPixelFormat.Rgba8,//谢谢你的回答,但我发现这不是Unity中的加载,而是UWP中的加载,我将在几分钟后发布答案。。。
StorageFile storageFile = StorageFile.GetFileFromPathAsync(filePath).AsTask().GetAwaiter().GetResult();
IRandomAccessStreamWithContentType random = storageFile.OpenReadAsync().AsTask().GetAwaiter().GetResult();
BitmapDecoder decoder = BitmapDecoder.CreateAsync(random).AsTask().GetAwaiter().GetResult();

// here is the catch
PixelDataProvider pixelData = decoder.GetPixelDataAsync(
    BitmapPixelFormat.Rgba8, // <--- you must to get the pixels like this
    BitmapAlphaMode.Straight,
    new BitmapTransform(),
    ExifOrientationMode.RespectExifOrientation,
    ColorManagementMode.DoNotColorManage // <--- you must to set this too
).AsTask().GetAwaiter().GetResult();