0

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 a Frame is received from the camera.
  • frame.Fill puts the image contained in the Frame object in a Bitmap.
  • mycamera.QueueFrame sends back the Frame 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.
Trion
  • 115
  • 11
  • I guess we want to see the updating code, at least the important parts of it. – TaW May 28 '18 at 09:39
  • 1
    @TaW I didn't want to clutter the question with code, I didn't think it was really relevant here. I am adding it right away ! – Trion May 28 '18 at 09:40
  • _I didn't want to clutter the question_ Good plan :-) – TaW May 28 '18 at 09:42
  • _I had to use multiple threads, because using the UI thread too much resulted in blocking the reception from the camera._ Hm, that sounds a bit suspicious, as if the camera code also runs on the UI..? Which might explain how the zooming gets blocked.. - But I am really mostly guessing here. – TaW May 28 '18 at 10:29
  • @TaW To be honest I have not the slightest clue either. But you could be right. Getting the live chart to work correctly without crashing after a few seconds has been a real pain. The API I am using is quite bad I think (but I don't have much choice), and the manual doesn't give any hint about if `OnFrameReceived` runs on the UI Thread or not. My guess is that it does, as I had to use a `BackgroundWorker`. Anyway, I don't think it is relevant to my issue with the focus, as it worked before. Is there a way to override the ReadOnly property of Control.Focus ? – Trion May 28 '18 at 10:44
  • Um, __if__ it really is about focus you can set it by `Control.Focus()`. – TaW May 28 '18 at 10:50

0 Answers0