コード例 #1
0
        /// <summary>
        /// Convert BitmapImage to HImage
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static HalconDotNet.HImage Bitmapimage2HImage(System.Windows.Media.Imaging.BitmapImage input)
        {
            if (input == null)
            {
                return(new HalconDotNet.HImage());
            }

            try
            {
                HalconDotNet.HImage himg = new HalconDotNet.HImage();
                System.Windows.Media.Imaging.WriteableBitmap Metadata = new System.Windows.Media.Imaging.WriteableBitmap(input);

                int    width     = input.PixelWidth;
                int    height    = input.PixelHeight;
                int    stride    = width * ((input.Format.BitsPerPixel + 7) / 8);
                byte[] imageData = new byte[height * stride];
                input.CopyPixels(imageData, stride, 0);

                IntPtr ImgPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(imageData.Length);
                System.Runtime.InteropServices.Marshal.Copy(imageData, 0, ImgPtr, imageData.Length);
                if (input.Format == System.Windows.Media.PixelFormats.Bgra32 || input.Format == System.Windows.Media.PixelFormats.Bgr32)
                {
                    himg.GenImageInterleaved(ImgPtr, "bgrx", input.PixelWidth, input.PixelHeight, -1, "byte", input.PixelWidth, input.PixelHeight, 0, 0, -1, 0);
                }
                else if (input.Format == System.Windows.Media.PixelFormats.Bgr24)
                {
                    himg.GenImageInterleaved(ImgPtr, "bgr", input.PixelWidth, input.PixelHeight, -1, "byte", input.PixelWidth, input.PixelHeight, 0, 0, -1, 0);
                }
                else if (input.Format == System.Windows.Media.PixelFormats.Rgb24)
                {
                    himg.GenImageInterleaved(ImgPtr, "rgb", input.PixelWidth, input.PixelHeight, -1, "byte", input.PixelWidth, input.PixelHeight, 0, 0, -1, 0);
                }
                else if (input.Format == System.Windows.Media.PixelFormats.Indexed8 || input.Format == System.Windows.Media.PixelFormats.Gray8)
                {
                    himg.GenImage1("byte", input.PixelWidth, input.PixelHeight, ImgPtr);
                }
                else // default: trans to color image
                {
                    System.Drawing.Bitmap Meta = ImageTypeConverter.Bitmapimage2Bitmap(input);
                    himg = ImageTypeConverter.Bitmap2HImage(Meta);
                }

                System.Runtime.InteropServices.Marshal.FreeHGlobal(ImgPtr);

                return(himg);
            }
            catch (HalconDotNet.HalconException ex)
            {
                Console.WriteLine("In ImageTypeConverter.Bitmapimage2HImage: " + ex.Message);
                return(null);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("In ImageTypeConverter.Bitmapimage2HImage: " + ex.Message);
                return(null);
            }
        }
コード例 #2
0
        /// <summary>
        /// Convert Bitmap to HImage which Halcon dot net data type
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static HalconDotNet.HImage Bitmap2HImage(System.Drawing.Bitmap input)
        {
            if (input == null)
            {
                return(new HalconDotNet.HImage());
            }

            try
            {
                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, input.Width, input.Height);

                HalconDotNet.HImage himg = new HalconDotNet.HImage();
                if (input.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
                {
                    System.Drawing.Imaging.BitmapData srcBmpData = input.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                    himg.GenImage1("byte", input.Width, input.Height, srcBmpData.Scan0);
                    input.UnlockBits(srcBmpData);
                }
                else if (input.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
                {
                    System.Drawing.Imaging.BitmapData srcBmpData = input.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    himg.GenImageInterleaved(srcBmpData.Scan0, "bgr", input.Width, input.Height, -1, "byte", 0, 0, 0, 0, -1, 0);
                    input.UnlockBits(srcBmpData);
                }
                else if (input.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                {
                    System.Drawing.Imaging.BitmapData srcBmpData = input.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, input.PixelFormat);
                    himg.GenImageInterleaved(srcBmpData.Scan0, "bgrx", input.Width, input.Height, -1, "byte", input.Width, input.Height, 0, 0, -1, 0);
                    input.UnlockBits(srcBmpData);
                }
                else // default: trans to color image
                {
                    System.Drawing.Imaging.BitmapData srcBmpData = null;
                    System.Drawing.Bitmap             MetaBmp    = new System.Drawing.Bitmap(input.Width, input.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    System.Drawing.Graphics           g          = System.Drawing.Graphics.FromImage(MetaBmp);
                    g.DrawImage(input, rect);
                    g.Dispose();
                    srcBmpData = MetaBmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    himg.GenImageInterleaved(srcBmpData.Scan0, "bgr", input.Width, input.Height, -1, "byte", 0, 0, 0, 0, -1, 0);
                    MetaBmp.UnlockBits(srcBmpData);
                }


                return(himg);
            }
            catch (HalconDotNet.HalconException ex)
            {
                Console.WriteLine("In ImageTypeConverter.Bitmap2HImage: " + ex.Message);
                return(null);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("In ImageTypeConverter.Bitmap2HImage: " + ex.Message);
                return(null);
            }
        }