从 ushort[] 创建 BitmapImage

发布于 2024-10-25 03:13:44 字数 189 浏览 5 评论 0原文

是否可以从 ushort 数组创建 BitmapImage?如果是这样怎么办?

目前我正在创建一个位图,将其转换为位图数组并显示它,但这太慢了,我需要不断更新图像(实时视频源),同时创建每个帧的 UI 填充,这当视频运行时,我的应用程序变得非常慢。所以我需要尽快将我的 ushort[] 转换为 BitmapImage

谢谢, 埃蒙

Is it possible to create a BitmapImage from a ushort array? and if so how?

At the minute I'm creating a Bitmap, converting it to a Bitmap array and displaying it, but this is too slow, I need to continuously update the image (live video feed), while each frame is being created the UI studders, this is making my app very slow when video is running. So I need to get my ushort[] into a BitmapImage as fast as possible

Thanks,
Eamonn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈下烟灰 2024-11-01 03:13:44

这里有一个如何通过 MemoryStream 获取 BitmapImage 的示例,这可能会有所帮助你

可以使用 BitConverter 将 ushort 转换为字节以输入到 MemoryStream

here you have an example of how to get a BitmapImage through a MemoryStream, this might help you

you can use BitConverter to convert ushorts to byte for input to MemoryStream

安稳善良 2024-11-01 03:13:44

假设您使用 0255 之间的值,您可以将其转换为字节数组,然后将其加载到 MemoryStream 中:

// Please note that with values higher than 255 the program will throw an exception
checked
{
    ushort[] values = { 200, 100, 30/*, 256*/ };
    var bytes = (from value in values
                 select (byte)value).ToArray();

    // Taken from: http://stackoverflow.com/questions/5346727/wpf-convert-memory-stream-to-bitmapimage
    using (var stream = new MemoryStream(data))
    {
        var bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.StreamSource = stream;
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        bitmap.EndInit();
        bitmap.Freeze();
    }
}

Assuming you're working with values between 0 and 255 you could cast it into an array of bytes and then load it into a MemoryStream:

// Please note that with values higher than 255 the program will throw an exception
checked
{
    ushort[] values = { 200, 100, 30/*, 256*/ };
    var bytes = (from value in values
                 select (byte)value).ToArray();

    // Taken from: http://stackoverflow.com/questions/5346727/wpf-convert-memory-stream-to-bitmapimage
    using (var stream = new MemoryStream(data))
    {
        var bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.StreamSource = stream;
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        bitmap.EndInit();
        bitmap.Freeze();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文