Пример #1
0
        /// <summary>
        /// Read dimensions of the image to adjust own window settings
        /// </summary>
        /// <param name="himage"></param>
        private void SetImagePart(HalconDotNet.HImage himage)
        {
            string type;
            int    width, height;

            himage.GetImagePointer1(out type, out width, out height);
            this.SetImagePart(0, 0, height, width);
        }
        /// <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);
            }
        }
Пример #3
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);
            }
        }
 public void SetImage(HalconDotNet.HImage img)
 {
     try
     {
         Miscs.HImageToBitmap(img, out Bitmap);
         pictureZoom.Bmp = Bitmap;
         pictureZoom.FitDisplay();
     }
     catch (Exception ex)
     {
         MessageBox.Show("载入图片失败:" + ex.Message);
     }
 }
        /// <summary>
        /// Here testing all converting function.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoadImg_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog of = new Microsoft.Win32.OpenFileDialog();
            of.Filter = "Image Files(*.bmp, *.jpg, *.jpeg, *.png)|*.bmp; *.jpg; *.jpeg; *png";
            if (of.ShowDialog() == true)
            {
                bitmapimageImg = new BitmapImage();
                System.IO.FileStream fs = new System.IO.FileStream(of.FileName, System.IO.FileMode.Open);

                bitmapimageImg.BeginInit();
                bitmapimageImg.StreamSource = fs;
                bitmapimageImg.CacheOption  = BitmapCacheOption.OnLoad;
                bitmapimageImg.EndInit();

                fs.Close();

                this.wpfImage.Source = bitmapimageImg;
                // ** BitmapImage -> Bitmap (OK)
                //bitmapImg = ImageTypeConverter.Bitmapimage2Bitmap(bitmapimageImg);
                //this.pctbxDisp.Image = bitmapImg;

                // ** Bitmap -> HImage (OK)
                //hImg = ImageTypeConverter.Bitmap2HImage(bitmapImg);
                //ShowHImage2HWin(hImg);

                // ** HImage -> Bitmap OK
                //hImg = new HalconDotNet.HImage();
                //hImg.ReadImage(new HalconDotNet.HTuple(of.FileName));
                //ShowHImage2HWin(hImg);
                //bitmapImg = ImageTypeConverter.HImage2Bitmap(hImg);
                //this.pctbxDisp.Image = bitmapImg;
                // ** Bitmap -> BitmapImage
                //bitmapimageImg = ImageTypeConverter.Bitmap2Bitmapimage(bitmapImg);
                //this.wpfImage.Source = bitmapimageImg;

                // ** BitmapImage -> HImage
                hImg = ImageTypeConverter.Bitmapimage2HImage(bitmapimageImg);
                ShowHImage2HWin(hImg);

                // ** HImage -> BitmapImage
                //bitmapimageImg = ImageTypeConverter.HImage2Bitmapimage(hImg);
                //this.wpfImage.Source = bitmapimageImg;
            }
        }
        /// <summary>
        /// Convert HImage to Bitmap
        /// </summary>
        /// <param name="HImg"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap HImage2Bitmap(HalconDotNet.HImage HImg)
        {
            if (HImg == null)
            {
                return(null);
            }

            try
            {
                System.Drawing.Bitmap bmpImg;

                HalconDotNet.HTuple Channels;
                Channels = HImg.CountChannels();

                if (Channels.I == 3)
                {
                    System.Drawing.Imaging.PixelFormat pixelFmt = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
                    HalconDotNet.HTuple hred, hgreen, hblue, type, width, height;
                    HalconDotNet.HOperatorSet.GetImagePointer3(HImg, out hred, out hgreen, out hblue, out type, out width, out height);

                    bmpImg = new System.Drawing.Bitmap(width.I, height.I, pixelFmt);
                    System.Drawing.Imaging.BitmapData bitmapData = bmpImg.LockBits(new System.Drawing.Rectangle(0, 0, width.I, height.I), System.Drawing.Imaging.ImageLockMode.ReadWrite, pixelFmt);
                    unsafe
                    {
                        Console.WriteLine(bitmapData.Scan0);
                        byte *data = (byte *)bitmapData.Scan0;
                        byte *hr   = (byte *)hred.IP;
                        byte *hg   = (byte *)hgreen.IP;
                        byte *hb   = (byte *)hblue.IP;

                        for (int i = 0; i < width.I * height.I; i++)
                        {
                            *(data + (i * 3))     = (*(hb + i));
                            *(data + (i * 3) + 1) = *(hg + i);
                            *(data + (i * 3) + 2) = *(hr + i);
                        }
                    }
                    bmpImg.UnlockBits(bitmapData);
                }
                else if (Channels.I == 4)
                {
                    System.Drawing.Imaging.PixelFormat pixelFmt = System.Drawing.Imaging.PixelFormat.Format32bppRgb;
                    HalconDotNet.HTuple hred, hgreen, hblue, type, width, height;
                    HalconDotNet.HOperatorSet.GetImagePointer3(HImg, out hred, out hgreen, out hblue, out type, out width, out height);

                    bmpImg = new System.Drawing.Bitmap(width.I, height.I, pixelFmt);
                    System.Drawing.Imaging.BitmapData bitmapData = bmpImg.LockBits(new System.Drawing.Rectangle(0, 0, width.I, height.I), System.Drawing.Imaging.ImageLockMode.ReadWrite, pixelFmt);
                    unsafe
                    {
                        byte *data = (byte *)bitmapData.Scan0;
                        byte *hr   = (byte *)hred.IP;
                        byte *hg   = (byte *)hgreen.IP;
                        byte *hb   = (byte *)hblue.IP;

                        for (int i = 0; i < width.I * height.I; i++)
                        {
                            *(data + (i * 4))     = *(hb + i);
                            *(data + (i * 4) + 1) = *(hg + i);
                            *(data + (i * 4) + 2) = *(hr + i);
                            *(data + (i * 4) + 3) = 255;
                        }
                        bmpImg.UnlockBits(bitmapData);
                    }
                }
                else if (Channels.I == 1)
                {
                    System.Drawing.Imaging.PixelFormat pixelFmt = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
                    HalconDotNet.HTuple hpointer, type, width, height;
                    const int           Alpha = 255;
                    Int64[]             ptr   = new Int64[2];
                    hpointer = HImg.GetImagePointer1(out type, out width, out height);

                    bmpImg = new System.Drawing.Bitmap(width.I, height.I, pixelFmt);
                    System.Drawing.Imaging.ColorPalette pal = bmpImg.Palette;
                    for (int i = 0; i < 256; i++)
                    {
                        pal.Entries[i] = System.Drawing.Color.FromArgb(Alpha, i, i, i);
                    }

                    bmpImg.Palette = pal;
                    System.Drawing.Imaging.BitmapData bitmapData = bmpImg.LockBits(new System.Drawing.Rectangle(0, 0, width.I, height.I), System.Drawing.Imaging.ImageLockMode.ReadWrite, pixelFmt);
                    int PixelsSize = System.Drawing.Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;

                    Console.WriteLine(bitmapData.Scan0);
                    ptr[0] = (Int64)bitmapData.Scan0;
                    ptr[1] = (Int64)hpointer.IP;
                    if (width % 4 == 0)
                    {
                        CopyMemory(ptr[0], ptr[1], width * height * PixelsSize);
                    }
                    else
                    {
                        ptr[1] += width;
                        CopyMemory(ptr[0], ptr[1], width * PixelsSize);
                        ptr[0] += width;
                    }
                    bmpImg.UnlockBits(bitmapData);
                }
                else
                {
                    bmpImg = null;
                }

                return(bmpImg);
            }
            catch (HalconDotNet.HalconException ex)
            {
                Console.WriteLine("In ImageTypeConverter.HImage2Bitmap: " + ex.Message);
                return(null);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("In ImageTypeConverter.HImage2Bitmap: " + ex.Message);
                return(null);
            }
        }
