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