private void axHV_OnReceiveVideo(object sender, EventArgs e) { string strName = ""; int iSize = 1024 * 1024; IntPtr irMapFile = IntPtr.Zero; IntPtr itData = IntPtr.Zero; try { AxHVActiveX2Lib.AxHVActiveX2 axHV = sender as AxHVActiveX2Lib.AxHVActiveX2; DeviceState info = axHV.Tag as DeviceState; info.State = 0; if (_ActiveDevice != null && _ActiveDevice.IP == (axHV.Tag as DeviceState).IP) { strName = axHV.GetVideoFrameSM(_ActiveDevice.VideoID, ref iSize); irMapFile = OpenFileMapping(FILE_MAP_READ, false, strName); if (irMapFile == IntPtr.Zero) { return; } itData = MapViewOfFile(irMapFile, FILE_MAP_READ, 0, 0, 0); if (itData == IntPtr.Zero) { CloseHandle(irMapFile); return; } byte[] bData = new byte[iSize]; Marshal.Copy(itData, bData, 0, iSize); MemoryStream stream = new MemoryStream(bData); video.Image = Image.FromStream(stream); } } catch (Exception ex) { Ralid.GeneralLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex); } finally { ///完成一次接收后调用下面两个函数关闭共享内存(也就是不用内存映射的时候要关闭) UnmapViewOfFile(itData); CloseHandle(irMapFile); } }
private void axHV_OnReceivePlate(object sender, EventArgs e) { try { AxHVActiveX2Lib.AxHVActiveX2 axHV = sender as AxHVActiveX2Lib.AxHVActiveX2; int videoID = axHV.GetVideoID(); DeviceState info = axHV.Tag as DeviceState; info.State = 0; foreach (CarPlateDevice device in _Devices) { if (device.IP == info.IP && device.VideoID == videoID) { device.ResetResult(); device.CarPlate = axHV.GetPlate(); device.EventDateTime = DateTime.Now; device.PlateColor = axHV.GetPlateColor(); string dir = TempFolderManager.GetCurrentFolder(); string path; path = Path.Combine(dir, Guid.NewGuid().ToString() + ".jpg"); if (axHV.SaveSnapImage(path) == 0) { device.SnapPath = path; } else { device.SnapPath = string.Empty; } path = Path.Combine(dir, Guid.NewGuid().ToString() + ".jpg"); if (axHV.SavePlateImage(path) == 0) { device.PlatePath = path; } else { device.PlatePath = string.Empty; } Action action = delegate() { foreach (DataGridViewRow row in dataGridView1.Rows) { CarPlateDevice d = row.Tag as CarPlateDevice; if (d.IP == device.IP && d.VideoID == device.VideoID) { ShowItemOnRow(device, row); } } if (_ActiveDevice != null && _ActiveDevice.IP == (axHV.Tag as DeviceState).IP && videoID == _ActiveDevice.VideoID) { this.picPlate.Image = Properties.Resources.NoImage; if (!string.IsNullOrEmpty(device.PlatePath)) { this.picPlate.Image = Image.FromFile(device.PlatePath); } } }; if (this.InvokeRequired) { this.Invoke(action); } else { action(); } } } } catch (Exception ex) { Ralid.GeneralLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex); } }