Home > Net > How to adjust the screen brightness in c #?
How to adjust the screen brightness in c #?
Time:10-06
As shown in figure, recently doing a project, to reach to adjust the brightness, saw on the net, using the method of adjusting the gamma but after I used the screen appear problem, please let me know if anyone know what to do, thank you!
CodePudding user response:
Microsoft's own control the brightness of the screen the API, is indeed call SetDeviceGammaRamp () the function, there is a very detailed graphic tutorials you can look at: http://bit.ly/2EkTArr Create a new application form, drag a slider and a button, the code sent to you, you can try:
using System; using System.Collections.Generic; Using System.Com ponentModel; Using System. The Data; Using System. Drawing; Using System. Linq; using System.Text; Using System. The Threading. The Tasks; Using System. Windows. Forms; Using System. The Runtime. InteropServices; The namespace brightnesscontrol { Public partial class Form1: Form { [DllImport (" gdi32. DLL)] Private unsafe static extern bool SetDeviceGammaRamp (Int32 HDC, void * ramp). Private static bool initialized=false; Private static Int32 HDC. Private static int a; Public _click () { InitializeComponent(); } Private static void InitializeClass () { If (the initialized) return; HDC=Graphics. FromHwnd (IntPtr. Zero.) GetHdc () ToInt32 (); An initialized=true; } Public static unsafe bool SetBrightness (int brightness) { InitializeClass (); If (brightness & gt; 255) Brightness=255; If (brightness & lt; 0) Brightness=0; Short * gArray=stackalloc short [3 * 256]; Short * independence idx=gArray. For (int j=0; J & lt; 3; J++) { For (int I=0; I & lt; 256; I++) { Int arrayVal=I * (brightness + 128); If (arrayVal & gt; 65535) ArrayVal=65535; * independence idx=(short) arrayVal; Idx++; } } Bool retVal=SetDeviceGammaRamp (HDC, gArray); Return retVal; } Private void trackBar1_Scroll (object sender, EventArgs e) { } Private void button1_Click (object sender, EventArgs e) { A=trackBar1. Value; SetBrightness (a); } } }
You put the value of the slider changed give it a try