/// <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); } }
static internal Media.Imaging.BitmapSource LoadRibbonButtonImage(string name, bool small = false) { const uint defaultDPI = 96; int desiredSize = small ? 16 : 32; var adjustedIconSize = desiredSize * 2; var adjustedDPI = defaultDPI * 2; var screenScale = GetRevitScreenScaleFactor(); string specificSizeName = name.Replace(".png", $"_{desiredSize}.png"); // if screen has no scaling and a specific size is provided, use that // otherwise rebuild icon for size and screen scale using (var resource = (screenScale == 1 ? Assembly.GetExecutingAssembly().GetManifestResourceStream($"RhinoInside.Revit.Resources.{specificSizeName}") : null) ?? Assembly.GetExecutingAssembly().GetManifestResourceStream($"RhinoInside.Revit.Resources.{name}")) { var baseImage = new Media.Imaging.BitmapImage(); baseImage.BeginInit(); baseImage.StreamSource = resource; baseImage.DecodePixelHeight = System.Convert.ToInt32(adjustedIconSize * screenScale); baseImage.EndInit(); resource.Seek(0, SeekOrigin.Begin); var imageWidth = baseImage.PixelWidth; var imageFormat = baseImage.Format; var imageBytePerPixel = baseImage.Format.BitsPerPixel / 8; var palette = baseImage.Palette; var stride = imageWidth * imageBytePerPixel; var arraySize = stride * imageWidth; var imageData = Array.CreateInstance(typeof(byte), arraySize); baseImage.CopyPixels(imageData, stride, 0); var imageDim = System.Convert.ToInt32(adjustedIconSize * screenScale); return(Media.Imaging.BitmapSource.Create( imageDim, imageDim, adjustedDPI * screenScale, adjustedDPI * screenScale, imageFormat, palette, imageData, stride )); } }