Skip to content

File CustomImageControl.xaml.cs

File List > PGRControls > CustomImageControl.xaml.cs

Go to the documentation of this file

//=============================================================================
// Copyright (c) 2026 FLIR Integrated Imaging Solutions, Inc. All Rights Reserved.
//
// This software is the confidential and proprietary information of FLIR
// Integrated Imaging Solutions, Inc. ("Confidential Information"). You
// shall not disclose such Confidential Information and shall use it only in
// accordance with the terms of the license agreement you entered into
// with FLIR Integrated Imaging Solutions, Inc. (FLIR)
//
// FLIR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
// SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE, OR NON-INFRINGEMENT. FLIR SHALL NOT BE LIABLE FOR ANY DAMAGES
// SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
// THIS SOFTWARE OR ITS DERIVATIVES.
//=============================================================================

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Diagnostics;
using System.Windows.Controls.Primitives;
using SpinnakerNET.GenApi;

namespace SpinnakerNET.GUI
{
    namespace WPFControls
    {


        public partial class CustomImageControl : BaseClass
        {
            private readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(CustomImageControl));
            private bool _disposed = false;

            SpinnakerNET.GUI.WPFControls.ROIControl _control;
            // StackPanel _sp;
            // bool _nameLabelBoldness = false;
            TextBlock txt_position;
            TextBlock txt_dimension;
            StackPanel sp_text;
            Button _maxRoi;
            Button _centerRoi;

#region PROPERTIES
#endregion

            public CustomImageControl()
            {
                InitializeComponent();
            }

            ~CustomImageControl()
            {
                Disconnect();
            }

            internal CustomImageControl(CustomImageControl originalControl)
            {
                InitializeComponent();

                // Common Properties
                this.SetMapper(originalControl.GetMapper());
                this.Connect(
                    originalControl.NodeMap, originalControl.GetNodeToControl(), originalControl.GetControlNameLabel());
                this.SetNameLabelBoldness(originalControl.NameLabelBoldness);
                this.SetVisibility(originalControl.Visibility);
                this.SetControlID(originalControl.ControlID);
                this.SetMin(originalControl.GetMin());
                this.SetMax(originalControl.GetMax());
                this.SetControlNameLabel(originalControl.ControlNameLabel);
                this.SetNameLabelVisibility(originalControl.GetNameLabelVisibility());
                this.SetNameLabelBoldness(originalControl.GetNameLabelBoldness());
                try
                {
                    this.SetToolTip(originalControl.GetToolTip());
                }
                catch (System.Exception /*ex*/)
                {
                }
                this.SetMaximumControlSize(originalControl.GetMaximumControlSize());
                this.SetMinimumControlSize(originalControl.GetMinimumControlSize());
                this.SetControlHeight(originalControl.GetControlHeight());
                this.SetControlWidth(originalControl.GetControlWidth());
                this.SetRefreshTime(originalControl.GetRefreshTime());
                this.SetMargin(originalControl.GetMargin());
            }

            protected override void Dispose(bool disposing)
            {
                if (!_disposed)
                {
                    if (disposing)
                    {
                    }

                    // Consider GenICam callbacks as unmanaged resource
                    Disconnect();
                    _disposed = true;
                }
                // Call Dispose in the base class.
                base.Dispose(disposing);
            }

