|
|
@@ -165,7 +165,30 @@ namespace TeamAAS_VP.Views.Home
|
|
|
{
|
|
|
Tag = sr.ProceductName,
|
|
|
};
|
|
|
- //ConfigureCogDisplay(disp);
|
|
|
+
|
|
|
+ disp.HandleCreated += (s, e) =>
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ConfigureCogDisplay(disp); // 句柄创建后再配置,最稳
|
|
|
+ // 绑定 WinForms 双击事件
|
|
|
+ //disp.MouseDoubleClick += (r, w) =>
|
|
|
+ //{
|
|
|
+ // try
|
|
|
+ // {
|
|
|
+ // OnDisplayDoubleClick(disp);
|
|
|
+ // }
|
|
|
+ // catch (Exception ex)
|
|
|
+ // {
|
|
|
+ // LogHelper.WriteLogError("双击预览弹窗出错", ex);
|
|
|
+ // }
|
|
|
+ //};
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.WriteLogError("ConfigureCogDisplay 失败", ex);
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
var host = new WindowsFormsHost()
|
|
|
{
|
|
|
@@ -237,6 +260,7 @@ namespace TeamAAS_VP.Views.Home
|
|
|
disp.HorizontalScrollBar = false;
|
|
|
disp.VerticalScrollBar = false;
|
|
|
disp.AutoFit = true;
|
|
|
+ disp.AutoFitWithGraphics = true;
|
|
|
disp.BackColor = System.Drawing.SystemColors.ActiveCaption;
|
|
|
}
|
|
|
|
|
|
@@ -567,5 +591,97 @@ namespace TeamAAS_VP.Views.Home
|
|
|
}
|
|
|
}));
|
|
|
}
|
|
|
+ private void OnDisplayDoubleClick(CogRecordDisplay disp)
|
|
|
+ {
|
|
|
+ if (disp == null) return;
|
|
|
+
|
|
|
+ // 必须在 WinForms/ActiveX 线程截图
|
|
|
+ disp.BeginInvoke(new Action(() =>
|
|
|
+ {
|
|
|
+ Bitmap bmp = null;
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (disp.IsDisposed || !disp.IsHandleCreated)
|
|
|
+ return;
|
|
|
+
|
|
|
+ // 优先使用显示内容截图(带图形/record叠加)
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var uiBmp = (Bitmap)disp.CreateContentBitmap(
|
|
|
+ Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Custom);
|
|
|
+
|
|
|
+ bmp = (Bitmap)uiBmp.Clone();
|
|
|
+ uiBmp.Dispose();
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ // 如果 ActiveX 状态不允许截图,退化:尝试从 Image 转 Bitmap
|
|
|
+ bmp = TryConvertCogImageToBitmap(disp.Image);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bmp == null) return;
|
|
|
+
|
|
|
+ // 回到 WPF UI 线程弹窗
|
|
|
+ this.Dispatcher.BeginInvoke(new Action(() =>
|
|
|
+ {
|
|
|
+ //var win = new ImagePreviewWindow(bmp, $"预览:{disp.Tag ?? "Display"}");
|
|
|
+ //win.Owner = Window.GetWindow(this); // 让弹窗归属当前窗口
|
|
|
+ //win.Show();
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.WriteLogError("OnDisplayDoubleClick 截图/弹窗出错", ex);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ // 这里 bmp 不要 Dispose,因为传给窗口了
|
|
|
+ // 窗口关闭时会释放
|
|
|
+ }
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 将 VisionPro 的 ICogImage 尝试转换成 Bitmap(作为 CreateContentBitmap 失败时的兜底)
|
|
|
+ /// </summary>
|
|
|
+ private Bitmap TryConvertCogImageToBitmap(ICogImage cogImage)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (cogImage == null) return null;
|
|
|
+
|
|
|
+ // VisionPro 常用转换:CogImage8Grey / CogImage24PlanarColor / CogImage24PackedColor 等
|
|
|
+ // 这里给通用兜底:用 CogImageFileBMP 落地到内存流,再读回 Bitmap
|
|
|
+ using (var ms = new MemoryStream())
|
|
|
+ {
|
|
|
+ // 需要引用 Cognex.VisionPro.ImageFile
|
|
|
+ using (var bmpFile = new CogImageFileBMP())
|
|
|
+ {
|
|
|
+ // CogImageFileBMP 只能写文件路径,不直接写流
|
|
|
+ // 所以用临时文件方式最稳(如你不想落盘,可以另写转换器)
|
|
|
+ var tmp = Path.Combine(Path.GetTempPath(), $"vp_preview_{Guid.NewGuid():N}.bmp");
|
|
|
+ try
|
|
|
+ {
|
|
|
+ bmpFile.Open(tmp, CogImageFileModeConstants.Write);
|
|
|
+ bmpFile.Append(cogImage);
|
|
|
+ bmpFile.Close();
|
|
|
+
|
|
|
+ var bitmap = (Bitmap)Bitmap.FromFile(tmp);
|
|
|
+ return (Bitmap)bitmap.Clone();
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ try { if (File.Exists(tmp)) File.Delete(tmp); } catch { }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|