/// <summary> /// Uses a WPF FrameworkElement to create a keyImage /// </summary> /// <param name="builder"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="e"></param> /// <returns></returns> public static KeyBitmap FromWpfElement( this IKeyBitmapFactory builder, int width, int height, FrameworkElement e ) { //Do WPF layout process manually (because the element is not a UI element) e.Measure(new Size(width, height)); e.Arrange(new Rect(0, 0, width, height)); e.UpdateLayout(); //Render the element as bitmap RenderTargetBitmap renderer = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); renderer.Render(e); //Convert to StreamDeck compatible format var pbgra32 = new byte[width * height * 4]; renderer.CopyPixels(pbgra32, width * 4, 0); var bitmapData = ConvertPbgra32ToBgr24(pbgra32, width, height); return(new KeyBitmap(width, height, bitmapData)); }
/// <summary> /// Create a bitmap from encoded image /// </summary> /// <param name="builder"></param> /// <param name="bitmapFile"></param> /// <returns></returns> public static KeyBitmap FromFile(this IKeyBitmapFactory builder, string bitmapFile) { using (var bitmap = (Bitmap)Image.FromFile(bitmapFile)) { return(builder.FromBitmap(bitmap)); } }
/// <summary> /// Create a bitmap from encoded image stream /// </summary> /// <param name="builder"></param> /// <param name="bitmapStream"></param> /// <returns></returns> public static KeyBitmap FromStream(this IKeyBitmapFactory builder, Stream bitmapStream) { using (var bitmap = (Bitmap)Image.FromStream(bitmapStream)) { return(builder.FromBitmap(bitmap)); } }
/// <summary> /// Creates a single color (single pixel) <see cref="KeyBitmap"/> with a given color. /// </summary> /// <param name="builder">The builder that is used to create the <see cref="KeyBitmap"/></param> /// <param name="r">Red channel.</param> /// <param name="g">Green channel.</param> /// <param name="b">Blue channel.</param> /// <returns></returns> public static KeyBitmap FromRgb(this IKeyBitmapFactory builder, byte r, byte g, byte b) { //If everything is 0 (black) take a shortcut ;-) if (r == 0 && g == 0 && b == 0) { return(KeyBitmap.Black); } var buffer = new byte[3] { b, g, r }; return(new KeyBitmap(1, 1, buffer)); }
/// <summary> /// Create key bitmap from graphics commands (for example with lambda expression) /// </summary> /// <param name="builder"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="graphicsAction"></param> /// <returns></returns> public static KeyBitmap FromGraphics( this IKeyBitmapFactory builder, int width, int height, Action <Graphics> graphicsAction ) { using (var bmp = CreateKeyBitmap(width, height)) { using (var g = System.Drawing.Graphics.FromImage(bmp)) graphicsAction(g); return(builder.FromBitmap(bmp)); } }
/// <summary> /// Create key bitmap from graphics commands (for example with lambda expression) /// </summary> /// <param name="builder"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="graphicsAction"></param> /// <returns></returns> public static KeyBitmap FromGraphics( this IKeyBitmapFactory builder, int width, int height, Action <Graphics> graphicsAction ) { if (graphicsAction is null) { throw new ArgumentNullException(nameof(graphicsAction)); } using (var bmp = CreateKeyBitmap(width, height)) { using (var g = Graphics.FromImage(bmp)) { graphicsAction(g); } return(builder.FromBitmap(bmp)); } }
/// <summary> /// Creates a <see cref="KeyBitmap"/> from a given <see cref="Bitmap"/> /// </summary> /// <param name="builder"></param> /// <param name="bitmap"></param> /// <returns></returns> public static KeyBitmap FromBitmap(this IKeyBitmapFactory builder, Bitmap bitmap) { var w = bitmap.Width; var h = bitmap.Height; BitmapData data = null; try { data = bitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat); var managedBGR = new byte[w * h * 3]; unsafe { byte *bdata = (byte *)data.Scan0; if (data.PixelFormat == PixelFormat.Format24bppRgb) { for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { var ps = data.Stride * y + x * 3; var pt = (w * y + x) * 3; managedBGR[pt + 0] = bdata[ps + 0]; managedBGR[pt + 1] = bdata[ps + 1]; managedBGR[pt + 2] = bdata[ps + 2]; } } } else if (data.PixelFormat == PixelFormat.Format32bppArgb) { for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { var ps = data.Stride * y + x * 4; var pt = (w * y + x) * 3; double alpha = (double)bdata[ps + 3] / 255f; managedBGR[pt + 0] = (byte)Math.Round(bdata[ps + 0] * alpha); managedBGR[pt + 1] = (byte)Math.Round(bdata[ps + 1] * alpha); managedBGR[pt + 2] = (byte)Math.Round(bdata[ps + 2] * alpha); } } } else { throw new NotSupportedException("Unsupported pixel format"); } } return(new KeyBitmap(w, h, managedBGR)); } finally { if (data != null) { bitmap.UnlockBits(data); } } }