            public override void Connect(INodeMap map, string nodename, string label = "")
            {
                if (map == null)
                {
                    throw new NullReferenceException("Nodemap object is null");
                }

                // Acquire device serial number
                if (string.IsNullOrWhiteSpace(_serialNumber))
                {
                    _serialNumber = RetrieveSerialNumber(map);
                }

                base._nodeMap = map;

                _control = new SpinnakerNET.GUI.WPFControls.ROIControl(map);

                _control.OnUpdateImageTextInformation +=
                    new ROIControl.UpdateImageTextInformation(OnUpdateImageTextInformation);
                _control.OnUpdateMaxImageSizeButton +=
                    new ROIControl.UpdateMaxImageSizeButton(OnUpdateMaxImageSizeButton);
                _control.OnUpdateCenterROIButton += new ROIControl.UpdateCenterROIButton(OnUpdateCenterROIButton);

                Grid.SetRow(_control, 1);
                rootLayout.Children.Add(_control);

                // Use a border to indicate the boundaries of the ROI display area
                Border roiRowBorder =
                    new Border(){BorderThickness = new Thickness(){Bottom = 0.5, Left = 0.5, Right = 0.5, Top = 0.5},
                                 BorderBrush = new SolidColorBrush(Colors.Black)};
                rootLayout.Children.Add(roiRowBorder);
                roiRowBorder.SetValue(Grid.RowProperty, 1);

                UniformGrid uniformGrid = new UniformGrid();
                uniformGrid.Columns = 2;
                uniformGrid.Rows = 1;
                uniformGrid.Margin = new System.Windows.Thickness(3);

                _centerRoi = new Button();
                _centerRoi.Content = "Center ROI";
                _centerRoi.ToolTip = "Move ROI to center of sensor";
                _centerRoi.HorizontalAlignment = HorizontalAlignment.Stretch;
                _centerRoi.Margin = new Thickness(10, 3, 10, 3);
                _centerRoi.Click += new RoutedEventHandler(centerRoi_Click);

                _maxRoi = new Button();
                _maxRoi.Content = "Max Image Size";
                _maxRoi.ToolTip = "Set ROI to maximum sensor size";
                _maxRoi.HorizontalAlignment = HorizontalAlignment.Stretch;
                _maxRoi.Margin = new Thickness(10, 3, 10, 3);
                _maxRoi.Click += new RoutedEventHandler(maxRoi_Click);

                Grid.SetColumn(_centerRoi, 0);
                Grid.SetColumn(_maxRoi, 0);

                uniformGrid.Children.Add(_centerRoi);
                uniformGrid.Children.Add(_maxRoi);

                Grid.SetRow(uniformGrid, 2);

                rootLayout.LayoutUpdated += new EventHandler(LayoutUpdatedHandler);

                rootLayout.Children.Add(uniformGrid);

                txt_position = new TextBlock();
                txt_position.TextWrapping = TextWrapping.Wrap;
                txt_position.HorizontalAlignment = HorizontalAlignment.Center;
                txt_position.VerticalAlignment = VerticalAlignment.Center;
                txt_position.Background = null;
                txt_position.IsHitTestVisible = false;
                txt_position.Text = "Position";

                txt_dimension = new TextBlock();
                txt_dimension.TextWrapping = TextWrapping.Wrap;
                txt_dimension.HorizontalAlignment = HorizontalAlignment.Center;
                txt_dimension.VerticalAlignment = VerticalAlignment.Center;
                txt_dimension.Background = null;
                txt_dimension.IsHitTestVisible = false;
                txt_dimension.Text = "Dimension";

                sp_text = new StackPanel();
                sp_text.Orientation = Orientation.Vertical;
                sp_text.HorizontalAlignment = HorizontalAlignment.Center;
                sp_text.VerticalAlignment = VerticalAlignment.Center;
                sp_text.IsHitTestVisible = false;
                sp_text.Children.Add(txt_position);
                sp_text.Children.Add(txt_dimension);
                Grid.SetRow(sp_text, 1);

                rootLayout.Children.Add(sp_text);

                bool result = _control.Connect();

                if (!result)
                {
                    _featureAvailable = false;
                    log.Error(
                        string.Format("{0}There was a problem creating CustomImageControl.", FormattedSerialNumber));
                    throw new Exception("There was a problem creating CustomImageControl");
                }

                _featureAvailable = true;

                RegisterInterfaceEvents();

                _control.Margin = new System.Windows.Thickness(3);
                _control.Width = this.ActualWidth == 0 ? this.Width : this.ActualWidth;
            }

            void OnUpdateCenterROIButton(bool status)
            {
                this._centerRoi.IsEnabled = status;
            }

            void OnUpdateMaxImageSizeButton(bool status)
            {
                this._maxRoi.IsEnabled = status;
            }

            void OnUpdateImageTextInformation(string position, string dimension)
            {
                if (txt_position != null && txt_dimension != null)
                {
                    txt_position.Text = position;
                    txt_dimension.Text = dimension;
                }
            }

