我正在尝试将视频从直升机转移到另一个应用程序。 如何将传输到此函数的字节数组保存在文件中或在界面中显示?
/// <summary>
/// Decode data. Do nothing here. This function would return a bytes array with image data in RGBA format.
private void ReceiveDecodedData(byte[] data, int width, int height)
{
}
对于UWP应用程序,我设法编写了以下代码:
public async Task SaveImage(byte[] bytes)
{
try
{
var buffer = CryptographicBuffer.CreateFromByteArray(bytes);
var outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, 1280, 1024);
var storageFolder = ApplicationData.Current.LocalFolder;
var sampleFile = await storageFolder.CreateFileAsync($"{Guid.NewGuid()}.bmp", CreationCollisionOption.ReplaceExisting);
await SaveSoftwareBitmapToFile(outputBitmap, sampleFile);
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
Debug.WriteLine(exception.StackTrace);
}
}
private async Task SaveSoftwareBitmapToFile(SoftwareBitmap softwareBitmap, IStorageFile outputFile)
{
using (var stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
{
// Create an encoder with the desired format
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
// Set the software bitmap
encoder.SetSoftwareBitmap(softwareBitmap);
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
encoder.IsThumbnailGenerated = true;
try
{
await encoder.FlushAsync();
}
catch (Exception err)
{
}
}
}
但是当我尝试制作1920x1080的图像时,此代码会因OutOfMemory而崩溃。
我做错了什么? 如何为WPF应用程序重复此代码?