/// <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); } }
/// <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; } }