/// <summary>
        /// Init the window rect from process window and calculate the caprure rect
        /// </summary>
        /// <param name="procName">process name</param>
        /// <param name="Profil">active profile</param>
        public void GetGameWindowRect(string procName, ProfileData Profil)
        {
            Process proc;
            try
            {
                if (String.IsNullOrEmpty(procName))
                {
                    CRect = new Rectangle(0, 0, 0, 0);
                    return;
                }

                proc = Process.GetProcessesByName(procName)[0];
                if (proc == null)
                {
                    CRect = new Rectangle(0, 0, 0, 0);
                    return;
                }

                GameWindowRect = new User32.Rect();
                IntPtr error = User32.GetWindowRect(proc.MainWindowHandle, ref GameWindowRect);

                while (error == (IntPtr)0)
                {
                    error = User32.GetWindowRect(proc.MainWindowHandle, ref GameWindowRect);
                }


                SetCaptureRect(Profil);

            }
            catch
            {
                CRect = new Rectangle(0, 0, 0, 0);
            }
        }
        /// <summary>
        /// Controls to draw cursor or crosshair
        /// </summary>
        /// <param name="bmp">bitmap too draw</param>
        /// <param name="wRect">Caption rect to calculate cursor</param>
        /// <param name="Profil">setting prolfie</param>
        /// <param name="CursorPosition">cursor position</param>
        /// <param name="myBrush">draw brush</param>
        /// <returns>bitmap with drawings</returns>
        public static Bitmap DrawExtras(Bitmap bmp, Rectangle wRect, ProfileData Profil, Point CursorPosition, SolidBrush myBrush)
        {
            try
            {
                if (bmp == null)
                    return new Bitmap(wRect.Width, wRect.Height, PixelFormat.Format32bppArgb);

                Graphics g = System.Drawing.Graphics.FromImage(bmp);

                if (Profil.ShowCrosshair)
                {
                    GDIGraphicTools.DrawCrosshair(g, wRect.Width, wRect.Height);
                }

                if (Profil.ShowCursor)
                {
                    GDIGraphicTools.DrawCursor(g,
                                                wRect.X,
                                                wRect.Y,
                                                wRect.Width,
                                                wRect.Height,
                                                CursorPosition,
                                                Profil.CursorCorrection,
                                                Profil.CursorCorrectionAdjWidth,
                                                Profil.CursorCorrectionAdjHeight,
                                                myBrush,
                                                Profil.CursorSize);
                    
                } // cursor

                g.Dispose();

            }
            catch { }

            return bmp;
        }
        /// <summary>
        /// calculate the capture rect (with corrections or custom)
        /// </summary>
        /// <param name="Profil">active profile</param>
        public void SetCaptureRect(ProfileData Profil)
        {
            try
            {
                if (Profil.CustomWindow)
                {
                    CRect.Width = Profil.CustomWindowSize.width;
                    CRect.Height = Profil.CustomWindowSize.height;
                    CRect.X = Profil.CustomWindowSize.x;
                    CRect.Y = Profil.CustomWindowSize.y;
                }
                else
                {
                    CRect.Width = GameWindowRect.right - (GameWindowRect.left + Profil.BorderCorrection.left) - Profil.BorderCorrection.right;
                    CRect.Height = GameWindowRect.bottom - (GameWindowRect.top + Profil.BorderCorrection.top) - Profil.BorderCorrection.bottom;
                    CRect.X = GameWindowRect.left + Profil.BorderCorrection.left;
                    CRect.Y = GameWindowRect.top + Profil.BorderCorrection.top;
                }

                if (CRect.Width <= 0)
                    CRect.Width = 1;

                if (CRect.Height <= 0)
                    CRect.Height = 1;


                // Set a min and max rect to prevent errors
                if (CRect.Height <= 50)
                {
                    CRect.Height = 50;
                }

                //if (CRect.Y + CRect.Height > height)
                //{
                //    if (CRect.Y > CRect.Height)
                //    {
                //        CRect.Y = height - 50;
                //        CRect.Height = 50;
                //    }
                //    else
                //    {
                //        CRect.Y = 0;
                //        CRect.Height = height;
                //    }
                //}

                if (CRect.Width <= 50)
                {
                    CRect.Width = 50;
                }
                
                if(CRect.X < 1)
                    CRect.X = 1;

                if (CRect.Y < 1)
                    CRect.Y = 1;

                //if (CRect.X + CRect.Width > width)
                //{
                //    if (CRect.X > CRect.Width)
                //    {
                //        CRect.X = width - 50;
                //        CRect.Width = 50;
                //    }
                //    else
                //    {
                //        CRect.X = 0;
                //        CRect.Width = width;
                //    }
                //}

            }
            catch
            {
                CRect = new Rectangle(0, 0, 0, 0);
            }
        }
 private void hScrollTop_ValueChanged(object sender, EventArgs e)
 {
     try
     {
         tbScrollTop.Text = hScrollTop.Value.ToString();
         this.Profil = FillProfileWithData(null);
         cptRect.SetCaptureRect(Profil);
         SetDxRect();
     }
     catch (Exception ex)
     {
         MessageBox.Show("hScrollTop_ValueChanged::" + ex.Message);
     }
 }
 private void nCustomWindowY_ValueChanged(object sender, EventArgs e)
 {
     try
     {
         this.Profil = FillProfileWithData(null);
         cptRect.SetCaptureRect(Profil);
         SetDxRect();
     }
     catch (Exception ex)
     {
         MessageBox.Show("nCustomWindowY_ValueChanged::" + ex.Message);
     }
 }
 private void hScrollCursorSize_ValueChanged(object sender, EventArgs e)
 {
     try
     {
         this.tbScrollCursorSize.Text = hScrollCursorSize.Value.ToString();
         this.Profil = FillProfileWithData(null);
     }
     catch (Exception ex)
     {
         MessageBox.Show("hScrollCursorSize_ValueChanged::" + ex.Message);
     }
 }
 private void hScrollImageQuality_ValueChanged(object sender, EventArgs e)
 {
     try
     {
         this.tbScrollImageQuality.Text = hScrollImageQuality.Value.ToString();
         this.Profil = FillProfileWithData(null);
         myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, this.hScrollImageQuality.Value);
     }
     catch (Exception ex)
     {
         MessageBox.Show("hScrollImageQuality_ValueChanged::" + ex.Message);
     }
 }
 private void cbSimulate3D_CheckedChanged(object sender, EventArgs e)
 {
     try{
         this.Profil = FillProfileWithData(null);
     }
     catch (Exception ex)
     {
         MessageBox.Show("cbSimulate3D_CheckedChanged::" + ex.Message);
     }
 }
        private ProfileData FillProfileWithData(ProfileData data)
        {
            try
            {
                if (data == null)
                    data = new ProfileData();

                if (data != null)
                {
                    int port = 0;
                    if (int.TryParse(tbServerPort.Text, out port))
                    {
                        data.DataPort = port;
                    }
                    else
                    {
                        data.DataPort = 0;
                    }

                    data.ProcessName = this.tbProcess.Text;
                    data.ShowCursor = this.cbShowCursor.Checked;
                    data.CursorCorrection = this.cbCursorCorrection.Checked;
                    data.BorderCorrection.top = this.hScrollTop.Value;
                    data.BorderCorrection.bottom = this.hScrollBottom.Value;
                    data.BorderCorrection.left = this.hScrollLeft.Value;
                    data.BorderCorrection.right = this.hScrollRight.Value;
                    data.ShowCrosshair = this.cbCrosshair.Checked;
                    data.CorsorColorName = this.cbCursorColors.SelectedItem.ToString();
                    data.CursorSize = this.hScrollCursorSize.Value;
                    data.CursorCorrectionAdjWidth = this.hScrollAdjWidth.Value;
                    data.CursorCorrectionAdjHeight = this.hScrollAdjHeight.Value;
                    data.UseGDICapture = this.cbUseGDI.Checked;
                    data.ImageQuality = this.hScrollImageQuality.Value;
                    data.CustomWindow = this.cbCustomWindow.Checked;
                    data.CustomWindowSize.x = (int)this.nCustomWindowX.Value;
                    data.CustomWindowSize.y = (int)this.nCustomWindowY.Value;
                    data.CustomWindowSize.width = (int)this.nCustomWindowWidth.Value;
                    data.CustomWindowSize.height = (int)this.nCustomWindowHeight.Value;
                }
            }
            catch {
                data = new ProfileData();
            }

            return data;
        }
        private void cbCustomWindow_CheckedChanged(object sender, EventArgs e)
        {
            try
            {

                if (cbCustomWindow.Checked)
                {
                    this.nCustomWindowX.Enabled = true;
                    this.nCustomWindowY.Enabled = true;
                    this.nCustomWindowWidth.Enabled = true;
                    this.nCustomWindowHeight.Enabled = true;
                }
                else
                {
                    this.nCustomWindowX.Enabled = false;
                    this.nCustomWindowY.Enabled = false;
                    this.nCustomWindowWidth.Enabled = false;
                    this.nCustomWindowHeight.Enabled = false;
                }
                this.Profil = FillProfileWithData(null);
                cptRect.SetCaptureRect(Profil);
                SetDxRect();
            }
            catch (Exception ex)
            {
                MessageBox.Show("cbCustomWindow::" + ex.Message);
            }
        }
 private void cbShowCursor_CheckedChanged(object sender, EventArgs e)
 {
     try{
         if (cbShowCursor.Checked)
         {
             cbCursorCorrection.Enabled = true;
             cbCursorColors.Enabled = true;
             hScrollAdjHeight.Enabled = true;
             hScrollAdjWidth.Enabled = true;
             hScrollCursorSize.Enabled = true;
         }
         else
         {
             cbCursorCorrection.Checked = false;
             cbCursorCorrection.Enabled = false;
             cbCursorColors.Enabled = false;
             hScrollAdjHeight.Enabled = false;
             hScrollAdjWidth.Enabled = false;
             hScrollCursorSize.Enabled = false;
         }
         this.Profil = FillProfileWithData(null);
     }
     catch (Exception ex)
     {
         MessageBox.Show("cbShowCursor_CheckedChanged::" + ex.Message);
     }
 }
 private void cbCursors_SelectedIndexChanged(object sender, EventArgs e)
 {
     try
     {
         Color myCl =(Color) this.cbCursorColors.SelectedItem;
         if (myCl != null)
         {
             myBrush = new SolidBrush(myCl);
             this.Profil = FillProfileWithData(null);
         }
     }
     catch {
         myBrush = new SolidBrush(Color.White);
         this.Profil = FillProfileWithData(null);
     }
 }
 private void cbCameraSelection_SelectedIndexChanged(object sender, EventArgs e)
 {
     try
     {
         this.Profil = FillProfileWithData(null);
     }
     catch (Exception ex)
     {
         MessageBox.Show("cbCameraSelection_SelectedIndexChanged::" + ex.Message);
     }
 }
        private void btStartServer_Click(object sender, EventArgs e)
        {
            try{
                this.tbStatus.Text = "Status: Starting Server...";
                DisableInterface();

                this.Profil = FillProfileWithData(null);
                if (this.Profil != null)
                {

                    if (!this.StartServer())
                    {
                        this.EnableInterface();
                    }
                }
                else {
                    MessageBox.Show("Error on reading profile data. \r\n");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("btStartServer_Click::Error on change profile data. \r\n" + ex.Message);
            }
        }
        private void btNewProfile_Click(object sender, EventArgs e)
        {
            try{
                wndAskForProfileName askwnd = new wndAskForProfileName();
                askwnd.ShowDialog();
                if ((!askwnd.Cancel) && (!String.IsNullOrEmpty(askwnd.ProfileName)))
                {
                    ProfileData newProfile = new ProfileData();
                    newProfile.Name = askwnd.ProfileName;

                    int port=0;
                    if (int.TryParse(tbServerPort.Text, out port))
                    {
                        newProfile.DataPort = port;
                    }
                    else
                    {
                        newProfile.DataPort = 0;
                    }

                    newProfile.ProcessName = this.tbProcess.Text;
                    newProfile.ShowCursor = this.cbShowCursor.Checked;
                    newProfile.CursorCorrection = this.cbCursorCorrection.Checked;
                    newProfile.BorderCorrection.top = this.hScrollTop.Value;
                    newProfile.BorderCorrection.bottom = this.hScrollBottom.Value;
                    newProfile.BorderCorrection.left = this.hScrollLeft.Value;
                    newProfile.BorderCorrection.right = this.hScrollRight.Value;
                    newProfile.ShowCrosshair = this.cbCrosshair.Checked;
                    newProfile.CorsorColorName = this.cbCursorColors.SelectedItem.ToString();
                    newProfile.CursorSize = this.hScrollCursorSize.Value;
                    newProfile.CursorCorrectionAdjWidth = this.hScrollAdjWidth.Value;
                    newProfile.CursorCorrectionAdjHeight = this.hScrollAdjHeight.Value;
                    newProfile.UseGDICapture = this.cbUseGDI.Checked;
                    newProfile.ImageQuality = this.hScrollImageQuality.Value;
                    newProfile.CustomWindow = this.cbCustomWindow.Checked;
                    newProfile.CustomWindowSize.x = (int) this.nCustomWindowX.Value;
                    newProfile.CustomWindowSize.y = (int)this.nCustomWindowY.Value;
                    newProfile.CustomWindowSize.width = (int)this.nCustomWindowWidth.Value;
                    newProfile.CustomWindowSize.height = (int)this.nCustomWindowHeight.Value;

                    // save
                    Profiles.Add(newProfile);

                    RefreshProfileData();
                    //cbProfiles.SelectedIndex=cbProfiles.Items.Count - 1;
                    cbProfiles.SelectedItem = newProfile;

                    SaveProfileData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("btNewProfile_Click::Error on creating new profile data. \r\n" + ex.Message);
            }
        }
 private void cbUseShader_CheckedChanged(object sender, EventArgs e)
 {
     try
     {
         if (this.cbUseShader.Checked)
         {
             this.cbShader.Enabled = true;
         }
         else
         {
             this.cbShader.Enabled = false;
         }
         this.Profil = FillProfileWithData(null);
     }
     catch (Exception ex)
     {
         MessageBox.Show("cbUseShader_CheckedChanged::" + ex.Message);
     }
 }