I have a project which consists of two forms, MainForm
and CrossCorrPlotForm
. On CrossCorrPlotForm
, I have two charts
(CrossCorrExpChart
and CrossCorrRefChart
) which I enabled scroll wheel zooming on, using the code described here and here.
Everything was working perfectly fine, until I added another chart
(histChart
), on MainForm
, which is updated on each frame incoming from a camera (15-20 FPS), using a BackgroundWorker
to collect and plot the data from the images. Now, both my charts on CrossCorrPlotChart
are not zoomable anymore.
I presume this has something to do with the live-updating chart taking back the focus on each update. I tried adding histChart.Focus = false
in the code but to no avail, as it seems "Control.Focused is read-only".
Does anyone have an idea how to make my charts
zoomable again ?
Thanks
EDIT : Here is the code for the BackgroundWorker
that updates chartHist
:
private void OnFrameReceived(Frame frame)
{
bgw1.RunWorkerAsync(frame);
}
private void bgw1_DoWork(object s, DoWorkEventArgs e)
{
Frame frame = (Frame)e.Argument;
myBitmap = null;
try
{
frame.Fill(ref myBitmap);
mycamera.QueueFrame(frame);
SaveBitmap = myBitmap.Clone(cloneRect, myBitmap.PixelFormat);
BitmapToPrint = myBitmap.Clone(cloneRect, myBitmap.PixelFormat);
}
catch {}
}
private void bgw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (this.InvokeRequired)
{
BeginInvoke((Action)(() => bgw1_RunWorkerCompleted(sender, e)));
}
else
{
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
DataFromBitmap DataFromBitmapHist = new DataFromBitmap(SaveBitmap.Clone(cloneRect, SaveBitmap.PixelFormat));
PixelColorCount = DataFromBitmapHist.ColorCountOutput();
}).Start();
chartHist.Titles["Title2"].Visible = false;
chartHist.Series["Pixel count"].Points.Clear();
//Plotting the pixel counter, to detect saturation
for (int i = 0; i < PixelColorCount.Length; i++)
{
chartHist.Series["Pixel count"].Points.AddXY(i, PixelColorCount[i]);
}
//If there are saturated pixels : toggle a title on chartHist to warn the user
if (PixelColorCount.Last() > 1)
{
chartHist.Titles["Title1"].Visible = false;
chartHist.Titles["Title2"].Visible = true;
}
else
{
chartHist.Titles["Title1"].Visible = true;
chartHist.Titles["Title2"].Visible = false;
}
}
}
NOTES :
OnFrameReceived
is a function from the API of the camera, it contains code that fires when aFrame
is received from the camera.frame.Fill
puts the image contained in theFrame
object in aBitmap
.mycamera.QueueFrame
sends back theFrame
object to the camera, to receive a new image.- I had to use multiple threads, because using the UI thread too much resulted in blocking the reception from the camera.