public static void Create(Control control, EInkCanvas canvas, int pollingIntervalMs) { if (control.Width > canvas.Width || control.Height > canvas.Height) { throw new Exception($"Control must be equal to or smaller than the device's canvas (device canvas is {canvas.Width}x{canvas.Height})"); } var existingControl = _registeredControls.FirstOrDefault(c => c.Control == control); if (existingControl != null) { existingControl.Timer.Stop(); _registeredControls.Remove(existingControl); } var registeredControl = new RegisteredInkControl() { Control = control, Canvas = canvas, Timer = new Timer() { Interval = pollingIntervalMs } }; registeredControl.Timer.Tick += (s, e) => { var bmp = new Bitmap(canvas.Width, canvas.Height); using (Graphics gb = Graphics.FromImage(bmp)) using (Graphics gc = Graphics.FromHwnd(control.Handle)) { IntPtr hdcDest = IntPtr.Zero; IntPtr hdcSrc = IntPtr.Zero; try { hdcDest = gb.GetHdc(); hdcSrc = gc.GetHdc(); // BitBlt copies directly from the gfx buffer; gets around GDI vs DX rendering issues BitBlt(hdcDest, 0, 0, canvas.Width, canvas.Height, hdcSrc, 0, 0, SRC_COPY); } finally { if (hdcDest != IntPtr.Zero) { gb.ReleaseHdc(hdcDest); } if (hdcSrc != IntPtr.Zero) { gc.ReleaseHdc(hdcSrc); } } } canvas.UpdateSurface(bmp); }; registeredControl.Timer.Start(); _registeredControls.Add(registeredControl); }
public static void RegisterForInk(this Control control, EInkCanvas canvas, int pollingIntervalMs = 500) { RegisteredInkControl.Create(control, canvas, pollingIntervalMs); }