将相机获取的位图绑定到WPF图像控制 - 滞后

时间:2018-05-29 09:11:20

标签: c# wpf mvvm bitmap

我一直在使用一些控制摄像机的API,每当我要求它们时,API都会为我提供生成位图的数据,代码是这样的:

public void ImageDisplay(IntPtr data, int rows, int cols, int channels, long timestamp)
{
    if(CameraNeeded)
    {
         CameraImage = new Bitmap(cols, rows, channels * cols, PixelFormat.FormatbppIndexed, data);
    }
}
//This method generates a new frame every [still undetermined but I guess around 100-200] ms.

然后我使用ValueConverter在我的View上显示这个位图,该值管理从Bitmap到BitmapImage的转换(用于WPF图像控件):

<!-- Camera -->
<Image Width="300"
       Height="300"
       Source="{Binding CameraImage, Converter={local:BitmapToBitmapImageConverter}}"
       RenderOptions.BitmapScalingMode="LowQuality"
/>
<!-- This is inside a Content Control so an instance is created only when needed -->

这是转换器:

public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
        Bitmap bitmap = (Bitmap)value;
        ColorPalette pal = bitmap.Palette;
        for (int i = 0; i <= 255; i++)
            pal.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
        bitmap.Palette = pal; 
//The camera needs to change the palette because it has weird colors

        MemoryStream ms = new MemoryStream();
        bitmap.Save(ms, ImageFormat.Bmp);
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        ms.Seek(0, SeekOrigin.Begin);
        image.StreamSource = ms;
        image.EndInit();

        return image;

    }

到目前为止,相机显示正确,质量良好,但是当我在不同的计算机上进行测试时,它会落后很多。 我想这与我如何在内存中创建/转换/保留位图有关。有什么建议吗?

0 个答案:

没有答案