//public void CaputreAndSave(string fn, string fType)
        //{
        //    var img = this.CaptureRaw();
        //    this.SaveImage(img, fn, fType);

        //}



        //public void Capture(int intDvcIdx)
        //{
        //    try
        //    {
        //        // 顯示裝置名稱
        //        Console.WriteLine("Device=" + StDevice[intDvcIdx].GetIStDeviceInfo().DisplayName);

        //        // 取得影像資料夾的路徑
        //        string fileNameHeader = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
        //        fileNameHeader += @"\" + StDevice[intDvcIdx].GetIStDeviceInfo().DisplayName + @"\" + DateTime.Now.ToString("yyyyMMdd_HHmmss");
        //        imgFileNameList.Add(fileNameHeader);

        //        object[] param = { intDvcIdx };
        //        StDataStream[intDvcIdx].RegisterCallbackMethod(OnCallback, param);

        //        // 主機端獲取影像
        //        StDataStream[intDvcIdx].StartAcquisition(1);

        //        // 開始由相機取得影像
        //        StDevice[intDvcIdx].AcquisitionStart();

        //        //// 循環取得資料並檢查狀態
        //        //// 持續執行取得影像直到足夠的幀數
        //        ////while (dataStream.IsGrabbing)
        //        //{
        //        //    // 逾時超過5000ms後,回收儲存影像資料的暫存區
        //        //    // 使用 'using' 語法可在不需使用時,自動管理暫存區重新排隊操作
        //        //    using (CStStreamBuffer streamBuffer = dataStream[intDvcIdx].RetrieveBuffer(5000))
        //        //    {
        //        //        // 檢查取得的資料是否包含影像資料
        //        //        if (streamBuffer.GetIStStreamBufferInfo().IsImagePresent)
        //        //        {
        //        //            // 若是,建立IStImage物件已進行進一步的影像處理
        //        //            //IStImage[] stImage=new IStImage[1];
        //        //            ImageList.Add(streamBuffer.GetIStImage());

        //        //            {
        //        //                // JPEG file extension.
        //        //                string imageFileName = fileNameHeader + ".jpg";

        //        //                // Save the image file in JPEG format.
        //        //                stillImageFiler.Quality = 75;
        //        //                Console.Write(Environment.NewLine + "Saving " + imageFileName + "... ");
        //        //                stillImageFiler.Save(ImageList[0], eStStillImageFileFormat.JPEG, imgFileNameList[0]);
        //        //                Console.Write("done" + Environment.NewLine);
        //        //                ImageList.RemoveAt(0);
        //        //                imgFileNameList.RemoveAt(0);
        //        //            }

        //        //            //// 顯示接收影像的詳細資訊
        //        //            //Byte[] imageData = stImage.GetByteArray();
        //        //            //Console.Write("BlockId=" + streamBuffer.GetIStStreamBufferInfo().FrameID);
        //        //            //Console.Write(" Size:" + stImage.ImageWidth + " x " + stImage.ImageHeight);
        //        //            //Console.Write(" First byte =" + imageData[0] + Environment.NewLine);


        //        //            //var width = (int)stImage.ImageWidth;
        //        //            //var height = (int)stImage.ImageHeight;
        //        //            //myimg = new Bitmap(width, height);

        //        //            ////TODO: https://stackoverflow.com/questions/3474434/set-individual-pixels-in-net-format16bppgrayscale-image

        //        //            //for (var idx = 0; idx < imageData.Length; idx++)
        //        //            //{
        //        //            //    var val = imageData[idx];
        //        //            //    var color = Color.FromArgb(val, val, val);
        //        //            //    myimg.SetPixel(idx % width, idx / width, color);
        //        //            //}
        //        //        }
        //        //        else
        //        //        {
        //        //            // 如果取得的資料不含影像資料
        //        //            Console.WriteLine("Image data does not exist.");
        //        //        }
        //        //    }
        //        //}
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //        // 停止相機取像
        //        StDevice[intDvcIdx].AcquisitionStop();
        //        // 停止主機取像
        //        StDataStream[intDvcIdx].StopAcquisition();
        //    }
        //}

        // Method for handling callback action
        public void OnCallback(IStCallbackParamBase paramBase, object[] param)
        {
            int DeviceIdx = (int)param[0];

            // Check callback type. Only NewBuffer event is handled in here
            if (paramBase.CallbackType == eStCallbackType.TL_DataStreamNewBuffer)
            {
                // In case of receiving a NewBuffer events:
                // Convert received callback parameter into IStCallbackParamGenTLEventNewBuffer for acquiring additional information.
                IStCallbackParamGenTLEventNewBuffer callbackParam = paramBase as IStCallbackParamGenTLEventNewBuffer;

                if (callbackParam != null)
                {
                    try
                    {
                        // Get the IStDataStream interface object from the received callback parameter.
                        IStDataStream dataStream = callbackParam.GetIStDataStream();

                        // Retrieve the buffer of image data for that callback indicated there is a buffer received.
                        using (CStStreamBuffer streamBuffer = dataStream.RetrieveBuffer(0))
                        {
                            // Check if the acquired data contains image data.
                            if (streamBuffer.GetIStStreamBufferInfo().IsImagePresent)
                            {
                                // If yes, we create a IStImage object for further image handling.
                                IStImage stImage = streamBuffer.GetIStImage();
                                //ImageList.Add(stImage);
                                this.stImage = stImage;
                            }
                            else
                            {
                                // If the acquired data contains no image data.
                                Console.WriteLine("Image data does not exist.");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // If any exception occurred, display the description of the error here.
                        throw ex;
                    }
                }
            }
        }
        // Method for handling callback action
        static void OnCallback(IStCallbackParamBase paramBase, object[] param)
        {
            // Check callback type. Only NewBuffer event is handled in here
            if (paramBase.CallbackType == eStCallbackType.TL_DataStreamNewBuffer)
            {
                // In case of receiving a NewBuffer events:
                // Convert received callback parameter into IStCallbackParamGenTLEventNewBuffer for acquiring additional information.
                IStCallbackParamGenTLEventNewBuffer callbackParam = paramBase as IStCallbackParamGenTLEventNewBuffer;

                if (callbackParam != null)
                {
                    try
                    {
                        // Get the IStDataStream interface object from the received callback parameter.
                        IStDataStream dataStream = callbackParam.GetIStDataStream();

                        // Retrieve the buffer of image data for that callback indicated there is a buffer received.
                        using (CStStreamBuffer streamBuffer = dataStream.RetrieveBuffer(0))
                        {
                            // Check if the acquired data contains image data.
                            if (streamBuffer.GetIStStreamBufferInfo().IsImagePresent)
                            {
                                // If yes, we create a IStImage object for further image handling.
                                IStImage stImage = streamBuffer.GetIStImage();
#if ENABLED_ST_GUI
                                CStImageDisplayWnd wnd = (CStImageDisplayWnd)param[0];

                                // Check if display window is visible.
                                if (!wnd.IsVisible)
                                {
                                    // Set the position and size of the window.
                                    wnd.SetPosition(0, 0, (int)stImage.ImageWidth, (int)stImage.ImageHeight);

                                    // Create a new thread to display the window.
                                    wnd.Show(eStWindowMode.ModalessOnNewThread);
                                }

                                // Register the image to be displayed.
                                // This will have a copy of the image data and original buffer can be released if necessary.
                                wnd.RegisterIStImage(stImage);
#else
                                // Display the information of the acquired image data.
                                Byte[] imageData = stImage.GetByteArray();
                                Console.Write("BlockId=" + streamBuffer.GetIStStreamBufferInfo().FrameID);
                                Console.Write(" Size:" + stImage.ImageWidth + " x " + stImage.ImageHeight);
                                Console.Write(" First byte =" + imageData[0] + Environment.NewLine);
#endif
                            }
                            else
                            {
                                // If the acquired data contains no image data.
                                Console.WriteLine("Image data does not exist.");
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        // If any exception occurred, display the description of the error here.
                        Console.Error.WriteLine("An exception occurred. \r\n" + e.Message);
                    }
                }
            }
        }