根据客户要求,每次视觉取像运行完毕后,按需保存OK或NG图像,图像分两种:
1.带视觉工具运行结果图像;
2.相机取像原图,.bmp格式。
保存图像代码如下:
using System; using System.Windows.Forms; using System.IO; using Cognex.VisionPro; using Cognex.VisionPro.ToolBlock; /// <summary> /// 保存图像 /// </summary> /// <param name="image">需要保存的图片</param> /// <param name="imageNmane">图像保存名称</param> /// <param name="imagePath">图片保存路径</param> public void SaveImage(ICogImage image, string imageNmane, string imagePath) { CogImageFile mImageFile = new CogImageFile(); string path = ""; imagePath = System.IO.Directory.GetCurrentDirectory() + "\\VisionRecord\\Image\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\" + imagePath; if (!Directory.Exists(imagePath)) { Directory.CreateDirectory(imagePath); } try { if (imageNmane != "") { path = imagePath + "\\" + imageNmane + ".bmp"; } else { path = imagePath + "\\" + DateTime.Now.ToString("HHmmss") + ".bmp"; } mImageFile.Open(path, CogImageFileModeConstants.Write); mImageFile.Append(image); mImageFile.Close(); } catch (Exception ex) { MessageBox.Show("图像保存失败!"); } }