Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
906 views
in Technique[技术] by (71.8m points)

c# - SetDeviceGammaRamp just blinks the screen

I was trying to make an eye saver program myself in c# that is supposed to darken the screen after some time of work. In order to change the screen brightness I followed this (https://www.codeproject.com/Articles/47355/Setting-Screen-Brightness-in-C) article that uses SetDeviceGammaRamp method. My code is the following:

private unsafe void dimScreen()
    {
        var brightness = 10;

        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++;
            }
        }

        SetDeviceGammaRamp(hdc, gArray);
        Thread.Sleep(10000);
    }

However, instead of changing the brightness permanently (or at least for 10 seconds) the screen just blinks for half a second. Calling SetDeviceGammaRamp in cycle with a sleep several times doesn't change the situation, all I get is just several such blinks. If I change the brightness variable the brightness of that blink changes as well so I assume the hdc and gArray variables are assigned properly. I tried looking for other solutions but most of them use this method and no one seems to have this problem. Any ideas on what the issue might be?

UPD: It seems that the problem all along was with flux. It notices the change in gamma and resets it to the previous value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's the code in C# that can set the screen brightness. You can have a try.

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); 
       } 
   } 
} 

Actually your code has no error. How you use this dimScreen() function? Maybe you do this in a not proper logic.

I found a tutorial with pictures here: http://www.lattepanda.com/topic-f11t3020.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...