ImageProcess.cs 7.0 KB
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTest
{
   public  class ImageProcess
    {
        public static HObject Bitmap2HObjectBpp24(Bitmap bmp )
        {
            HObject ho_Image = null;
            try
            {
                HOperatorSet.GenEmptyObj(out ho_Image);
                ho_Image.Dispose();
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

                BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
                HOperatorSet.GenImageInterleaved(out ho_Image, srcBmpData.Scan0, "bgrx", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
                bmp.UnlockBits(srcBmpData);

            }
            catch (Exception ex)
            {
                ho_Image = null;
            }
            return ho_Image;
        }

        public static void Bitmap2HObjectBpp8(Bitmap bmp, out HObject image)
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

                BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

                HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
                bmp.UnlockBits(srcBmpData);
            }
            catch (Exception ex)
            {
                image = null;
            }
        }
        public static void GenertateGrayBitmap(HObject image, out Bitmap res)
        {
            HTuple hpoint, type, width, height;

            const int Alpha = 255;
            int[] ptr = new int[2];
            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;
            ptr[0] = bitmapData.Scan0.ToInt32();
            ptr[1] = hpoint.I;
            if (width % 4 == 0)
                CopyMemory(ptr[0], ptr[1], width * height * PixelSize);
            else
            {
                for (int i = 0; i < height - 1; i++)
                {
                    ptr[1] += width;
                    CopyMemory(ptr[0], ptr[1], width * PixelSize);
                    ptr[0] += bitmapData.Stride;
                }
            }
            res.UnlockBits(bitmapData);

        }

        private static void CopyMemory(int v1, int v2, HTuple hTuple)
        {
          
        }

        public static void GenertateRGBBitmap(HObject image, out Bitmap res)
        {
            HTuple hred, hgreen, hblue, type, width, height;

            HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);

            res = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

            Rectangle rect = new Rectangle(0, 0, width, height);
            BitmapData bitmapData = res.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
            //unsafe
            //{
            //    byte* bptr = (byte*)bitmapData.Scan0;
            //    byte* r = ((byte*)hred.I);
            //    byte* g = ((byte*)hgreen.I);
            //    byte* b = ((byte*)hblue.I);
            //    for (int i = 0; i < width * height; i++)
            //    {
            //        bptr[i * 4] = (b)[i];
            //        bptr[i * 4 + 1] = (g)[i];
            //        bptr[i * 4 + 2] = (r)[i];
            //        bptr[i * 4 + 3] = 255;
            //    }
            //}

            res.UnlockBits(bitmapData);

        }
        /// <summary>
        /// 将Halcon中8位灰度图转换为Bitmap图像
        /// </summary>
        /// <param name="image">Halcon中8位灰度图</param>
        /// <param name="res">.net中Bitmap图像</param>
        public static  void ConvertHalconGrayByteImageToBitmap(HObject image, out Bitmap res)
        {
            HTuple hpoint, type, width, height;


            const int Alpha = 255;
            int[] ptr = new int[2];
            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;
            ptr[0] = bitmapData.Scan0.ToInt32();
            ptr[1] = hpoint.I;
            if (width % 4 == 0)
                CopyMemory(ptr[0], ptr[1], width * height * PixelSize);
            else
            {
                for (int i = 0; i < height - 1; i++)
                {
                    ptr[1] += width;
                    CopyMemory(ptr[0], ptr[1], width * PixelSize);
                    ptr[0] += bitmapData.Stride;
                }
            }
            res.UnlockBits(bitmapData);
        }


        ///// <summary>
        ///// 将Halcon中RGB图像转换为Bitmap图像
        ///// </summary>
        ///// <param name="image">Halcon中RGB图像</param>
        ///// <param name="res">.net中Bitmap图像</param>
        //public static void GenertateRGBBitmap(HObject image, out Bitmap res)
        //{
        //    HTuple hred, hgreen, hblue, type, width, height;
        //    HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);
        //    res = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        //    Rectangle rect = new Rectangle(0, 0, width, height);
        //    BitmapData bitmapData = res.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
        //    unsafe
        //    {
        //        byte* bptr = (byte*)bitmapData.Scan0;
        //        byte* r = ((byte*)hred.I);
        //        byte* g = ((byte*)hgreen.I);
        //        byte* b = ((byte*)hblue.I);
        //        for (int i = 0; i < width * height; i++)
        //        {
        //            bptr[i * 4] = (b)[i];
        //            bptr[i * 4 + 1] = (g)[i];
        //            bptr[i * 4 + 2] = (r)[i];
        //            bptr[i * 4 + 3] = 255;
        //        }
        //    }


        //    res.UnlockBits(bitmapData);
        //}

    }
}