将相机图像渲染到 WPF Image 控件

发布于 2024-12-05 08:59:31 字数 1348 浏览 1 评论 0原文

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

 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秒。如何获取位图 显示在图像 控制和处置它们一旦我完成留下最小的内存占用,成为一名优秀的小程序员:)?

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);

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 :) ?

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

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

发布评论

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

评论(1

一袭水袖舞倾城 2024-12-12 08:59:31

这是我们的做法(对我来说,它以 200fps 运行而不加载 CPU(大约 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");
    }

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");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文