简体   繁体   English

通过ushort创建一个BitmapImage []

[英]Create a BitmapImage from ushort[]

Is it possible to create a BitmapImage from a ushort array? 是否可以从ushort数组创建BitmapImage? 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. 在我创建位图的那一刻,将其转换为位图数组并显示它,但这太慢了,我需要不断更新图像(实时视频提要),同时在创建每个帧的UI钉时,视频运行时,我的应用程序运行非常缓慢。 So I need to get my ushort[] into a BitmapImage as fast as possible 所以我需要尽快将ushort []放入BitmapImage中

Thanks, Eamonn 谢谢,Eamonn

here you have an example of how to get a BitmapImage through a MemoryStream, this might help you 这里有一个如何通过MemoryStream获取BitmapImage的示例,这可能对您有帮助

you can use BitConverter to convert ushorts to byte for input to MemoryStream 您可以使用BitConverter将Ushort转换为字节以输入到MemoryStream

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 : 假设您正在使用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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM