private void Process(string path) { foreach (var tool in m_Stream.Tools) { if (tool is ViDi2.IRedTool redTool) { //이미지 프로세스 using (Bitmap bitmap = new Bitmap(path)) { try { //ViDi2.VisionPro.Image image = new ViDi2.VisionPro.Image(new CogImage8Grey(bitmap)); ViDi2.IImage image = new ViDi2.FormsImage(bitmap); ViDi2.ISample sample = m_Stream.Process(image); foreach (var marking in sample.Markings) { if (marking.Value is ViDi2.IRedMarking redMarking) { #region VisionPro CogRecordDisplay에 ViDi검사 결과 변환하여 표시하기 //CogRecord 생성 CogRecord record = new CogRecord(); //record.Content = image.InternalImage; record.Content = image.Bitmap; //ViDi 검사결과를 VisionPro Record로 변환 CogGraphicCollection gc = new CogGraphicCollection(); ViDi2.VisionPro.RedViewRecord redViewRecord = new ViDi2.VisionPro.RedViewRecord(redMarking.Views[0] as ViDi2.IRedView, new ViDi2.VisionPro.Records.DefaultRedToolGraphicCreator()); if (redViewRecord.HasGraphics && redViewRecord.GraphicsVisible) { foreach (ICogRecord icg in redViewRecord.SubRecords) { gc.Add(icg.Content as ICogGraphic); } } record.SubRecords.Add(new CogRecord() { Content = gc, ContentType = typeof(CogGraphicCollection) }); //CogRecordDisplay에 Record 삽입 CogRecordDisplay.Record = record; CogRecordDisplay.Fit(); #endregion #region WPF Image control 에 ViDi 검사결과 표시 ViDi2.IImage overlay = redMarking.OverlayImage(); Bitmap result = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics graphics = Graphics.FromImage(result)) { graphics.DrawImage(bitmap, 0, 0); graphics.DrawImage(overlay.Bitmap, 0, 0); } using (var memory = new MemoryStream()) { result.Save(memory, ImageFormat.Bmp); memory.Position = 0; var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); BitmapImageSource = bitmapImage; PropertyChangedEvent(nameof(BitmapImageSource)); } #endregion } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } }
private void AssignCogRecordToDisplay(Control cogDisplay, ICogRecord cogRecord) { if (cogDisplay.Visible != true) { return; } if (cogDisplay.InvokeRequired) { cogDisplay.BeginInvoke(new _delParamCogDisplayIcogRecord(AssignCogRecordToDisplay), new object[] { cogDisplay, cogRecord }); return; } if (cogDisplay is CogRecordsDisplay) { (cogDisplay as CogRecordsDisplay).Subject = cogRecord; } else if (cogDisplay is CogDisplay) { CogDisplay cDisplay = cogDisplay as CogDisplay; try { //cogDisplay.Visible = false; if (cogRecord.SubRecords == null || cogRecord.SubRecords.Count == 0) { return; } if (cogRecord.Content != null && cogRecord.Content is List <ICogRecord> ) { List <ICogRecord> recordList = cogRecord.Content as List <ICogRecord>; CogGraphicInteractiveCollection cogGraphicInteractCollection = new CogGraphicInteractiveCollection(); foreach (ICogRecord record in recordList) { // Assign image if it is not there if ((cDisplay.Image == null || !(cDisplay.Image is CogImage8Grey)) && CogCamera != null) { // Assign image lock (CogCamera.LockCogDisplay) { cDisplay.Image = record.SubRecords[0].Content as ICogImage; cDisplay.Fit(true); } } cDisplay.InteractiveGraphics.Clear(); if (IsDisplayGraphic) { //Assign graphic if any foreach (ICogRecord subRecord in record.SubRecords[0].SubRecords) { if (subRecord.Content is CogGraphicCollection) { CogGraphicCollection contentCollect = subRecord.Content as CogGraphicCollection; if (contentCollect != null) { foreach (CogCompositeShape graphic in contentCollect) { //cDisplay.InteractiveGraphics.Add(graphic, this.Name, false); cogGraphicInteractCollection.Add(graphic); } } } if (subRecord.Content is ICogGraphicInteractive) { //cDisplay.InteractiveGraphics.Add(subRecord.Content as ICogGraphicInteractive, this.Name, false); cogGraphicInteractCollection.Add(subRecord.Content as ICogGraphicInteractive); } } if (EnableDisplayTimeLabel) { CogGraphicLabel cogLabel = new CogGraphicLabel(); cogLabel.SelectedSpaceName = "@"; cogLabel.Color = CogColorConstants.Green; cogLabel.SetXYText(TimeLabelLocX, TimeLabelLocY, DateTime.Now.ToString("HH:mm:ss:fff")); cogGraphicInteractCollection.Add(cogLabel); } cDisplay.InteractiveGraphics.AddList(cogGraphicInteractCollection, this.Name, false); //(cogDisplay as CogDisplay).Invalidate(); } } } else { // Assign image if it is not there if ((cDisplay.Image == null || !(cDisplay.Image is CogImage8Grey)) && CogCamera != null) { // Assign image lock (CogCamera.LockCogDisplay) { //(cogDisplay as CogDisplay).Image = cogRecord.SubRecords[0].Content as ICogImage; cDisplay.Image = cogRecord.SubRecords[cogRecord.SubRecords.Count - 1].Content as ICogImage; cDisplay.Fit(true); } } cDisplay.InteractiveGraphics.Clear(); if (IsDisplayGraphic) { // Assign graphic if any CogGraphicInteractiveCollection cogGraphicInteractCollection = new CogGraphicInteractiveCollection(); foreach (ICogRecord record in cogRecord.SubRecords[cogRecord.SubRecords.Count - 1].SubRecords) { if (record.Content is CogGraphicCollection) { CogGraphicCollection contentCollect = record.Content as CogGraphicCollection; foreach (CogObjectBase objBase in contentCollect) { ICogGraphicInteractive graphic = objBase as ICogGraphicInteractive; if (graphic != null) { //cDisplay.InteractiveGraphics.Add(graphic, this.Name, false); cogGraphicInteractCollection.Add(graphic); } } } else if (record.Content is ICogGraphicInteractive) { //cDisplay.InteractiveGraphics.Add(record.Content as ICogGraphicInteractive, this.Name, false); cogGraphicInteractCollection.Add(record.Content as ICogGraphicInteractive); } } if (EnableDisplayTimeLabel) { CogGraphicLabel cogLabel = new CogGraphicLabel(); cogLabel.SelectedSpaceName = "@"; cogLabel.Color = CogColorConstants.Green; cogLabel.SetXYText(TimeLabelLocX, TimeLabelLocY, DateTime.Now.ToString("HH:mm:ss:fff")); cogGraphicInteractCollection.Add(cogLabel); } cDisplay.InteractiveGraphics.AddList(cogGraphicInteractCollection, this.Name, false); //(cogDisplay as CogDisplay).Invalidate(); } } } finally { //cogDisplay.Visible = true; //cogDisplay.Update(); } //CogDisplayResult =new Bitmap((cogDisplay as CogDisplay).CreateContentBitmap(CogDisplayContentBitmapConstants.Image, null, 0)); } else { // There is no Cognex Display, do nothing } }