Reputation: 9
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();
}
}
}
Upvotes: 0
Views: 52