将相机图像渲染到WPF图像控件 [英] Rendering out camera images to a WPF Image control

查看:379
本文介绍了将相机图像渲染到WPF图像控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个uEye相机,我以1000ms的间隔拍摄图像的快照,我想在WPF 图像 像这样的控件

I have a uEye camera and I take snapshots of images at a 1000ms interval and I want to render them in a WPF Image Control like so

 Bitmap MyBitmap;

// get geometry of uEye image buffer

int width = 0, height = 0, bitspp = 0, pitch = 0, bytespp = 0;

long imagesize = 0;

m_uEye.InquireImageMem(m_pCurMem, GetImageID(m_pCurMem), ref width, ref height, ref bitspp, ref pitch);

bytespp = (bitspp + 1) / 8;

imagesize = width * height * bytespp; // image size in bytes

// bulit a system bitmap
MyBitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);

// fill the system bitmap with the image data from the uEye SDK buffer
BitmapData bd = MyBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
m_uEye.CopyImageMem(m_pCurMem, GetImageID(m_pCurMem), bd.Scan0);
MyBitmap.UnlockBits(bd);

我正在尝试将这些位图放入 Image 控件的比率为1第二。如何获取 位图 出现在 Image 控件,并在我完成处理后立即将它们丢弃,以使最小的内存占用量成为一个好的小程序员:)?

I am trying to put these bitmaps in to an Image control at the rate of 1 second. How can I get Bitmap to appear in the Image control and disposing them as soon as I'm done to leave minimal memory footprint to be a good little programmer :) ?

推荐答案

我们采用的方法(对我来说,它的工作速度为200fps,而无需加载CPU(大约5%)):

Here the way we do (for me it works at 200fps without loading CPU (about 5%)):

    private WriteableBitmap PrepareForRendering(VideoBuffer videoBuffer) {
        PixelFormat pixelFormat;
        if (videoBuffer.pixelFormat == PixFrmt.rgb24) {
            pixelFormat = PixelFormats.Rgb24;
        } else if (videoBuffer.pixelFormat == PixFrmt.bgra32) {
            pixelFormat = PixelFormats.Bgra32;
        } else if (videoBuffer.pixelFormat == PixFrmt.bgr24) {
            pixelFormat = PixelFormats.Bgr24;
        } else {
            throw new Exception("unsupported pixel format");
        }
        var bitmap = new WriteableBitmap(
            videoBuffer.width, videoBuffer.height,
            96, 96,
            pixelFormat, null
        );
        _imgVIew.Source = bitmap;
        return bitmap;
    }

    private void DrawFrame(WriteableBitmap bitmap, VideoBuffer videoBuffer, double averangeFps) {
        VerifyAccess();
        if (isPaused) {
            return;
        }

        bitmap.Lock();
        try {
            using (var ptr = videoBuffer.Lock()) {
                bitmap.WritePixels(
                    new Int32Rect(0, 0, videoBuffer.width, videoBuffer.height),
                    ptr.value, videoBuffer.size, videoBuffer.stride,
                    0, 0
                );
            }
        } finally {
            bitmap.Unlock();
        }
        fpsCaption.Text = averangeFps.ToString("F1");
    }

这篇关于将相机图像渲染到WPF图像控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