Adjust screen brightness using C#

前端 未结 6 1590
面向向阳花
面向向阳花 2020-12-01 11:13

How do I adjust the screen brightness in C#?

6条回答
  •  我在风中等你
    2020-12-01 12:04

    Actually you can use SetDeviceGammaRamp() to set the screen brightness in C#.

    Create a new windows form application and copy the following code. Just drag a trackbar and a button to the windows.

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    using System.Runtime.InteropServices; 
    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 Form1() 
           { 
               InitializeComponent(); 
           } 
           private static void InitializeClass() 
           { 
               if (initialized) 
                   return; 
               hdc = Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32(); 
               initialized = true; 
           } 
           public static unsafe bool SetBrightness(int brightness) 
           { 
               InitializeClass(); 
               if (brightness > 255) 
                   brightness = 255; 
               if (brightness < 0) 
                   brightness = 0; 
               short* gArray = stackalloc short[3 * 256]; 
               short* idx = gArray; 
               for (int j = 0; j < 3; j++) 
               { 
                   for (int i = 0; i < 256; i++) 
                   { 
                       int arrayVal = i * (brightness + 128); 
                       if (arrayVal > 65535) 
                           arrayVal = 65535; 
                       *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); 
           } 
       } 
    } 
    

    Maybe you need to change the max and min value of the trackbar.

    You can follow the tutorial here. More pictures and details: http://www.lattepanda.com/topic-f11t3020.html?sid=f9dc5d65cd4f2feb3c91ca41196c087e

提交回复
热议问题