Is there a more efficient way to display live camera images in a C# desktop application

I am trying to display the images from a LUCID camera in a C# desktop application. The camera comes with a SDK and the image acquisition takes between 20-35ms per image. The problem is that I have very few (2-5) FPS and i dont know how to increase them to about 10-15. When I run the program my CPU is at about 30%. This makes me think im doing something wrong. The resolution of the camera is also not enormous at around 2400x 1500px.

My question is, do you guys know a better way to display/render images in a C# Desktop application?

Thanks in advance!

First I made a Windows Forms Application in C# with a picturebox that I updated after fetching the current image (changing the intervall of the timer didnt change the FPS). I tried options like double buffering or only rerendering the picturebox instead of the whole form.

My current approach is using WPF, but the performance is equally slow.

This call right here returns the current camera image as a System.Drawing.Image

myCameraManager.getImageById(serialNumber);  

And here is my current WPF code:

MainWindow.xaml.cs:

using System;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Drawing;
using System.IO;
using System.Diagnostics;
using StrelenViewerWPF;

namespace CameraApp
{
    public partial class MainWindow : Window
    {
        private DispatcherTimer imageTimer;
        private DispatcherTimer fpsTimer;
        private CameraManager myCameraManager;

        private Stopwatch frameStopwatch;
        private int frameCount;
        private double fpsValue;

        // Stopwatches for performance measurement
        private Stopwatch fetchStopwatch;
        private Stopwatch conversionStopwatch;
        private Stopwatch uiUpdateStopwatch;

        public MainWindow()
        {
            InitializeComponent();
            myCameraManager = new CameraManager();
            InitializeCameraFeed();
        }

        private void InitializeCameraFeed()
        {
            // Initialize the DispatcherTimer for image updates
            imageTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(50) // 20 FPS
            };
            imageTimer.Tick += ImageTimer_Tick;
            imageTimer.Start();

            // Initialize the DispatcherTimer for FPS updates
            fpsTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(1) // Update FPS every second
            };
            fpsTimer.Tick += FpsTimer_Tick;
            fpsTimer.Start();

            // Initialize performance tracking
            frameStopwatch = new Stopwatch();
            frameStopwatch.Start();

            fetchStopwatch = new Stopwatch();
            conversionStopwatch = new Stopwatch();
            uiUpdateStopwatch = new Stopwatch();
        }

        private void ImageTimer_Tick(object sender, EventArgs e)
        {
            // Fetch images from the camera and update the UI
            UpdateImage(Camera1, myCameraManager.allCameras[0].serialNumber);
        }

        private void UpdateImage(System.Windows.Controls.Image imageControl, string serialNumber)
        {
            fetchStopwatch.Restart();
            var image = myCameraManager.getImageById(serialNumber);
            fetchStopwatch.Stop();

            if (image != null)
            {
                // Convert image to BitmapImage
                conversionStopwatch.Restart();
                var bitmapImage = BitmapToImageSource(image);
                conversionStopwatch.Stop();

                // Update UI
                Dispatcher.Invoke(() =>
                {
                    uiUpdateStopwatch.Restart();
                    imageControl.Source = bitmapImage; // Update UI on the UI thread
                    uiUpdateStopwatch.Stop();

                    // Increment frame count for FPS calculation
                    frameCount++;
                });
            }
        }

        private BitmapImage BitmapToImageSource(System.Drawing.Image image)
        {
            using (var memory = new MemoryStream())
            {
                image.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                memory.Position = 0;

                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();

                return bitmapImage;
            }
        }

        private void FpsTimer_Tick(object sender, EventArgs e)
        {
            // Calculate FPS
            double elapsedSeconds = frameStopwatch.Elapsed.TotalSeconds;
            fpsValue = frameCount / elapsedSeconds;

            // Reset for next interval
            frameCount = 0;
            frameStopwatch.Restart();

            // Update FPS display
            FpsLabel1.Text = $"{fpsValue:0.0} FPS";
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            imageTimer.Stop();
            fpsTimer.Stop();
        }
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật