C# Haclon 相机识别拍照回显图片控件
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace DHuaDemo
{
public partial class test2 : Form
{
public test2()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
HObject ho_Image = null;
HTuple hv_AcqHandle = null;
int snapcontorl = 0;
Thread thread1 = null;
HTuple hv_Width = new HTuple();
HTuple hv_Height = new HTuple();
HTuple hv_WindowHandle = new HTuple();
private void button1_Click(object sender, EventArgs e)
{
CameraRecognition1();
}
private void cameracontrol1()
{
while (true)
{
ho_Image.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
if (snapcontorl == 1)
{
HOperatorSet.WriteImage(ho_Image, "jpg", 0, @"D:\");
//textBox1.Text = "照片已保存";
snapcontorl = 0;
Bitmap m;
pictureBox1.Image = HObject2Bitmap8(ho_Image, out m);
}
}
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");
HOperatorSet.SetLineWidth(hWindowControl1.HalconWindow, 3);
HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height, hv_Width);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
snapcontorl = 1;
}
private bool result = true;
private void button3_Click(object sender, EventArgs e)
{
FormClose();
result = false;
}
#region
public Bitmap HObject2Bitmap8(HObject image, out Bitmap res)
{
HTuple hpoint, type, width, height;
const int Alpha = 255;
HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height);
res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
ColorPalette pal = res.Palette;
for (int i = 0; i <= 255; i++)
{ pal.Entries[i] = Color.FromArgb(Alpha, i, i, i); }
res.Palette = pal; Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
IntPtr ptr1 = bitmapData.Scan0;
IntPtr ptr2 = hpoint; int bytes = width * height;
byte[] rgbvalues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr2, rgbvalues, 0, bytes);
System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr1, bytes);
res.UnlockBits(bitmapData);
return res;
}
#endregion
#region
public static Bitmap Honject2Bitmap24(HObject hObject)
{
HTuple width0 = new HTuple();
HTuple height0 = new HTuple();
HTuple Pointer = new HTuple();
HTuple type = new HTuple();
HTuple width = new HTuple();
HTuple height = new HTuple();
HObject InterImage = new HObject();
HOperatorSet.GetImageSize(hObject, out width0, out height0);
HOperatorSet.InterleaveChannels(hObject, out InterImage, "rgb", 4 * width0, 0);
HOperatorSet.GetImagePointer1(InterImage, out Pointer, out type, out width, out height);
IntPtr ptr = Pointer;
Bitmap bitmap = new Bitmap(width / 4, height, width, PixelFormat.Format24bppRgb, ptr);
return bitmap;
}
#endregion
private void test2_FormClosing(object sender, FormClosingEventArgs e)
{
if (result)
{
FormClose();
}
}
public void FormClose()
{
if (thread1 != null)
{
thread1.Abort();
}
if (hv_AcqHandle != null)
{
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
}
}
#region
public void CameraRecognition1()
{
if (string.IsNullOrEmpty(Properties.Settings.Default.videoName1))
{
MessageBox.Show("请设置摄像机的型号参数!");
return;
}
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.OpenFramegrabber("GigEVision2", 0, 0, 0, 0, 0, 0, "progressive", -1, "default", -1, "false", "default", "38af29c7c8b7_DahuaTechnologyn_A3A20MG8pHi", 0, -1, out hv_AcqHandle);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.CloseWindow(HDevWindowStack.Pop());
}
HOperatorSet.SetWindowAttr("background_color", "black");
HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
HDevWindowStack.Push(hv_WindowHandle);
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureMode", "Timed");
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 4500.0);
MessageBox.Show("相机已打开!");
thread1 = new Thread(cameracontrol1);
thread1.IsBackground = true;
thread1.Start();
}
#endregion
}
}