d2phap / DXControl

🖌️ A WinForms hybrid control that supports Direct2D and GDI+ drawing
MIT License
24 stars 5 forks source link

Improve performance for GetPixelColor #5

Closed Charltsing closed 2 months ago

Charltsing commented 2 months ago

https://github.com/d2phap/ImageGlass/blob/e2045cb557300df4260e3374554efa0597bee561/Source/Components/ImageGlass.Base/BHelper/Extensions/ID2D1Bitmap1Extensions.cs#L33-L76

var startIndex = (y map.pitch) + (x 4);
var bytes = new byte[4];

Marshal.Copy((nint)(map.bits + startIndex), bytes, 0, bytes.Length); ---------- only read 4 bytes bitmap1.Unmap();

// since pixel data is D2D1_ALPHA_MODE_PREMULTIPLIED, // we need to re-calculate the color values var a = bytes[3]; var alphaPremultiplied = a / 255f;

var r = (byte)(bytes[2] / alphaPremultiplied); var g = (byte)(bytes[1] / alphaPremultiplied); var b = (byte)(bytes[0] / alphaPremultiplied);

var color = Color.FromArgb(a, r, g, b);

d2phap commented 2 months ago

Thank you, I just pushed the code