private void OnStartRecord(object sender, EventArgs e) { int err; uint dwWarn = 0, dwError = 0, dwStatus = 0; ushort width = 0; ushort height = 0; ushort widthmax = 0; ushort heightmax = 0; // It is recommended to call this function in order to get information about the camera internal state err = PCO_SDK_LibWrapper.PCO_GetCameraHealthStatus(f_CameraHandle, ref dwWarn, ref dwError, ref dwStatus); PCO_SDK_LibWrapper.PCO_SetTriggerMode(f_CameraHandle, 0); err = PCO_SDK_LibWrapper.PCO_ArmCamera(f_CameraHandle); err = PCO_SDK_LibWrapper.PCO_GetSizes(f_CameraHandle, ref width, ref height, ref widthmax, ref heightmax); err = PCO_SDK_LibWrapper.PCO_CamLinkSetImageParameters(f_CameraHandle, (UInt16)width, (UInt16)height); err = PCO_SDK_LibWrapper.PCO_SetRecordingState(f_CameraHandle, 1); cbGrabCamera.IsEnabled = true; if (camDialog != IntPtr.Zero) { PCO_SDK_LibWrapper.PCO_EnableDialogCam(camDialog, false); } f_CameraTimer.Start(); }
private void OnGrabImage(object sender, EventArgs evt) { int err = 0; int size; System.IntPtr evhandle; Bitmap imagebmp; UIntPtr buf; bool bauto = true; // set this to true to get auto min max UInt16 width = 0; UInt16 height = 0; UInt16 widthmax = 0; UInt16 heightmax = 0; int ishift = 16 - pcoDescr.wDynResDESC; int ipadd = width / 4; int iconvertcol = pcoDescr.wColorPatternDESC / 0x1000; int max; int min; ipadd *= 4; ipadd = width - ipadd; err = PCO_SDK_LibWrapper.PCO_GetSizes(f_CameraHandle, ref width, ref height, ref widthmax, ref heightmax); size = width * height * 2; buf = UIntPtr.Zero; evhandle = IntPtr.Zero; if ((bufwidth != width) || (bufheight != height)) { if (bufnr != -1) { PCO_SDK_LibWrapper.PCO_FreeBuffer(f_CameraHandle, bufnr); } bufnr = -1; imagedata = new byte[(width + ipadd) * height * 3]; err = PCO_SDK_LibWrapper.PCO_AllocateBuffer(f_CameraHandle, ref bufnr, size, ref buf, ref evhandle); if (err == 0) { bufwidth = width; bufheight = height; } } else { err = PCO_SDK_LibWrapper.PCO_GetBuffer(f_CameraHandle, bufnr, ref buf, ref evhandle); } if ((convertDialog == IntPtr.Zero) && (f_ConvertHandle != IntPtr.Zero)) { var hdl = new WindowInteropHelper(this).Handle; PCO_Convert_LibWrapper.PCO_OpenConvertDialog(ref convertDialog, hdl, "Convert Dialog", WM_APPp100, f_ConvertHandle, 500, 100); } //Mandatory for Cameralink and GigE. Don't care for all other interfaces, so leave it intact here. err = PCO_SDK_LibWrapper.PCO_AddBufferEx(f_CameraHandle, 0, 0, bufnr, (UInt16)width, (UInt16)height, (UInt16)pcoDescr.wDynResDESC); bool bImageIsOk = false; uint res = WaitForSingleObject(evhandle, 3000); if (res == 0) { bImageIsOk = true; } if (!bImageIsOk) { return; } // End Event Block unsafe { short *bufi = (short *)buf.ToPointer(); max = 0; min = 65535; for (int i = 20 * width; i < height * width; i++) { bufi[i] >>= ishift; if (bufi[i] > max) { max = bufi[i]; } if (bufi[i] < min) { min = bufi[i]; } } if (max <= min) { max = min + 1; } } PCO_Convert_LibWrapper.PCO_Convert16TOCOL(f_ConvertHandle, 0, iconvertcol, width, height, buf, imagedata); if ((convertDialog != IntPtr.Zero) && (f_ConvertHandle != IntPtr.Zero)) { PCO_Convert_LibWrapper.PCO_SetDataToDialog(convertDialog, width, height, buf, imagedata); } if (bauto) { PCO_ConvertStructures.PCO_Display strDisplay = new PCO_ConvertStructures.PCO_Display(1) { wSize = (ushort)Marshal.SizeOf(typeof(PCO_ConvertStructures.PCO_Display)) }; err = PCO_Convert_LibWrapper.PCO_ConvertGetDisplay(f_ConvertHandle, ref strDisplay); strDisplay.iScale_min = min; strDisplay.iScale_max = max; err = PCO_Convert_LibWrapper.PCO_ConvertSetDisplay(f_ConvertHandle, ref strDisplay); err = PCO_Convert_LibWrapper.PCO_SetConvertDialog(convertDialog, f_ConvertHandle); } Dispatcher.Invoke(() => Debug.WriteLine(CbPixelFormatConv.SelectedValue)); imagebmp = new Bitmap(width, height, PixelFormat.Format24bppRgb); //Format24bppRgb Rectangle dimension = new Rectangle(0, 0, imagebmp.Width, imagebmp.Height); BitmapData picData = imagebmp.LockBits(dimension, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); //Format24bppRgb IntPtr pixelStartAddress = picData.Scan0; //Copy the pixel data into the bitmap structure Marshal.Copy(imagedata, 0, pixelStartAddress, imagedata.Length); imagebmp.UnlockBits(picData); bool isOnSave = false; Dispatcher.Invoke(() => isOnSave = cbSaveCameraImage.IsChecked.Value); if (isOnSave) { string propPath = ""; string prefix = "00"; Dispatcher.Invoke(() => { propPath = tbSaveCamPath.Text; prefix = tbSaveCamPrefix.Text; }); if (propPath.Length <= 0) { propPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); } var dt = DateTime.Now; var fName = $"{propPath}\\{prefix}-{dt.Month}-{dt.Day}-{dt.Hour}-{dt.Minute}-{dt.Second}_{dt.Millisecond}.jpg"; if (!Properties.Settings.Default.CameraMaxFrameOn) { imagebmp.Save(fName, ImageFormat.Jpeg); } else if (countSaved <= MaxCameraFrame) { imagebmp.Save(fName, ImageFormat.Jpeg); Dispatcher.Invoke(() => tbCurrentFrame.Text = countSaved.ToString()); countSaved++; } } Dispatcher.Invoke(() => { BitmapImage bmpImage = BitmapToImageSource(imagebmp); PictureBox1.Source = null; PictureBox1.Source = bmpImage; LogBox.Items.Insert(0, new LogBoxItem { Dt = DateTime.Now, LogText = "Получено изображение с камеры" }); }); }