我正在加载图像字节并尝试将其应用到Texture2D
.
不要担心异步/等待/线程问题...
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}
};
统一代码:
Texture2D texture = new Texture2D(textureSizeStruct.width, textureSizeStruct.height, TextureFormat.RGBA32, false);
texture.LoadRawTextureData(textureBytes);
texture.Apply();
图像是这样显示的...