Esempio n. 1
0
        private void Button_CAPTURE_Click(object sender, EventArgs e)
        {
            if (m_nDeviceCurSel == -1)
            {
                return;
            }

            int nCaptureWidth    = 0;
            int nCaptureHeight   = 0;
            int nCaptureBitCount = 0;

            int nRet = KSJApiBase.KSJ_CaptureGetSizeEx(m_nDeviceCurSel, ref nCaptureWidth, ref nCaptureHeight, ref nCaptureBitCount);

            PrintErrorMessage(nRet);

            byte[] pImageData = new byte[nCaptureWidth * nCaptureHeight * (nCaptureBitCount >> 3)];

            long counterStart = 0;

            KSJApiBase.QueryPerformanceCounter(ref counterStart);

            nRet = KSJApiBase.KSJ_CaptureRgbData(m_nDeviceCurSel, pImageData);

            PrintErrorMessage(nRet);
            if (nRet != KSJCode.RET_SUCCESS)
            {
                TextBox_ELAPSE_TIME.Text = "Capture Fail.";
            }

            long counterEnd = 0;

            KSJApiBase.QueryPerformanceCounter(ref counterEnd);

            long nFreq = 0;

            KSJApiBase.QueryPerformanceFrequency(ref nFreq);

            float fInterval = (float)(counterEnd - counterStart);
            float fElapse   = fInterval / (float)nFreq * 1000;  // MS

            bool bCheck = CheckBox_SAVE.Checked;

            if (bCheck)
            {
                string szFileName = string.Format("capture-{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}.bmp", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                                                  DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond, fElapse);
                KSJApiBase.KSJ_HelperSaveToBmp(pImageData, nCaptureWidth, nCaptureHeight, nCaptureBitCount, szFileName);
            }

            TextBox_ELAPSE_TIME.Text = string.Format("Elapse: {0}ms", fElapse);
        }
Esempio n. 2
0
        private unsafe void Button_CAPTURE_Click(object sender, EventArgs e)
        {
            if (m_nDeviceCurSel == -1)
            {
                return;
            }

            int nCaptureWidth    = 0;
            int nCaptureHeight   = 0;
            int nCaptureBitCount = 0;

            int nRet = KSJApiBase.KSJ_CaptureGetSizeEx(m_nDeviceCurSel, ref nCaptureWidth, ref nCaptureHeight, ref nCaptureBitCount);

            PrintErrorMessage(nRet);

            byte[] pImageData = new byte[nCaptureWidth * nCaptureHeight * (nCaptureBitCount >> 3)];

            long counterStart = 0;

            KSJApiBase.QueryPerformanceCounter(ref counterStart);

            nRet = KSJApiBase.KSJ_CaptureRgbData(m_nDeviceCurSel, pImageData);

            PrintErrorMessage(nRet);
            if (nRet != KSJCode.RET_SUCCESS)
            {
                TextBox_ELAPSE_TIME.Text = "Capture Fail.";
            }

            long counterEnd = 0;

            KSJApiBase.QueryPerformanceCounter(ref counterEnd);

            long nFreq = 0;

            KSJApiBase.QueryPerformanceFrequency(ref nFreq);

            float fInterval = (float)(counterEnd - counterStart);
            float fElapse   = fInterval / (float)nFreq * 1000;  // MS

            bool bCheck = CheckBox_SAVE.Checked;

            if (bCheck)
            {
                string szFileName = string.Format("capture-{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}.bmp", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                                                  DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond, fElapse);
                KSJApiBase.KSJ_HelperSaveToBmp(pImageData, nCaptureWidth, nCaptureHeight, nCaptureBitCount, szFileName);
            }

            TextBox_ELAPSE_TIME.Text = string.Format("Elapse: {0}ms", fElapse);

            Bitmap bitmap = new Bitmap(nCaptureWidth, nCaptureHeight, PixelFormat.Format8bppIndexed);
            //获取图像的BitmapData对像
            BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            byte *ptr = (byte *)(bmpData.Scan0);

            for (int i = 0; i < nCaptureHeight; i++)
            {
                for (int j = 0; j < nCaptureWidth; j++)
                {
                    int offset = (nCaptureHeight - i - 1) * bmpData.Width + j;
                    int val    = pImageData[offset];
                    *(ptr + j) = (byte)val;
                }

                ptr = ptr + bmpData.Stride;
            }

            //Marshal.Copy(bmpData, 0, ptr, scanBytes);
            bitmap.UnlockBits(bmpData);  // 解锁内存区域

            // 修改生成位图的索引表,从伪彩修改为灰度
            ColorPalette palette;

            // 获取一个Format8bppIndexed格式图像的Palette对象
            using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
            {
                palette = bmp.Palette;
            }
            for (int i = 0; i < 256; i++)
            {
                palette.Entries[i] = Color.FromArgb(i, i, i);
            }
            // 修改生成位图的索引表
            bitmap.Palette = palette;

            Cognex.VisionPro.CogImage8Grey G8 = new Cognex.VisionPro.CogImage8Grey(bitmap);

            cogDisplay1.Image = G8;
        }