我有一个WPF项目,并将USB摄像机的图像捕获到System.Drawing.Bitmap(我也可以捕获System.Windows.Media.Imaging.BitmapSource),并且需要将其转换为Windows.Graphics.Imaging .SoftwareBitmap制作“ VideoFrame”以与Onnx模型进行比较。
相机驱动程序是.net程序集,不会绑定到uwp项目。我尝试创建.net标准程序集以弥合差距,但没有成功。我只需要将位图转换为SoftwareBitmap。请帮忙!
我将这段代码用于相机的位图图像的同情心-https://github.com/Azure-Samples/cognitive-services-onnx12-customvision-sample
答案 0 :(得分:0)
没有直接转换。您需要从System.Drawing.Bitmap中提取图像数据,然后根据该数据创建新的SoftwareBitmap。
例如,您可以使用Save(Stream, ImageFormat)方法将该图像以指定格式保存到指定流。
然后,您可以尝试调用BitmapDecoder.CreateAsync方法以从流中创建解码器。
之后,您可以调用GetSoftwareBitmapAsync来获取SoftwareBitmap对象。
以下是一个简单的代码示例:
Bitmap bitmap = getyourbitmap();
using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
{
bitmap.Save(stream.AsStream(),ImageFormat.Jpeg);//choose the specific image format by your own bitmap source
Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
}
答案 1 :(得分:0)
我发现您可以使用Windows.Security.Cryptography从图像数组字节创建IBuffer。然后,您可以将IBuffer复制到SoftwareBitmap。
using Windows.Security.Cryptography;
IBuffer buffer = CryptographicBuffer.CreateFromByteArray(ImageByteArray);
SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Gray8, 800, 600);
softwareBitmap.CopyFromBuffer(buffer);
VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);