using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Timers;
using DPSDK_RES = System.Int32;

namespace DPSDK_CSharpDemo
{
    public partial class CSharpDemo : Form
    {
        public Dictionary<int, DPSDK_REALPLAY_PARAM> m_mapPreview = new Dictionary<int, DPSDK_REALPLAY_PARAM>();

        public static void ReStartVideoEventCallback(string id, int status, int eventType, CSharpDemo userdata)
        {
            userdata.FuncReStartPreview(id, status, eventType);
            userdata.FuncReStartPlayback(id, status, eventType);
        }

        public void FuncReStartPreview(string id, int status, int eventType)
        {
            // on-line:1, Off-line:2, see DPSDK_DEV_STATUS
            if (status == 1 || eventType == DPSDK_EVENT_RELOGIN_SUCCESS)
            {
                switch (eventType)
                {
                    case DPSDK_EVENT_DEVICE_STATUS:
                        {
                            foreach (KeyValuePair<int, DPSDK_REALPLAY_PARAM> pair in m_mapPreview)
                            {
                                DPSDK_REALPLAY_PARAM tmpParam = pair.Value;
                                int tmpWindowID = pair.Key;

                                if (id == TakeDeviceID(tmpParam.struMediaBaseParam.szCodeId))
                                {
                                    FuncReStartPreviewSpecific(tmpWindowID, tmpParam);
                                }
                            }
                        }
                        break;
                    case DPSDK_EVENT_CHANNEL_STATUS:
                        {
                            foreach (KeyValuePair<int, DPSDK_REALPLAY_PARAM> pair in m_mapPreview)
                            {
                                DPSDK_REALPLAY_PARAM tmpParam = pair.Value;
                                int tmpWindowID = pair.Key;

                                if (id == tmpParam.struMediaBaseParam.szCodeId)
                                {
                                    FuncReStartPreviewSpecific(tmpWindowID, tmpParam);
                                }
                            }
                        }
                        break;
                    case DPSDK_EVENT_RELOGIN_SUCCESS:
                        {
                            foreach (KeyValuePair<int, DPSDK_REALPLAY_PARAM> pair in m_mapPreview)
                            {
                                DPSDK_REALPLAY_PARAM tmpParam = pair.Value;
                                int tmpWindowID = pair.Key;

                                FuncReStartPreviewSpecific(tmpWindowID, tmpParam);
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        public void FuncReStartPreviewSpecific(int iWindowID, DPSDK_REALPLAY_PARAM realPlayParam)
        {
            switch (iWindowID)
            {
                case 1:
                    DPSDK_StopRealPlay(m_iSessionID, m_iPreviewMediaSessionID);
                    m_iPreviewMediaSessionID = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                    DPSDK_StartRealPlayEx(m_iSessionID, m_iPreviewMediaSessionID, ref realPlayParam, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                    break;
                case 2:
                    DPSDK_StopRealPlay(m_iSessionID, m_iPreviewMediaSessionID2);
                    m_iPreviewMediaSessionID2 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                    DPSDK_StartRealPlayEx(m_iSessionID, m_iPreviewMediaSessionID2, ref realPlayParam, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                    break;
                case 3:
                    DPSDK_StopRealPlay(m_iSessionID, m_iPreviewMediaSessionID3);
                    m_iPreviewMediaSessionID3 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                    DPSDK_StartRealPlayEx(m_iSessionID, m_iPreviewMediaSessionID3, ref realPlayParam, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                    break;
                case 4:
                    DPSDK_StopRealPlay(m_iSessionID, m_iPreviewMediaSessionID4);
                    m_iPreviewMediaSessionID4 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                    DPSDK_StartRealPlayEx(m_iSessionID, m_iPreviewMediaSessionID4, ref realPlayParam, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                    break;
                default:
                    break;
            }
        }

        private void trackBarPreviewVolume_ValueChanged(object sender, EventArgs e)
        {
            labelPreviewVolume.Text = trackBarPreviewVolume.Value.ToString();
        }

        private void radioPreviewSinglePlay_CheckedChanged(object sender, EventArgs e)
        {
            if (radioPreviewSinglePlay.Checked)
            {
                textPreviewDeviceID.Enabled = true;
                textPreviewStreamType.Enabled = true;
                textPreviewDataType.Enabled = true;
                textPreviewScreenNum.Enabled = false;
                textPreviewTrackID.Enabled = false;
                textPreviewStartIndex.Enabled = false;

                btnPreviewStartVideo.Enabled = true;
                btnPreviewStopVideo.Enabled = true;
            }
        }

        private void radioPreviewMultiviewPlay_CheckedChanged(object sender, EventArgs e)
        {
            if (radioPreviewMultiviewPlay.Checked)
            {
                textPreviewDeviceID.Enabled = false;
                textPreviewStreamType.Enabled = false;
                textPreviewDataType.Enabled = false;
                textPreviewScreenNum.Enabled = true;
                textPreviewTrackID.Enabled = true;
                textPreviewStartIndex.Enabled = true;

                btnPreviewStartVideo.Enabled = false;
                btnPreviewStopVideo.Enabled = false;
            }
        }

        private void clearPreviewWindowText(bool bClearFlag, bool bClearFlag2, bool bClearFlag3, bool bClearFlag4)
        {
            if (bClearFlag == true)
            {
                Graphics graphics = pictureBoxPreviewVideo.CreateGraphics();
                graphics.Clear(Color.FromArgb(255, 255, 255));
                graphics.Dispose();
            }

            if (bClearFlag2 == true)
            {
                Graphics graphics2 = pictureBoxPreviewVideo2.CreateGraphics();
                graphics2.Clear(Color.FromArgb(255, 255, 255));
                graphics2.Dispose();
            }

            if (bClearFlag3 == true)
            {
                Graphics graphics3 = pictureBoxPreviewVideo3.CreateGraphics();
                graphics3.Clear(Color.FromArgb(255, 255, 255));
                graphics3.Dispose();
            }

            if (bClearFlag4 == true)
            {
                Graphics graphics4 = pictureBoxPreviewVideo4.CreateGraphics();
                graphics4.Clear(Color.FromArgb(255, 255, 255));
                graphics4.Dispose();
            }
        }

        private void setPreviewWindowText(bool bSetFlag, bool bSetFlag2, bool bSetFlag3, bool bSetFlag4)
        {
            Brush brush = new SolidBrush(Color.FromArgb(0, 0, 0));
            Font font = new Font("Microsoft YaHei UI", 60f, FontStyle.Bold);

            if (bSetFlag == true)
            {
                Graphics graphics = pictureBoxPreviewVideo.CreateGraphics();
                graphics.DrawString("No Signal", font, brush, 15, 20);
                graphics.Dispose();
            }

            if (bSetFlag2 == true)
            {
                Graphics graphics2 = pictureBoxPreviewVideo2.CreateGraphics();
                graphics2.DrawString("No Signal", font, brush, 15, 20);
                graphics2.Dispose();
            }

            if (bSetFlag3 == true)
            {
                Graphics graphics3 = pictureBoxPreviewVideo3.CreateGraphics();
                graphics3.DrawString("No Signal", font, brush, 15, 20);
                graphics3.Dispose();
            }

            if (bSetFlag4 == true)
            {
                Graphics graphics4 = pictureBoxPreviewVideo4.CreateGraphics();
                graphics4.DrawString("No Signal", font, brush, 15, 20);
                graphics4.Dispose();
            }

            brush.Dispose();
            font.Dispose();
        }

        private void btnPreviewStartVideo_Click(object sender, EventArgs e)
        {
            textPreviewResult.Text = "";
            if (radioPreviewSinglePlay.Checked)
            {
                string strDisPlayWindows = comPreviewWindows.Text;
                if (strDisPlayWindows.Length < 0)
                {
                    textPreviewResult.Text = AppData._CS("Display Windows cannot be empty");
                    comPreviewWindows.Focus();
                    return;
                }

                // Stops the window currently playing
                btnPreviewStopVideo_Click(sender, e);

                string strXmlData = textPreviewResult.Text;

                ArrayList arrDisPlayWindows = new ArrayList();
                StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

                string strWindowNumList = arrDisPlayWindows[1].ToString();
                ArrayList arrWindowNumList = new ArrayList();
                StringSplit(strWindowNumList, ",", ref arrWindowNumList);
                if (arrWindowNumList.Count > 0)
                {
                    DPSDK_REALPLAY_PARAM pRealPlayParam = new DPSDK_REALPLAY_PARAM();
                    pRealPlayParam.struMediaBaseParam.szCodeId = textPreviewDeviceID.Text;
                    pRealPlayParam.struMediaBaseParam.iStreamType = Convert.ToInt32(textPreviewStreamType.Text);
                    pRealPlayParam.struMediaBaseParam.iDataType = Convert.ToInt32(textPreviewDataType.Text);
                    pRealPlayParam.struMediaBaseParam.iDecodeType = (int)(checkBoxPreviewDecode.CheckState == CheckState.Checked ? DPSDK_DECODE_TYPE.DPSDK_DECODE_HW : DPSDK_DECODE_TYPE.DPSDK_DECODE_SW);
                    pRealPlayParam.struMediaBaseParam.iStreamMode = Convert.ToInt32(textPreviewStreamMode.Text);
                    pRealPlayParam.struMediaBaseParam.uiDelayTime = Convert.ToUInt32(textPreviewDelayTime.Text);
                    string szDeviceID = TakeDeviceID(pRealPlayParam.struMediaBaseParam.szCodeId);
                    string szDeviceName = m_mapIdName.ContainsKey(szDeviceID) ? m_mapIdName[szDeviceID] : "";

                    DPSDK_RES iRet = -1;
                    clearPreviewWindowText(true, true, true, true);
                    for (int i = 0; i < arrWindowNumList.Count; i++)
                    {
                        switch (Convert.ToInt32(arrWindowNumList[i]))
                        {
                            case 1: // Window 1  play
                                {
                                    pRealPlayParam.struMediaBaseParam.pHWnd = pictureBoxPreviewVideo.Handle;
                                    m_iPreviewMediaSessionID = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                                    iRet = DPSDK_StartRealPlayEx(m_iSessionID, m_iPreviewMediaSessionID ,ref pRealPlayParam, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                                    if (iRet == DPSDK_SUCCESS)
                                    {
                                        strXmlData = strXmlData + "start real play in Window 1 success, Device Name: " + szDeviceName + "\r\n";
                                        m_mapPreview.Add(1, pRealPlayParam);
                                        m_strPreviewChannelID = pRealPlayParam.struMediaBaseParam.szCodeId;
                                    }
                                    else
                                    {
                                        strXmlData = strXmlData + "start real play in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                        m_iPreviewMediaSessionID = IntPtr.Zero;

                                        if (iRet == 110291)
                                        {
                                            setPreviewWindowText(true, false, false, false);
                                        }
                                    }
                                }
                                break;
                            case 2: // Window 2  play
                                {
                                    pRealPlayParam.struMediaBaseParam.pHWnd = pictureBoxPreviewVideo2.Handle;
                                    m_iPreviewMediaSessionID2 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                                    iRet = DPSDK_StartRealPlayEx(m_iSessionID, m_iPreviewMediaSessionID2, ref pRealPlayParam, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                                    if (iRet == DPSDK_SUCCESS)
                                    {
                                        strXmlData = strXmlData + "start real play in Window 2 success, Device Name: " + szDeviceName + "\r\n";
                                        m_mapPreview.Add(2, pRealPlayParam);
                                        m_strPreviewChannelID2 = pRealPlayParam.struMediaBaseParam.szCodeId;
                                    }
                                    else
                                    {
                                        strXmlData = strXmlData + "start real play in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                        m_iPreviewMediaSessionID2 = IntPtr.Zero;

                                        if (iRet == 110291)
                                        {
                                            setPreviewWindowText(false, true, false, false);
                                        }
                                    }
                                }
                                break;
                            case 3: // Window 3  play
                                {
                                    pRealPlayParam.struMediaBaseParam.pHWnd = pictureBoxPreviewVideo3.Handle;
                                    m_iPreviewMediaSessionID3 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                                    iRet = DPSDK_StartRealPlayEx(m_iSessionID, m_iPreviewMediaSessionID3, ref pRealPlayParam, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                                    if (iRet == DPSDK_SUCCESS)
                                    {
                                        strXmlData = strXmlData + "start real play in Window 3 success, Device Name: " + szDeviceName + "\r\n";
                                        m_mapPreview.Add(3, pRealPlayParam);
                                        m_strPreviewChannelID3 = pRealPlayParam.struMediaBaseParam.szCodeId;
                                    }
                                    else
                                    {
                                        strXmlData = strXmlData + "start real play in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                        m_iPreviewMediaSessionID3 = IntPtr.Zero;

                                        if (iRet == 110291)
                                        {
                                            setPreviewWindowText(false, false, true, false);
                                        }
                                    }
                                }
                                break;
                            case 4: // Window 4  play
                                {
                                    pRealPlayParam.struMediaBaseParam.pHWnd = pictureBoxPreviewVideo4.Handle;
                                    m_iPreviewMediaSessionID4 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                                    iRet = DPSDK_StartRealPlayEx(m_iSessionID, m_iPreviewMediaSessionID4, ref pRealPlayParam, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                                    if (iRet == DPSDK_SUCCESS)
                                    {
                                        strXmlData = strXmlData + "start real play in Window 4 success, Device Name: " + szDeviceName + "\r\n";
                                        m_mapPreview.Add(4, pRealPlayParam);
                                        m_strPreviewChannelID4 = pRealPlayParam.struMediaBaseParam.szCodeId;
                                    }
                                    else
                                    {
                                        strXmlData = strXmlData + "start real play in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                        m_iPreviewMediaSessionID4 = IntPtr.Zero;

                                        if (iRet == 110291)
                                        {
                                            setPreviewWindowText(false, false, false, true);
                                        }
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
                textPreviewResult.Text = strXmlData;
            }
        }

        private void btnPreviewStopVideo_Click(object sender, EventArgs e)
        {
            textPreviewResult.Text = "";
            string strXmlData = "";
            string strDisPlayWindows = comPreviewWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPreviewResult.Text = AppData._CS("Display Windows cannot be empty");
                comPreviewWindows.Focus();
                return;
            }

            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);
            if (arrWindowNumList.Count > 0)
            {
                DPSDK_RES iRet = -1;
                //txtUserManageResult.Text = strXmlData;
                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1:
                            if (m_iPreviewMediaSessionID != IntPtr.Zero)
                            {
                                iRet = DPSDK_StopRealPlay(m_iSessionID, m_iPreviewMediaSessionID);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    strXmlData = strXmlData + "stop real play in Window 1 success \r\n";
                                    pictureBoxPreviewVideo.Image = null;
                                    m_mapPreview.Remove(1);
                                    m_strPreviewChannelID = "";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "stop real play in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                                m_iPreviewMediaSessionID = IntPtr.Zero;
                                ResetPreviewDrawID(1);
                            }
                            clearPreviewWindowText(true, false, false, false);
                            break;
                        case 2:
                            if (m_iPreviewMediaSessionID2 != IntPtr.Zero)
                            {
                                iRet = DPSDK_StopRealPlay(m_iSessionID, m_iPreviewMediaSessionID2);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    strXmlData = strXmlData + "stop real play in Window 2 success \r\n";
                                    pictureBoxPreviewVideo2.Image = null;
                                    m_mapPreview.Remove(2);
                                    m_strPreviewChannelID2 = "";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "stop real play in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                                m_iPreviewMediaSessionID2 = IntPtr.Zero;
                                ResetPreviewDrawID(2);
                            }
                            clearPreviewWindowText(false, true, false, false);
                            break;
                        case 3:
                            if (m_iPreviewMediaSessionID3 != IntPtr.Zero)
                            {
                                iRet = DPSDK_StopRealPlay(m_iSessionID, m_iPreviewMediaSessionID3);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    strXmlData = strXmlData + "stop real play in Window 3 success \r\n";
                                    pictureBoxPreviewVideo3.Image = null;
                                    m_mapPreview.Remove(3);
                                    m_strPreviewChannelID3 = "";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "stop real play in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                                m_iPreviewMediaSessionID3 = IntPtr.Zero;
                                ResetPreviewDrawID(3);
                            }
                            clearPreviewWindowText(false, false, true, false);
                            break;
                        case 4:
                            if (m_iPreviewMediaSessionID4 != IntPtr.Zero)
                            {
                                iRet = DPSDK_StopRealPlay(m_iSessionID, m_iPreviewMediaSessionID4);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    strXmlData = strXmlData + "stop real play in Window 4 success \r\n";
                                    pictureBoxPreviewVideo4.Image = null;
                                    m_mapPreview.Remove(4);
                                    m_strPreviewChannelID4 = "";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "stop real play in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                                m_iPreviewMediaSessionID4 = IntPtr.Zero;
                                ResetPreviewDrawID(4);
                            }
                            clearPreviewWindowText(false, false, false, true);
                            break;
                        default:
                            break;
                    }
                }
            }

            textPreviewResult.Text = strXmlData;
        }

        private void btnPreviewGetStreamMode_Click(object sender, EventArgs e)
        {
            IntPtr pStreamMode = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_GetPlayStreamMode(m_iSessionID, m_iPreviewMediaSessionID, ref pStreamMode);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewStreamMode.Text = pStreamMode.ToString();
                textPreviewResult.Text = AppData._CS("Get Stream Mode success. uiStreamMode = %1", pStreamMode.ToString());
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Get Stream Mode failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewSetStreamMode_Click(object sender, EventArgs e)
        {
            if (textPreviewStreamMode.Text == "" || textPreviewDelayTime.Text == "")
            {
                AppData.ShowMsgInfo(AppData._CS("Please input correct params!"));
                return;
            }

            uint uiStreamMode = Convert.ToUInt32(textPreviewStreamMode.Text);
            uint uiDelayTime = Convert.ToUInt32(textPreviewDelayTime.Text);
            DPSDK_RES iRet = DPSDK_SetPlayStreamMode(m_iSessionID, m_iPreviewMediaSessionID, uiStreamMode, uiDelayTime);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Set Stream Mode success.");
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Set Stream Mode failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewSetRegion_Click(object sender, EventArgs e)
        {
            if (textPreviewTop.Text == "" || textPreviewBottom.Text == "" || textPreviewLeft.Text == "" || textPreviewRight.Text == "")
            {
                AppData.ShowMsgInfo(AppData._CS("Please input correct params!"));
                return;
            }

            DPSDK_RECT pRECT = new DPSDK_RECT();
            pRECT.top = Convert.ToInt64(textPreviewTop.Text);
            pRECT.bottom = Convert.ToInt64(textPreviewBottom.Text);
            pRECT.left = Convert.ToInt64(textPreviewLeft.Text);
            pRECT.right = Convert.ToInt64(textPreviewRight.Text);
            DPSDK_RES iRet = DPSDK_SetDisplayRegion(m_iSessionID, m_iPreviewMediaSessionID, ref pRECT, pictureBoxPreviewVideo.Handle, checkBoxPreviewEnable.Checked);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Set Display Region success.");
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Set Display Region failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewLockVideo_Click(object sender, EventArgs e)
        {

            string strPreviewLockVideo = textPreviewDeviceID.Text;
            IntPtr pPreviewLockVideo = Marshal.StringToHGlobalAnsi(strPreviewLockVideo);

            if (btnPreviewLockVideo.Text == AppData._CS("LockVideo"))
            {

                DPSDK_RES iRet = DPSDK_OperateRealPlay(m_iSessionID, m_iPreviewMediaSessionID, pPreviewLockVideo, (int)DPSDK_VIDEO_LOCK_TYPE.DPSDK_VIDEO_CMD_LOCK);
                if (iRet == DPSDK_SUCCESS)
                {
                    btnPreviewLockVideo.Text = AppData._CS("UnlockVideo");
                    textPreviewResult.Text = AppData._CS("Lock video success.");
                }
                else
                {
                    textPreviewResult.Text = AppData._CS("Lock video failed, error code = %1", iRet.ToString());
                }
            }
            else if (btnPreviewLockVideo.Text == AppData._CS("UnlockVideo"))
            {
                DPSDK_RES iRet = DPSDK_OperateRealPlay(m_iSessionID, m_iPreviewMediaSessionID, pPreviewLockVideo, (int)DPSDK_VIDEO_LOCK_TYPE.DPSDK_VIDEO_CMD_UNLOCK_ONE);
                if (iRet == DPSDK_SUCCESS)
                {
                    btnPreviewLockVideo.Text = AppData._CS("LockVideo");
                    textPreviewResult.Text = AppData._CS("Unlock video success.");
                }
                else
                {
                    textPreviewResult.Text = AppData._CS("Unlock video failed, error code = %1", iRet.ToString());
                }
            }
        }

        private void btnPreviewGetPicSize_Click(object sender, EventArgs e)
        {
            IntPtr pWidth = IntPtr.Zero;
            IntPtr pHeight = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_GetPictureSize(m_iSessionID, m_iPreviewMediaSessionID, ref pWidth, ref pHeight);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Get Picture Size success. lWidth = %1, lHeight = %2", pWidth.ToInt32().ToString(), pHeight.ToInt32().ToString());
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Get Picture Size failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewGetPicToFile_Click(object sender, EventArgs e)
        {
            uint uiPicFormat = (uint)comboBoxPreviewPicture.SelectedIndex;
            string strPicturePath = textPreviewPicPath.Text;
            DPSDK_RES iRet = DPSDK_Get24BitPictureFile(m_iSessionID, m_iPreviewMediaSessionID, uiPicFormat, strPicturePath);
            if (iRet == DPSDK_SUCCESS)
            {
                pictureBoxPreviewPicture.Image = Image.FromFile(strPicturePath);
                textPreviewResult.Text = AppData._CS("Get Picture To File success.");
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Get Picture To File failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewGetPicToBuffer_Click(object sender, EventArgs e)
        {
            IntPtr pWidth = IntPtr.Zero;
            IntPtr pHeight = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_GetPictureSize(m_iSessionID, m_iPreviewMediaSessionID, ref pWidth, ref pHeight);
            if (iRet != DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Get Picture Size failed, iRet = %1", iRet.ToString());
                return;
            }

            int iBufsize = GetPicBuffSize(comboBoxPreviewPicture.SelectedIndex, pHeight.ToInt32(), pWidth.ToInt32());
            byte[] pPicBuf = new byte[iBufsize];
            IntPtr pPicSize = IntPtr.Zero;
            iRet = DPSDK_GetPictureBuf(m_iSessionID, m_iPreviewMediaSessionID, ref pPicBuf[0], (IntPtr)iBufsize, ref pPicSize, (uint)comboBoxPreviewPicture.SelectedIndex);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Get Picture To Buffer success. lBufSize = %1, iFactPicSize = %2", iBufsize.ToString(), pPicSize.ToString());
                MemoryStream ms = new MemoryStream(pPicBuf);
                pictureBoxPreviewPicture.Image = Image.FromStream(ms);
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Get Picture To File Buffer, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewOpenSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_OpenSound(m_iSessionID, m_iPreviewMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Open Sound success.");
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Open Sound failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewCloseSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_CloseSound(m_iSessionID, m_iPreviewMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Close Sound success.");
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Close Sound failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewOpenShareSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_OpenSoundShare(m_iSessionID, m_iPreviewMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Open Share Sound success.");
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Open Share Sound failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewCloseShareSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_CloseSoundShare(m_iSessionID, m_iPreviewMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Close Share Sound success.");
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Close Share Sound failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewIsOpenSound_Click(object sender, EventArgs e)
        {
            IntPtr pIsOpenSound = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_IsOpenSoundState(m_iSessionID, m_iPreviewMediaSessionID, ref pIsOpenSound);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Is Open Sound State success. bIsOpen = %1", pIsOpenSound.ToString());
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Is Open Sound State failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewSetVolume_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_SetVolume(m_iSessionID, m_iPreviewMediaSessionID, (uint)trackBarPreviewVolume.Value);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Set Volume success.");
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Set Volume failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewGetVolum_Click(object sender, EventArgs e)
        {
            IntPtr pVolume = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_GetVolume(m_iSessionID, m_iPreviewMediaSessionID, ref pVolume);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Get Volume success. iVolume = %1", pVolume.ToString());
                labelPreviewVolume.Text = pVolume.ToString();
                trackBarPreviewVolume.Value = pVolume.ToInt32();
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Get Volume failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewSetSplitLen_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_SetSplitRecordLen(m_iSessionID, m_iPreviewMediaSessionID, (uint)Convert.ToUInt32(textPreviewSplitLen.Text));
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Set Split Record Len success.");
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Set Split Record Len failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewStartRecord_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_StartRecord(m_iSessionID, m_iPreviewMediaSessionID, textPreviewRecordPath.Text, (uint)Convert.ToUInt32(textPreviewSplitLen.Text), 0);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Start Record success.");
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Start Record failed, iRet = %1", iRet.ToString());
            }
        }

        private void btnPreviewStopRecord_Click(object sender, EventArgs e)
        {
            uint uiBufLen = (uint)(Marshal.SizeOf(typeof(UInt64)) + Marshal.SizeOf(typeof(DPSDK_FILE_STORE_INFO)) * 100);
            IntPtr pRecordFile = Marshal.AllocHGlobal((int)uiBufLen);
            DPSDK_RES iRet = DPSDK_StopRecord(m_iSessionID, m_iPreviewMediaSessionID, pRecordFile, uiBufLen);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Stop Record success.") + "\r\n";
                DPSDK_FILE_STORE_LIST stuRecordFile = new DPSDK_FILE_STORE_LIST();
                stuRecordFile = (DPSDK_FILE_STORE_LIST)Marshal.PtrToStructure(pRecordFile, typeof(DPSDK_FILE_STORE_LIST));
                if (stuRecordFile.ulTotal > 0)
                {
                    DPSDK_FILE_STORE_INFO[] pFileList = new DPSDK_FILE_STORE_INFO[stuRecordFile.ulTotal];
                    for (int i = 0; i < (int)stuRecordFile.ulTotal; i++)
                    {
                        IntPtr ptr = new IntPtr(pRecordFile.ToInt32() + Marshal.SizeOf(typeof(UInt64)) + Marshal.SizeOf(typeof(DPSDK_FILE_STORE_INFO)) * i);
                        pFileList[i] = (DPSDK_FILE_STORE_INFO)Marshal.PtrToStructure(ptr, typeof(DPSDK_FILE_STORE_INFO));
                        textPreviewResult.Text += "uiStoreLen = " + pFileList[i].uiStoreLen.ToString() + ", lBeginTime = " + pFileList[i].lBeginTime.ToString() + ", lEndTimeTime = " + pFileList[i].lEndTime.ToString() + ", szFile = " + pFileList[i].szFile + "\r\n";
                    }
                }
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Stop Record failed, iRet = %1", iRet.ToString());
            }
            Marshal.FreeHGlobal(pRecordFile);

        }

        private void btnPreviewIsRecordState_Click(object sender, EventArgs e)
        {
            IntPtr pIsRecord = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_IsRecordState(m_iSessionID, m_iPreviewMediaSessionID, ref pIsRecord);
            if (iRet == DPSDK_SUCCESS)
            {
                textPreviewResult.Text = AppData._CS("Get Is Record State success. bIsRecord = %1", pIsRecord.ToString());
            }
            else
            {
                textPreviewResult.Text = AppData._CS("Get Is Record State failed, iRet = %1", iRet.ToString());
            }
        }

        private void ResetPreviewDrawID(int iWindowID)
        {
            int iIndex = iWindowID - 1;
            m_vecPreviewDrawTitleID[iIndex] = IntPtr.Zero;
            m_vecPreviewDrawLocalTimeID[iIndex] = IntPtr.Zero;
            m_vecPreviewDrawChannelNameID[iIndex] = IntPtr.Zero;
        }

        private void checkBoxPreviewShowTitle_CheckedChanged(object sender, EventArgs e)
        {
            string strOSDInfo = previewOSDTitle.Text;
            string strTitleX = previewTitleX.Text;
            string strTitleY = previewTitleY.Text;
            int iX, iY;
            if (strOSDInfo == "" || !int.TryParse(strTitleX, out iX) || !int.TryParse(strTitleY, out iY))
            {
                textPreviewResult.Text = AppData._CS("Invalid parameter");
                return;
            }

            byte[] strUFT8OSDInfo = Encoding.GetEncoding("utf-8").GetBytes(strOSDInfo);
            if (strUFT8OSDInfo.Length > DPSDK_MEMO_LEN)
            {
                textPreviewResult.Text = AppData._CS("Exceeded maximum character length");
                return;
            }

            CheckBox checkBox = (CheckBox)sender;
            bool bDraw = checkBox.Checked ? true : false;

            string strDisPlayWindows = comPreviewWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPreviewResult.Text = AppData._CS("Display Windows cannot be empty");
                comPreviewWindows.Focus();
                return;
            }

            string strXmlData = "";
            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);
            if (arrWindowNumList.Count > 0)
            {
                DPSDK_RES iRet = -1;
                DPSDK_EXTRA_OSD_PARAM pExtraOSDParam = new DPSDK_EXTRA_OSD_PARAM();
                pExtraOSDParam.iX = iX;
                pExtraOSDParam.iY = iY;
                pExtraOSDParam.szOSDInfo = Encoding.UTF8.GetString(strUFT8OSDInfo);

                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1: // Window 1 
                            if (bDraw)
                            {
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawTitleID[0];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawTitleID[0] = iDrawID;
                                    strXmlData = strXmlData + "Draw title in Window 1 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw title in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawTitleID[0];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawTitleID[0] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear title in Window 1 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear title in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 2: // Window 2 
                            if (bDraw)
                            {
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawTitleID[1];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawTitleID[1] = iDrawID;
                                    strXmlData = strXmlData + "Draw title in Window 2 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw title in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawTitleID[1];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawTitleID[1] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear title in Window 2 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear title in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 3: // Window 3 
                            if (bDraw)
                            {
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawTitleID[2];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawTitleID[2] = iDrawID;
                                    strXmlData = strXmlData + "Draw title in Window 3 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw title in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawTitleID[2];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawTitleID[2] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear title in Window 3 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear title in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 4: // Window 4 
                            if (bDraw)
                            {
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawTitleID[3];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawTitleID[3] = iDrawID;
                                    strXmlData = strXmlData + "Draw title in Window 4 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw title in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawTitleID[3];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawTitleID[3] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear title in Window 4 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear title in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        default:
                            break;
                    }
                }
            }
            textPreviewResult.Text = strXmlData;
        }

        private void StartPreviewLocalTimer()
        {
            m_previewLocalTimer.Elapsed += new ElapsedEventHandler(UpdatePreviewLocalTimeInfo);
            m_previewLocalTimer.Enabled = true;
            m_previewLocalTimer.Start();
        }

        private void StopPreviewLocalTimer()
        {
            m_previewLocalTimer.Elapsed -= new ElapsedEventHandler(UpdatePreviewLocalTimeInfo);
            m_previewLocalTimer.Enabled = false;
            m_previewLocalTimer.Stop();
        }

        private void checkBoxPreviewShowLocalTime_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkBox = (CheckBox)sender;
            bool bDrawLocalTime = checkBox.Checked ? true : false;
            SetPreviewDrawFlags(bDrawLocalTime);
            if (bDrawLocalTime)
            {
                if (!m_previewLocalTimer.Enabled)
                {
                    StartPreviewLocalTimer();
                }
            }
            else
            {
                ClearPreviewLocalTime();

                if (!m_bPreviewDrawLocalTime && !m_bPreviewDrawLocalTime2
                    && !m_bPreviewDrawLocalTime3 && !m_bPreviewDrawLocalTime4)
                {
                    StopPreviewLocalTimer();
                }
            }
        }

        private void SetPreviewDrawFlags(bool bDraw)
        {
            string strDisPlayWindows = comPreviewWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPreviewResult.Text = AppData._CS("Display Windows cannot be empty");
                comPreviewWindows.Focus();
                return;
            }

            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);
            if (arrWindowNumList.Count > 0)
            {
                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1: // Window 1 
                            m_bPreviewDrawLocalTime = bDraw;
                            break;
                        case 2:
                            m_bPreviewDrawLocalTime2 = bDraw;
                            break;
                        case 3:
                            m_bPreviewDrawLocalTime3 = bDraw;
                            break;
                        case 4:
                            m_bPreviewDrawLocalTime4 = bDraw;
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        private void DrawPreviewLocalTime()
        {
            string strLocalTime = DateTime.Now.ToLocalTime().ToString();

            string strLocalTimeX = previewLocalTimeX.Text;
            string strLocalTimeY = previewLocalTimeY.Text;
            int iX, iY;
            if (strLocalTime == "" || !int.TryParse(strLocalTimeX, out iX) || !int.TryParse(strLocalTimeY, out iY))
            {
                textPreviewResult.Text = AppData._CS("Invalid parameter");
                return;
            }
            byte[] utf8Bytes = Encoding.UTF8.GetBytes(strLocalTime);
            string strXmlData = "";
            DPSDK_RES iRet = -1;
            DPSDK_EXTRA_OSD_PARAM pExtraOSDParam = new DPSDK_EXTRA_OSD_PARAM();
            pExtraOSDParam.iX = iX;
            pExtraOSDParam.iY = iY;
            pExtraOSDParam.szOSDInfo = Encoding.UTF8.GetString(utf8Bytes);

            if (m_bPreviewDrawLocalTime)
            {
                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawLocalTimeID[0];
                if (iDrawID == IntPtr.Zero)
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                }
                else
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                }

                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPreviewDrawLocalTimeID[0] = iDrawID;
                    strXmlData = strXmlData + "Draw Local Time in Window 1 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Draw Local TIme in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (m_bPreviewDrawLocalTime2)
            {
                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawLocalTimeID[1];
                if (iDrawID == IntPtr.Zero)
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                }
                else
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                }

                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPreviewDrawLocalTimeID[1] = iDrawID;
                    strXmlData = strXmlData + "Draw Local Time in Window 2 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Draw Local TIme in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (m_bPreviewDrawLocalTime3)
            {
                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawLocalTimeID[2];
                if (iDrawID == IntPtr.Zero)
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                }
                else
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                }

                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPreviewDrawLocalTimeID[2] = iDrawID;
                    strXmlData = strXmlData + "Draw Local Time in Window 3 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Draw Local TIme in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (m_bPreviewDrawLocalTime4)
            {
                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawLocalTimeID[3];
                if (iDrawID == IntPtr.Zero)
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                }
                else
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                }

                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPreviewDrawLocalTimeID[3] = iDrawID;
                    strXmlData = strXmlData + "Draw Local Time in Window 4 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Draw Local TIme in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            textPreviewResult.Text = strXmlData;
        }

        private void ClearPreviewLocalTime()
        {
            string strXmlData = "";
            DPSDK_RES iRet = -1;
            DPSDK_EXTRA_OSD_PARAM pExtraOSDParam = new DPSDK_EXTRA_OSD_PARAM();
            pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;

            if (!m_bPreviewDrawLocalTime)
            {
                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawLocalTimeID[0];
                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPreviewDrawLocalTimeID[0] = IntPtr.Zero;
                    strXmlData = strXmlData + "Clear Local Time in Window 1 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Clear Local TIme in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (!m_bPreviewDrawLocalTime2)
            {
                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawLocalTimeID[1];
                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPreviewDrawLocalTimeID[1] = IntPtr.Zero;
                    strXmlData = strXmlData + "Clear Local Time in Window 2 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Clear Local TIme in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (!m_bPreviewDrawLocalTime3)
            {
                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawLocalTimeID[2];
                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPreviewDrawLocalTimeID[2] = IntPtr.Zero;
                    strXmlData = strXmlData + "Clear Local Time in Window 3 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Clear Local TIme in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (!m_bPreviewDrawLocalTime4)
            {
                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawLocalTimeID[3];
                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPreviewDrawLocalTimeID[3] = IntPtr.Zero;
                    strXmlData = strXmlData + "Clear Local Time in Window 4 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Clear Local TIme in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            textPreviewResult.Text = strXmlData;
        }

        private void UpdatePreviewLocalTimeInfo(object sender, ElapsedEventArgs e)
        {
            if (m_iPreviewMediaSessionID == IntPtr.Zero && m_iPreviewMediaSessionID2 == IntPtr.Zero
                  && m_iPreviewMediaSessionID3 == IntPtr.Zero && m_iPreviewMediaSessionID4 == IntPtr.Zero)
            {
                StopPreviewLocalTimer();
            }

            DrawPreviewLocalTime();
        }

        private void checkBoxPreviewShowChannelName_CheckedChanged(object sender, EventArgs e)
        {
            string strChannelNameX = previewChannelNameX.Text;
            string strChannelNameY = previewChannelNameY.Text;
            int iX, iY;
            if (!int.TryParse(strChannelNameX, out iX) || !int.TryParse(strChannelNameY, out iY))
            {
                textPreviewResult.Text = AppData._CS("Invalid parameter");
                return;
            }

            CheckBox checkBox = (CheckBox)sender;
            bool bDraw = checkBox.Checked ? true : false;

            string strDisPlayWindows = comPreviewWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPreviewResult.Text = AppData._CS("Display Windows cannot be empty");
                comPreviewWindows.Focus();
                return;
            }

            string strXmlData = "";
            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);
            if (arrWindowNumList.Count > 0)
            {
                DPSDK_RES iRet = -1;
                DPSDK_EXTRA_OSD_PARAM pExtraOSDParam = new DPSDK_EXTRA_OSD_PARAM();
                pExtraOSDParam.iX = iX;
                pExtraOSDParam.iY = iY;

                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1: // Window 1 
                            if (bDraw)
                            {
                                byte[] strUFT8OSDInfo = Encoding.GetEncoding("utf-8").GetBytes(GetChannelName(m_strPreviewChannelID));
                                if (strUFT8OSDInfo.Length > DPSDK_MEMO_LEN)
                                {
                                    textPreviewResult.Text = AppData._CS("Exceeded maximum character length");
                                    break;
                                }
                                pExtraOSDParam.szOSDInfo = Encoding.UTF8.GetString(strUFT8OSDInfo);

                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawChannelNameID[0];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawChannelNameID[0] = iDrawID;
                                    strXmlData = strXmlData + "Draw channel name in Window 1 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw channel name in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else 
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawChannelNameID[0];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawChannelNameID[0] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear channel name in Window 1 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear channel name in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 2: // Window 2 
                            if (bDraw)
                            {
                                byte[] strUFT8OSDInfo = Encoding.GetEncoding("utf-8").GetBytes(GetChannelName(m_strPreviewChannelID2));
                                if (strUFT8OSDInfo.Length > DPSDK_MEMO_LEN)
                                {
                                    textPreviewResult.Text = AppData._CS("Exceeded maximum character length");
                                    break;
                                }
                                pExtraOSDParam.szOSDInfo = Encoding.UTF8.GetString(strUFT8OSDInfo);

                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawChannelNameID[1];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawChannelNameID[1] = iDrawID;
                                    strXmlData = strXmlData + "Draw channel name in Window 2 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw channel name in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawChannelNameID[1];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawChannelNameID[1] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear channel name in Window 2 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear channel name in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 3: // Window 3 
                            if (bDraw)
                            {
                                byte[] strUFT8OSDInfo = Encoding.GetEncoding("utf-8").GetBytes(GetChannelName(m_strPreviewChannelID3));
                                if (strUFT8OSDInfo.Length > DPSDK_MEMO_LEN)
                                {
                                    textPreviewResult.Text = AppData._CS("Exceeded maximum character length");
                                    break;
                                }
                                pExtraOSDParam.szOSDInfo = Encoding.UTF8.GetString(strUFT8OSDInfo);

                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawChannelNameID[2];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawChannelNameID[2] = iDrawID;
                                    strXmlData = strXmlData + "Draw channel name in Window 3 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw channel name in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else 
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawChannelNameID[2];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawChannelNameID[2] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear channel name in Window 3 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear channel name in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 4: // Window 4 
                            if (bDraw)
                            {
                                byte[] strUFT8OSDInfo = Encoding.GetEncoding("utf-8").GetBytes(GetChannelName(m_strPreviewChannelID4));
                                if (strUFT8OSDInfo.Length > DPSDK_MEMO_LEN)
                                {
                                    textPreviewResult.Text = AppData._CS("Exceeded maximum character length");
                                    break;
                                }
                                pExtraOSDParam.szOSDInfo = Encoding.UTF8.GetString(strUFT8OSDInfo);

                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawChannelNameID[3];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawChannelNameID[3] = iDrawID;
                                    strXmlData = strXmlData + "Draw channel name in Window 4 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw channel name in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPreviewDrawChannelNameID[3];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPreviewMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPreviewDrawChannelNameID[3] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear channel name in Window 4 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear channel name in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        default:
                            break;
                    }
                }
                textPreviewResult.Text = strXmlData;
            }
        }
    }
}