            public override void Disconnect()
            {
                try
                {
                    if (_control != null)
                    {
                        _control.UnInitializePage();
                        base.Disconnect();
                    }
                }
                catch (System.Exception /*ex*/)
                {
                }
            }

            public override void SetRefreshTime(int seconds)
            {
                this._refreshTime = seconds;

                // Start timer if an valid refresh time was provided
                if (this._refreshTime > 0)
                {
                    try
                    {
                        _timer = new System.Windows.Forms.Timer();
                        _timer.Interval = this._refreshTime;
                        _timer.Tick += new EventHandler(timer_Tick);
                        _timer.Start();
                    }
                    catch (System.Exception /*ex*/)
                    {
                    }
                }
            }

            public override void Refresh()
            {
                _control.Refresh();
            }

#region DEBUG_SLIDER
            // Sliders for testing purpose
            //_sp = new StackPanel();
            // SliderControl slider1 = new SliderControl();
            // slider1.Connect(map, "Width");
            // SliderControl slider2 = new SliderControl();
            // slider2.Connect(map, "Height");
            // SliderControl slider3 = new SliderControl();
            // slider3.Connect(map, "OffsetX");
            // SliderControl slider4 = new SliderControl();
            // slider4.Connect(map, "OffsetY");
            //_sp.Children.Add(slider1);
            //_sp.Children.Add(slider2);
            //_sp.Children.Add(slider3);
            //_sp.Children.Add(slider4);
            // Grid.SetRow(_sp, 2);
            // rootLayout.Children.Add(_sp);

// public void HideSliders()
//{
//    if (_sp != null)
//    {
//        _sp.Visibility = System.Windows.Visibility.Collapsed;
//    }
//}

// public void ShowSliders()
//{
//    if (_sp != null)
//    {
//        _sp.Visibility = System.Windows.Visibility.Visible;
//    }
//}
#endregion

            void maxRoi_Click(object sender, RoutedEventArgs e)
            {
                if (_control != null)
                {
                    _control.SetMaxROI();
                }
            }

            void centerRoi_Click(object sender, RoutedEventArgs e)
            {
                if (_control != null)
                {
                    _control.SetCenterROI();
                }
            }

            void timer_Tick(object sender, EventArgs e)
            {
                try
                {
                    Refresh();
                }
                catch (System.Exception /*ex*/)
                {
                    Debug.WriteLine("Problem retrieving new value from {0} node", _propertyToControl);
                }
            }

            public void SetCanvasMaximumHeight(int maxHeight)
            {
                if (!_featureAvailable || _control != null)
                {
                    _control.MaxViewboxHeight = maxHeight;
                }
                else
                {
                    throw new Exception("CustomImageControl not initialized.");
                }
            }

            public int GetCanvasMaximumHeight()
            {
                if (!_featureAvailable || _control != null)
                {
                    return _control.MaxViewboxHeight;
                }
                else
                {
                    throw new Exception("CustomImageControl not initialized.");
                }
            }

            void LayoutUpdatedHandler(object sender, EventArgs e)
            {
                // We need to give the roi display row a fixed size so it does not resize with the ROI control
                double controlHeight = GetControlHeight();
                double roiHeight = controlHeight - m_buttonRow.ActualHeight;
                if (controlHeight != 0 && !Double.IsNaN(controlHeight) && m_roiRow.Height.Value != roiHeight)
                {
                    m_roiRow.Height = new GridLength(roiHeight);
                }
                else if (base._nodeMap != null && this.ActualHeight > 0 && m_roiRow.Height.IsAuto)
                {
                    // In the case when user does not define a controlHeight, the roi row will be shrunk to fit
                    // the ROI control, so we need to scale it back up according to the BinningVertical node
                    var binningVertical = base._nodeMap.GetNode<Integer>("BinningVertical");
                    roiHeight = (this.ActualHeight - m_buttonRow.ActualHeight) * binningVertical;
                    m_roiRow.Height = new GridLength(roiHeight);
                }
            }
        }

    }
}