using SmartSEMControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HOZProject
{
    public partial class FormUCMain : Form
    {
        #region 全局变量定义
        //全局只有一个fatorySEM
        static FactoryHardware factorySEM = FactoryHardware.Instance;
        ISEMControl iSEM = factorySEM.ISEM;
        //电镜连接状态变量
        bool m_SEMConnectState = false;
        //自动状态变量
        bool m_AutoState = false;
        //电压状态变量
        bool m_HTState = false;
        //样品台状态变量
        bool m_StageStage = false;
        int m_ThreadTime = 1000;
        int m_ThreadCount = 5;
        string m_ImgFileSavePath = "D://tempImage.tif";
        string m_ImgAutoFileSavePath = "D://tempAutoImage.tif";
        short m_ImgWidth = 1024;
        short m_ImgHeight = 768;
        #endregion

        #region 构造
        public FormUCMain()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        } 
        #endregion

        #region 窗体事件
        private void FormUCMain_Load(object sender, EventArgs e)
        {
            ////获取初始状态(电镜连接状态、自动函数的状态、样品台状态)
            //Thread thread = new Thread(new ThreadStart(GetInitState));
            //thread.Start();
        }
        #endregion

        #region 获取初始状态
        public void GetInitState()
        {
            if (iSEM != null)
            {
                //电镜连接状态
                GetSEMConnectState();
                //自动函数的状态
                GetAutoFUNCState();
                //样品台状态
                GetStageState();
            }
        }
        #endregion

        #region 获取状态(电镜连接状态、自动函数的状态、样品台状态)
        public bool GetSEMConnectState()
        {
            bool result = GetAllState(StateType.SEMConnectState);
            if (result)
            {
                //已连接，设置按钮图像
                btnConnect.BackgroundImage = Properties.Resources.CloseImg;
                btnConnect.Invalidate();
            }
            else
            {
                btnConnect.BackgroundImage = Properties.Resources.ConnectImg;
                btnConnect.Invalidate();
            }
            //设置电镜连接状态：True
            m_SEMConnectState = result;
            return result;
        }
        public bool GetAutoFUNCState()
        {
            bool result = GetAllState(StateType.AutoFUNCState);
            m_AutoState = result;
            return result;
        }
        public bool GetStageState()
        {
            bool result = GetAllState(StateType.StageState);
            if (result)
            {
                btnConnect.BackgroundImage = Properties.Resources.CloseImg;
            }
            else
            {
                btnConnect.BackgroundImage = Properties.Resources.ConnectImg;
            }
            m_StageStage = result;
            return result;
        }
        public bool GetAllState(StateType stateType)
        {
            //线程循环计数器
            int threadCount = 0;
            bool bResult = false;
            float fResult = 0;
            while (true)
            {
                if (threadCount <= m_ThreadCount)
                {
                    switch (stateType)
                    {
                        case StateType.SEMConnectState:
                            bResult = iSEM.ConnectStatus();
                            if (bResult)
                            {
                                return bResult;
                            }
                            break;
                        case StateType.AutoFUNCState:
                            fResult = iSEM.GetAutoFunction();
                            if (fResult == 0)
                            {
                                bResult = true;
                                return bResult;
                            }
                            break;
                        case StateType.StageState:

                            fResult = iSEM.GetStageIs();
                            if (fResult == 0)
                            {
                                bResult = true;
                                return bResult;
                            }
                            break;
                    }
                }
                else
                {
                    return false;
                }
                threadCount++;
                Thread.Sleep(m_ThreadTime);
            }
        }
        /// <summary>
        /// 状态枚举
        /// </summary>
        public enum StateType
        {
            SEMConnectState = 0,
            AutoFUNCState = 1,
            StageState = 2
        }
        #endregion

        #region 关闭与最小化按钮事件
        private void pichide_MouseEnter(object sender, EventArgs e)
        {
            pichide.Image = Properties.Resources.Min_Blue;
        }

        private void pichide_MouseLeave(object sender, EventArgs e)
        {
            pichide.Image = Properties.Resources.Min_Gray;
        }

        private void picexit_MouseEnter(object sender, EventArgs e)
        {
            picexit.Image = Properties.Resources.exit_Red;
        }

        private void picexit_MouseLeave(object sender, EventArgs e)
        {
            picexit.Image = Properties.Resources.exit_Gray;
        }

        private void picexit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pichide_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        #endregion

        #region 拖动窗体
        private Point mouseOff;//鼠标移动位置变量
        private bool leftFlag;//标签是否为左键
        private void FormUCMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }
        private void FormUCMain_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }
        private void FormUCMain_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }
        #endregion

        #region SEM功能按钮读写事件
        #region 连接、关闭设备
        /// <summary>
        /// 初始化连接设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, EventArgs e)
        {
            //1.判断当前连接状态
            if (m_SEMConnectState)
            {
                if (iSEM != null)
                {
                    iSEM.Dispose();
                }
            }
        }

        #endregion

        #region 获取与设置缩放参数
        /// <summary>
        /// 获取缩放参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnenlargeGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetMagnification();
            if (float.IsNaN(ret))
            {
                txtenlarge.Enabled = false;
                btnenlargeSet.Enabled = false;
            }
            else
            {
                txtenlarge.Text = ret.ToString();
                btnenlargeSet.Enabled = true;
            }
        }
        /// <summary>
        /// 获取缩放参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnenlargeSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtenlarge.Text, out set))
            {
                iSEM.SetMagnification(set);
            }
        }
        #endregion

        #region 获取与设置亮度参数
        private void btnBrightnessGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetBrightness();
            if (float.IsNaN(ret))
            {
                txtBrightness.Enabled = false;
                btnBrightnessSet.Enabled = false;
            }
            else
            {
                txtBrightness.Text = ret.ToString();
                btnBrightnessSet.Enabled = true;
            }
        }

        private void btnBrightnessSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtBrightness.Text, out set))
            {
                iSEM.SetBrightness(set);
            }
        }
        #endregion

        #region 获取与设置焦距参数
        private void btnWDGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetWorkingDistance();
            if (float.IsNaN(ret))
            {
                txtWD.Enabled = false;
                btnWDSet.Enabled = false;
            }
            else
            {
                txtWD.Text = ret.ToString();
                btnWDSet.Enabled = true;
            }
        }

        private void btnWDSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtWD.Text, out set))
            {
                iSEM.SetWorkingDistance(set);
            }
        }
        #endregion

        #region 获取与设置对比度参数
        private void btnContrastGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetContrast();
            if (float.IsNaN(ret))
            {
                txtContrast.Enabled = false;
                btnContrastSet.Enabled = false;
            }
            else
            {
                txtContrast.Text = ret.ToString();
                btnContrastSet.Enabled = true;
            }
        }

        private void btnContrastSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtContrast.Text, out set))
            {
                iSEM.SetContrast(set);
            }
        }
        #endregion

        #region 获取与设置倾斜矫正参数
        private void btnTiltAngleGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetTiltAngle();
            if (float.IsNaN(ret))
            {
                txtTiltAngle.Enabled = false;
                btnTiltAngleSet.Enabled = false;
            }
            else
            {
                txtTiltAngle.Text = ret.ToString();
                btnTiltAngleSet.Enabled = true;
            }
        }

        private void btnTiltAngleSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtTiltAngle.Text, out set))
            {
                iSEM.SetTiltAngle(set);
            }
        }
        #endregion

        #region 获取与设置旋转角度参数
        private void btnScanRotationGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetScanRotation();
            if (float.IsNaN(ret))
            {
                txtScanRotation.Enabled = false;
                btnScanRotationSet.Enabled = false;
            }
            else
            {
                txtScanRotation.Text = ret.ToString();
                btnScanRotationSet.Enabled = true;
            }
        }

        private void btnScanRotationSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtScanRotation.Text, out set))
            {

                iSEM.SetScanRotation(set);
            }
        }
        #endregion

        #region 获取与设置像散Y参数
        private void btnAstigmatismXGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetAstigmatismX();
            if (float.IsNaN(ret))
            {
                txtAstigmatismX.Enabled = false;
                btnAstigmatismXSet.Enabled = false;
            }
            else
            {
                txtAstigmatismX.Text = ret.ToString();
                btnAstigmatismXSet.Enabled = true;
            }
        }

        private void btnAstigmatismXSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtAstigmatismX.Text, out set))
            {
                iSEM.SetAstigmatismX(set);
            }
        }
        #endregion

        #region 获取与设置像散Y参数
        private void btnAstigmatismYGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetAstigmatismY();
            if (float.IsNaN(ret))
            {
                txtAstigmatismY.Enabled = false;
                btnAstigmatismYSet.Enabled = false;
            }
            else
            {
                txtAstigmatismY.Text = ret.ToString();
                btnAstigmatismYSet.Enabled = true;
            }
        }

        private void btnAstigmatismYSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtAstigmatismY.Text, out set))
            {
                iSEM.SetAstigmatismY(set);
            }
        }
        #endregion

        #region 获取像素尺寸
        private void btnPixelSizeGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetPixelSize();
            if (float.IsNaN(ret))
            {
                txtPixelSize.Text = "NaN";
            }
            else
            {
                txtPixelSize.Text = ret.ToString();
            }
        }
        #endregion

        #region 切换电压状态
        private void pbHT_Click(object sender, EventArgs e)
        {
            bool result = true;
            if (!m_HTState)
            {
                //打开电压
                result = iSEM.CmdOpenVoltage();
                if (result)
                {
                    m_HTState = true;
                    pbHT.Image = Properties.Resources.OFF;
                    pbHT.Invalidate();
                }
            }
            else
            {
                //关闭电压
                result = iSEM.CmdCloseVoltage();
                if (result)
                {
                    m_HTState = false;
                    pbHT.Image = Properties.Resources.ON;
                    pbHT.Invalidate();
                }
            }
        }
        #endregion

        #region 获取样品台六轴位置信息
        private void btnGetStagePosition_Click(object sender, EventArgs e)
        {
            if (iSEM == null)
            {
                return;
            }
            try
            {
                float[] pt = iSEM.GetStagePosition();
                if (!float.IsNaN(pt[0]))
                {
                    txtStageX.Text = pt[0].ToString();
                }
                if (!float.IsNaN(pt[1]))
                {
                    txtStageY.Text = pt[1].ToString();
                }
                if (!float.IsNaN(pt[2]))
                {
                    txtStageZ.Text = pt[2].ToString();
                }
                if (!float.IsNaN(pt[3]))
                {
                    txtStageT.Text = pt[3].ToString();
                }
                if (!float.IsNaN(pt[4]))
                {
                    txtStageR.Text = pt[4].ToString();
                }
                if (!float.IsNaN(pt[5]))
                {
                    txtStageM.Text = pt[5].ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取与设置样品台X轴参数
        private void btnStageGetX_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtX();
            if (float.IsNaN(ret))
            {
                txtStageX.Text = "NaN";
            }
            else
            {
                txtStageX.Text = ret.ToString();
            }
        }

        private void btnStageSetX_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageX.Text, out set))
            {
                iSEM.SetStageGotoX(set);
            }
        }
        #endregion

        #region 获取与设置样品台T轴参数
        private void btnStageGetT_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtT();
            if (float.IsNaN(ret))
            {
                txtStageT.Text = "NaN";
            }
            else
            {
                txtStageT.Text = ret.ToString();
            }
        }

        private void btnStageSetT_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageT.Text, out set))
            {
                iSEM.SetStageGotoT(set);
            }
        }
        #endregion

        #region 获取与设置样品台Y轴参数
        private void btnStageGetY_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtY();
            if (float.IsNaN(ret))
            {
                txtStageY.Text = "NaN";
            }
            else
            {
                txtStageY.Text = ret.ToString();
            }
        }

        private void btnStageSetY_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageY.Text, out set))
            {
                iSEM.SetStageGotoY(set);
            }
        }
        #endregion

        #region 获取与设置样品台R轴参数
        private void btnStageGetR_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtR();
            if (float.IsNaN(ret))
            {
                txtStageR.Text = "NaN";
            }
            else
            {
                txtStageR.Text = ret.ToString();
            }
        }

        private void btnStageSetR_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageR.Text, out set))
            {
                iSEM.SetStageGotoR(set);
            }
        }
        #endregion

        #region 获取与设置样品台Z轴参数
        private void btnStageGetZ_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtZ();
            if (float.IsNaN(ret))
            {
                txtStageZ.Text = "NaN";
            }
            else
            {
                txtStageZ.Text = ret.ToString();
            }
        }

        private void btnStageSetZ_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageZ.Text, out set))
            {
                iSEM.SetStageGotoZ(set);
            }
        }
        #endregion

        #region 获取与设置样品台M轴参数
        private void btnStageGetM_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtM();
            if (float.IsNaN(ret))
            {
                txtStageM.Text = "NaN";
            }
            else
            {
                txtStageM.Text = ret.ToString();
            }
        }

        private void btnStageSetM_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageM.Text, out set))
            {
                iSEM.SetStageGotoM(set);
            }
        }
        #endregion

        #region 移动当前位置至SEM中心
        private void btnMoveSEMCenter_Click(object sender, EventArgs e)
        {
            if (iSEM == null)
            {
                return;
            }
            //设置当前样品台状态
            m_StageStage = false;
            float setX = 0;
            float setY = 0;
            bool GotoResult = false;
            //判断X、Y轴信息
            if (float.TryParse(txtMoveStageX.Text, out setX) && float.TryParse(txtMoveStageY.Text, out setY))
            {
                GotoResult = iSEM.MoveStageXY(setX, setY);
            }
        }
        #endregion

        #region 停止
        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            if (iSEM != null)
            {
                //停止
                iSEM.CmdStageAbort();
            }
        }
        #endregion
        #endregion
        
        #region 自动函数处理
        FormShowImage fShowImage = null;
        #region 获取图像
        /// <summary>
        /// 获取图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetImage_Click(object sender, EventArgs e)
        {
            if (iSEM == null)
            {
                return;
            }
            fShowImage = new FormShowImage();
            //获取当前图像以及当前电镜参数
            //放大倍数
            float f_Mag = iSEM.GetMagnification();
            //焦距
            float f_WD = iSEM.GetWorkingDistance();
            bool imageResult = iSEM.GrabImage(m_ImgFileSavePath, 0, 0, m_ImgWidth, m_ImgHeight, 0);
            if (imageResult)
            {
                FileStream fileStream = new FileStream(m_ImgFileSavePath, FileMode.Open, FileAccess.Read);
                fShowImage.Width = fShowImage.Width / 2;
                //设置当前图像与参数
                fShowImage.pbImage.Image = Image.FromStream(fileStream);
                fileStream.Close();
                fileStream.Dispose();
                fShowImage.Mag = "放大倍数：" + f_Mag;
                fShowImage.Content = "焦距：" + f_WD;
                fShowImage.SetParaInfo();
                fShowImage.Show();
            }
        }
        #endregion

        #region 自动对焦
        private void btnAutoWD_Click(object sender, EventArgs e)
        {
            if (iSEM == null)
            {
                return;
            }
            fShowImage = new FormShowImage();
            //获取图像尺寸
            int[] imageSize = iSEM.GetImageStore();
            m_ImgWidth = Convert.ToInt16(imageSize[0]);
            m_ImgHeight = Convert.ToInt16(imageSize[1]);
            //放大倍数
            float f_Mag = iSEM.GetMagnification();
            //焦距
            float f_WD = iSEM.GetWorkingDistance();
            //获取当前图像以及当前电镜参数
            bool imageResult = iSEM.GrabImage(m_ImgFileSavePath, 0, 0, m_ImgWidth, m_ImgHeight, 0);
            if (imageResult)
            {
                FileStream fileStream = new FileStream(m_ImgFileSavePath, FileMode.Open, FileAccess.Read);
                fShowImage.pbImage.Image = Image.FromStream(fileStream);
                fileStream.Close();
                fileStream.Dispose();
                //设置当前图像与参数
                fShowImage.Mag = "放大倍数：" + f_Mag;
                fShowImage.Content = "焦距：" + f_WD;
                fShowImage.SetParaInfo();
                fShowImage.Show();
                Boolean result = iSEM.CmdAutoFocusCoarse();
                if(result)
                {
                    Thread threadImg = new Thread(new ThreadStart(GetAutoWDImage));
                    threadImg.Start();
                }
            }
        }
        /// <summary>
        /// 获取自动状态后
        /// </summary>
        private void GetAutoWDImage()
        {
            if (fShowImage != null)
            {
                //获取当前自动状态
                bool result = GetAllState(StateType.AutoFUNCState);
                if (result)
                { 
                    //放大倍数
                    float f_Mag = iSEM.GetMagnification();
                    //焦距
                    float f_WD = iSEM.GetWorkingDistance();
                    //获取自动对焦后图像以及电镜参数
                    bool imageResult = iSEM.GrabImage(m_ImgAutoFileSavePath, 0, 0, m_ImgWidth, m_ImgHeight, 0);
                    FileStream fileStream = new FileStream(m_ImgFileSavePath, FileMode.Open, FileAccess.Read);
                    //自动后设置图像与参数
                    fShowImage.pbAutoImage.Image = Image.FromStream(fileStream);
                    fileStream.Close();
                    fileStream.Dispose();
                    fShowImage.AutoMag = "放大倍数：" + f_Mag;
                    fShowImage.AutoContent = "焦距：" + f_WD;
                    fShowImage.SetAutoParaInfo();
                    fShowImage.Invalidate();
                }
            }
        }
        #endregion

        #region 自动亮度
        private void btnAutoBrightness_Click(object sender, EventArgs e)
        {
            if (iSEM == null)
            {
                return;
            }
            fShowImage = new FormShowImage();
            //获取图像尺寸
            int[] imageSize = iSEM.GetImageStore();
            m_ImgWidth = Convert.ToInt16(imageSize[0]);
            m_ImgHeight = Convert.ToInt16(imageSize[1]);
            //放大倍数
            float f_Mag = iSEM.GetMagnification();
            //亮度
            float f_Brightness = iSEM.GetBrightness();
            //对比度
            float f_Contrast = iSEM.GetContrast();
            //获取当前图像以及当前电镜参数
            bool imageResult = iSEM.GrabImage(m_ImgFileSavePath, 0, 0, m_ImgWidth, m_ImgHeight, 0);
            if (imageResult)
            {
                FileStream fileStream = new FileStream(m_ImgFileSavePath, FileMode.Open, FileAccess.Read);
                fShowImage.pbImage.Image = Image.FromStream(fileStream);
                fileStream.Close();
                fileStream.Dispose();
                fShowImage.Mag = "放大倍数：" + f_Mag;
                fShowImage.Content = "亮度："+f_Brightness+"对比度:"+ f_Contrast;
                fShowImage.SetParaInfo();
                fShowImage.Show();
                float result = iSEM.GetAutoVideo();
                if (result== 0)
                {
                    Thread threadImg = new Thread(new ThreadStart(GetAutoBrightness));
                    threadImg.Start();
                }
            }
        }
        /// <summary>
        /// 获取自动状态后
        /// </summary>
        private void GetAutoBrightness()
        {
            if (fShowImage != null)
            {
                //获取当前自动状态
                bool result = GetAllState(StateType.AutoFUNCState);
                if (result)
                {
                    //放大倍数
                    float f_Mag = iSEM.GetMagnification();
                    //亮度
                    float f_Brightness = iSEM.GetBrightness();
                    //对比度
                    float f_Contrast = iSEM.GetContrast();
                    //获取自动对焦后图像以及电镜参数
                    bool imageResult = iSEM.GrabImage(m_ImgAutoFileSavePath, 0, 0, m_ImgWidth, m_ImgHeight, 0);
                    FileStream fileStream = new FileStream(m_ImgFileSavePath, FileMode.Open, FileAccess.Read);
                    fShowImage.pbAutoImage.Image = Image.FromStream(fileStream);
                    fileStream.Close();
                    fileStream.Dispose();
                    fShowImage.AutoMag = "放大倍数：" + f_Mag;
                    fShowImage.AutoContent = "亮度：" + f_Brightness + "对比度:" + f_Contrast;
                    fShowImage.SetAutoParaInfo();
                    fShowImage.Invalidate();
                }
            }
        }
        #endregion

        #region 自动消像散
        private void btnAutoAstigmatism_Click(object sender, EventArgs e)
        {
            if (iSEM == null)
            {
                return;
            }
            fShowImage = new FormShowImage();
            //获取图像尺寸
            int[] imageSize = iSEM.GetImageStore();
            m_ImgWidth = Convert.ToInt16(imageSize[0]);
            m_ImgHeight = Convert.ToInt16(imageSize[1]);
            //放大倍数
            float f_Mag = iSEM.GetMagnification();
            //消像散X
            float f_AstigmatismX = iSEM.GetAstigmatismX();
            //消像散Y
            float f_AstigmatismY = iSEM.GetAstigmatismY();
            //获取当前图像以及当前电镜参数
            bool imageResult = iSEM.GrabImage(m_ImgFileSavePath, 0, 0, m_ImgWidth, m_ImgHeight, 0);
            if (imageResult)
            {
                FileStream fileStream = new FileStream(m_ImgFileSavePath, FileMode.Open, FileAccess.Read);
                fShowImage.pbImage.Image = Image.FromStream(fileStream);
                fShowImage.Mag = "放大倍数：" + f_Mag;
                fShowImage.Content = "消像散X：" + f_AstigmatismX + "消像散Y:" + f_AstigmatismY;
                fShowImage.SetParaInfo();
                fShowImage.Show();
                fileStream.Close();
                fileStream.Dispose();
                float result = iSEM.GetAutoVideo();
                if (result == 0)
                {
                    Thread threadImg = new Thread(new ThreadStart(GetAutoAstigmatism));
                    threadImg.Start();
                }
            }
        }
        /// <summary>
        /// 获取自动状态后
        /// </summary>
        private void GetAutoAstigmatism()
        {
            if (fShowImage != null)
            {
                //获取当前自动状态
                bool result = GetAllState(StateType.AutoFUNCState);
                if (result)
                {
                    //放大倍数
                    float f_Mag = iSEM.GetMagnification();
                    //消像散X
                    float f_AstigmatismX = iSEM.GetAstigmatismX();
                    //消像散Y
                    float f_AstigmatismY = iSEM.GetAstigmatismY();
                    //获取自动对焦后图像以及电镜参数
                    bool imageResult = iSEM.GrabImage(m_ImgAutoFileSavePath, 0, 0, m_ImgWidth, m_ImgHeight, 0);
                    FileStream fileStream = new FileStream(m_ImgFileSavePath, FileMode.Open, FileAccess.Read);
                    fShowImage.pbAutoImage.Image = Image.FromStream(fileStream);
                    fShowImage.AutoMag = "放大倍数：" + f_Mag;
                    fShowImage.AutoContent = "消像散X：" + f_AstigmatismX + "消像散Y:" + f_AstigmatismY;
                    fShowImage.SetAutoParaInfo();
                    fShowImage.Invalidate();
                    fileStream.Close();
                    fileStream.Dispose();
                }
            }
        }
        #endregion

        #endregion

        private void btnContrast_Click(object sender, EventArgs e)
        {
            if (iSEM == null)
            {
                return;
            }
        }
    }
}
