【问题标题】:Rendering out camera images to a WPF Image control将相机图像渲染到 WPF 图像控件
【发布时间】:2011-09-16 15:38:42
【问题描述】:

我有一个 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);

我正在尝试以 1 秒的速度将这些位图放入 Image 控件中。我怎样才能让Bitmap 出现在Image 控件中,并在我完成后立即处理它们以留下最小的内存占用以成为一个优秀的小程序员:)?

【问题讨论】:

    标签: c# wpf camera gdi+


    【解决方案1】:

    我们这样做的方式(对我来说,它在不加载 CPU 的情况下以 200fps 的速度工作(大约 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");
        }
    

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多