Пример #7
0
 private void btnLoad_Click(object sender, EventArgs e)
 {
     HalconDotNet.HImage img = new HalconDotNet.HImage();
     img.ReadImage("D:/Rorze_Data/Projects/lena.bmp");
     hswDisplay.HalconWindow.DispObj(img);
 }
Пример #8
0
        /// <summary>
        /// Convert HImage to BitmapImage
        /// </summary>
        /// <param name="HImg"></param>
        /// <returns></returns>
        public static System.Windows.Media.Imaging.BitmapImage HImage2Bitmapimage(HalconDotNet.HImage HImg)
        {
            if (HImg == null)
            {
                return(null);
            }

            try
            {
                System.Windows.Media.Imaging.BitmapImage bmpimg = new System.Windows.Media.Imaging.BitmapImage();

                HalconDotNet.HTuple Channels;
                Channels = HImg.CountChannels();

                if (Channels.I == 1)
                {
                    System.Windows.Media.PixelFormat pixelFmt = System.Windows.Media.PixelFormats.Gray8;
                    HalconDotNet.HTuple hpointer, type, width, height;
                    hpointer = HImg.GetImagePointer1(out type, out width, out height);

                    System.Windows.Media.Imaging.WriteableBitmap MetaImg = new System.Windows.Media.Imaging.WriteableBitmap(width.I, height.I, 96, 96, pixelFmt, null);
                    MetaImg.Lock();
                    unsafe
                    {
                        byte *data    = (byte *)MetaImg.BackBuffer;
                        byte *img_ptr = (byte *)hpointer.IP;

                        for (int i = 0; i < width.I * height.I; i++)
                        {
                            *(data + i) = (*(img_ptr + i));
                        }
                    }
                    MetaImg.Unlock();

                    bmpimg = ConvertWriteablebitmapToBitmapimage(MetaImg);
                }
                else if (Channels.I == 3)
                {
                    System.Windows.Media.PixelFormat pixelFmt = System.Windows.Media.PixelFormats.Bgr24;
                    HalconDotNet.HTuple hred, hgreen, hblue, type, width, height;
                    HImg.GetImagePointer3(out hred, out hgreen, out hblue, out type, out width, out height);
                    //HalconDotNet.HOperatorSet.GetImagePointer3(HImg, out hred, out hgreen, out hblue, out type, out width, out height);

                    System.Windows.Media.Imaging.WriteableBitmap MetaImg = new System.Windows.Media.Imaging.WriteableBitmap(width.I, height.I, 96, 96, pixelFmt, null);
                    MetaImg.Lock();
                    unsafe
                    {
                        byte *data = (byte *)MetaImg.BackBuffer;
                        byte *hr   = (byte *)hred.IP;
                        byte *hg   = (byte *)hgreen.IP;
                        byte *hb   = (byte *)hblue.IP;

                        for (int i = 0; i < width.I * height.I; i++)
                        {
                            *(data + (i * 3))     = (*(hb + i));
                            *(data + (i * 3) + 1) = *(hg + i);
                            *(data + (i * 3) + 2) = *(hr + i);
                        }
                    }
                    MetaImg.Unlock();

                    bmpimg = ConvertWriteablebitmapToBitmapimage(MetaImg);
                }
                else if (Channels.I == 4)
                {
                    System.Windows.Media.PixelFormat pixelFmt = System.Windows.Media.PixelFormats.Pbgra32;
                    HalconDotNet.HTuple hred, hgreen, hblue, type, width, height;
                    HImg.GetImagePointer3(out hred, out hgreen, out hblue, out type, out width, out height);
                    //HalconDotNet.HOperatorSet.GetImagePointer3(HImg, out hred, out hgreen, out hblue, out type, out width, out height);

                    System.Windows.Media.Imaging.WriteableBitmap MetaImg = new System.Windows.Media.Imaging.WriteableBitmap(width.I, height.I, 96, 96, pixelFmt, null);
                    MetaImg.Lock();
                    unsafe
                    {
                        byte *data = (byte *)MetaImg.BackBuffer;
                        byte *hr   = (byte *)hred.IP;
                        byte *hg   = (byte *)hgreen.IP;
                        byte *hb   = (byte *)hblue.IP;

                        for (int i = 0; i < width.I * height.I; i++)
                        {
                            *(data + (i * 4))     = *(hb + i);
                            *(data + (i * 4) + 1) = *(hg + i);
                            *(data + (i * 4) + 2) = *(hr + i);
                            *(data + (i * 4) + 3) = 255;
                        }
                    }
                    MetaImg.Unlock();

                    bmpimg = ConvertWriteablebitmapToBitmapimage(MetaImg);
                }
                else
                {
                    bmpimg = null;
                }

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