海康相机显示ROI

1.画ROI

    #region ROI设定
    //画矩形框
    Point start;
    Point end;
    bool blnDraw;   //在MouseMove事件中判断是否绘制矩形框
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            start = e.Location;
            blnDraw = true;
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (blnDraw)
        {
            if (e.Button != MouseButtons.Left)
                return;
            end = e.Location;
            pictureBox1.Invalidate();
        }

    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        PictureBox pic = sender as PictureBox;
        if (e.Button == MouseButtons.Left)
        {
            end = e.Location;
            blnDraw = false;
        }
        if (e.Button == MouseButtons.Right)
        {

        }
    }
    reletiveRoi _roi= new reletiveRoi();
    public reletiveRoi roi
    {
        get { return _roi; }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        PictureBox pic = sender as PictureBox;
        Pen pen = new Pen(Color.Red, 3);
        pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;     //绘制线的格式
        if (blnDraw)
        {
            //此处是为了在绘制时可以由上向下绘制,也可以由下向上
            e.Graphics.DrawRectangle(pen, Math.Min(start.X, end.X), Math.Min(start.Y, end.Y), Math.Abs(start.X - end.X), Math.Abs(start.Y - end.Y));
            _roi.width = (Math.Abs(start.X - end.X) / (double)pic.Width);
            _roi.height = (Math.Abs(start.Y - end.Y) / (double)pic.Height);
            _roi.offsetX = (Math.Min(start.X, end.X) / (double)pic.Width);
            _roi.offsetY = (Math.Min(start.Y, end.Y) / (double)pic.Height);
        }
        pen.Dispose();
    }
    #endregion

2.写入相机
得到相机最大尺寸
private ImageSize GetMaxSize()
{
ImageSize imgMaxSize = new ImageSize();
int nRet = MyCamera.MV_OK;
//_isDrawROI = false;
//ch:获取包大小||en: Get Payload Size
MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
//ch:获取高||en: Get Height
nRet = m_pMyCamera.MV_CC_GetIntValue_NET(“HeightMax”, ref stParam);
if (MyCamera.MV_OK != nRet)
{
ShowErrorMsg(“Get Height Fail!”, nRet);
}
imgMaxSize.height = stParam.nCurValue;

        //ch:获取宽||en:Get Width
        nRet = m_pMyCamera.MV_CC_GetIntValue_NET("WidthMax", ref stParam);
        if (MyCamera.MV_OK != nRet)
        {
            ShowErrorMsg("Get Width Fail!", nRet);
        }
        imgMaxSize.width = stParam.nCurValue;
        return imgMaxSize;
    }
    
   按比列写入
           public void SetRoi()
    {
        int nRet = MyCamera.MV_OK;
        ImageSize imgMaxSize = GetMaxSize();
        uint nWidthMax = imgMaxSize.width;
        uint nHeightMax = imgMaxSize.height;

        nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetX", 0);
        if (MyCamera.MV_OK != nRet)
        {
            ShowErrorMsg("Set OffsetX Fail!", nRet);
        }
        nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetY", 0);
        if (MyCamera.MV_OK != nRet)
        {
            ShowErrorMsg("Set OffsetY Fail!", nRet);
        }

        imgWidth = (uint)(_roi.width * nWidthMax);
        uint nVal = (imgWidth / 32) * 32;
        nRet = m_pMyCamera.MV_CC_SetIntValue_NET("Width", nVal);
        if (MyCamera.MV_OK != nRet)
        {
            ShowErrorMsg("Set Width Fail!", nRet);
        }
        imgHeight = (uint)(_roi.height * nHeightMax);
        nVal = (imgHeight / 16) * 16;
        nRet = m_pMyCamera.MV_CC_SetIntValue_NET("Height", nVal);
        if (MyCamera.MV_OK != nRet)
        {
            ShowErrorMsg("Set Height Fail!", nRet);
        }
        offsetX = (uint)(_roi.offsetX * nWidthMax);
        nVal = (offsetX / 16) * 16;
        nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetX", nVal);
        if (MyCamera.MV_OK != nRet)
        {
            ShowErrorMsg("Set OffsetX Fail!", nRet);
        }
        offsetY = (uint)(_roi.offsetY * nHeightMax);
        nVal = (offsetY / 16) * 16;
        nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetY", nVal);
        if (MyCamera.MV_OK != nRet)
        {
            ShowErrorMsg("Set OffsetY Fail!", nRet);
        }

    }

3.调用

相机先关闭、再打开
private void btnGetRoi_Click(object sender, EventArgs e)
{
ReadRoi();
ucCamera2.closeCamera();
ucCamera2.openCamera();
ucCamera2.roi = this.roi;
ucCamera2.SetRoi();
ucCamera2.StartGrab(pictureBox1);
}


版权声明:本文为Sunny_love_you原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。