Skip to content

Instantly share code, notes, and snippets.

@dataartist
Created November 30, 2015 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dataartist/e9e7a23e808c66030b16 to your computer and use it in GitHub Desktop.
Save dataartist/e9e7a23e808c66030b16 to your computer and use it in GitHub Desktop.
Program.cs
using System;
using System.Timers;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using BlinkStickDotNet;
namespace ColorUnderPointer
{
class Program
{
private static BlinkStick device;
private static Timer timer;
private static Point cursor;
private static Color color;
private static Bitmap pixel;
[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point lpPoint);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
static void Main(string[] args)
{
device = BlinkStick.FindFirst();
device.OpenDevice();
device.SetMode(2);
cursor = new Point();
pixel = new Bitmap(2, 2, PixelFormat.Format32bppArgb);
timer = new Timer(300);
timer.Elapsed += OnTimedEvent;
timer.AutoReset = true;
timer.Enabled = true;
Console.ReadLine();
device.TurnOff();
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
GetCursorPos(ref cursor);
color = GetColorAt(cursor);
device.SetColor(color.R, color.G, color.B);
}
public static Color GetColorAt(Point location)
{
using (var gd = Graphics.FromImage(pixel))
{
using (var gs = Graphics.FromHwnd(IntPtr.Zero))
{
var hSrcDC = gs.GetHdc();
var hDC = gd.GetHdc();
BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
gd.ReleaseHdc();
gs.ReleaseHdc();
}
}
return pixel.GetPixel(0, 0);
}
}
}
@dataartist
Copy link
Author

This is C# code that allows you to move the mouse pointer around the desktop and have the color under the pointer show in your BlinkStick. It requires BlickStickDotNet.dll reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment