以下代码将屏幕的滤色器设置为特定颜色。
我怎样才能获得屏幕的颜色?
[DllImport("GDI32.dll")]
private unsafe static extern bool SetDeviceGammaRamp(IntPtr hdc, void* ramp);
private static IntPtr hdc;
public unsafe bool SetLCDbrightness(Color c)
{
short red = c.R;
short green = c.G;
short blue = c.B;
Graphics gg = Graphics.FromHwnd(IntPtr.Zero);
hdc = gg.GetHdc();
short* gArray = stackalloc short[3 * 256];
short* idx = gArray;
short brightness = 0;
for (int j = 0; j < 3; j++)
{
if (j == 0) brightness = red;
if (j == 1) brightness = green;
if (j == 2) brightness = blue;
for (int i = 0; i < 256; i++)
{
int arrayVal = i * (brightness);
if (arrayVal > 65535) arrayVal = 65535;
*idx = (short)arrayVal;
idx++;
}
}
// For some reason, this always returns false?
bool retVal = SetDeviceGammaRamp(hdc, gArray);
gg.Dispose();
return false;
}
答案 0 :(得分:1)
我对此other post进行了调整,以形成以下答案:
[DllImport("gdi32.dll")]
public static extern int GetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct RAMP
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public UInt16[] Red;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public UInt16[] Green;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public UInt16[] Blue;
}
private Color getScreenColor()
{
RAMP r = new RAMP();
GetDeviceGammaRamp(GetDC(IntPtr.Zero), ref r);
return Color.FromArgb(r.Red[1], r.Green[1], r.Blue[1]);
}