Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SetDeviceGammaRamp只会使屏幕闪烁_C# - Fatal编程技术网

C# SetDeviceGammaRamp只会使屏幕闪烁

C# SetDeviceGammaRamp只会使屏幕闪烁,c#,C#,我正试着自己用c#制作一个保护眼睛的程序,该程序应该在工作一段时间后使屏幕变暗。为了改变屏幕亮度,我遵循了使用SetDeviceGammaRamp方法的这篇()文章。我的代码如下: private unsafe void dimScreen() { var brightness = 10; short* gArray = stackalloc short[3 * 256]; short* idx = gArray; fo

我正试着自己用c#制作一个保护眼睛的程序,该程序应该在工作一段时间后使屏幕变暗。为了改变屏幕亮度,我遵循了使用SetDeviceGammaRamp方法的这篇()文章。我的代码如下:

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);
    }
private不安全的void dimScreen()
{
var亮度=10;
short*gArray=stackalloc short[3*256];
short*idx=gArray;
对于(int j=0;j<3;j++)
{
对于(int i=0;i<256;i++)
{
int arrayVal=i*(亮度+128);
如果(arrayVal>65535)
arrayVal=65535;
*idx=(短)arrayVal;
idx++;
}
}
SetDeviceGammaRamp(hdc,加里);
睡眠(10000);
}
然而,与其永久改变亮度(或至少10秒),屏幕只会闪烁半秒。在睡眠循环中多次调用SetDeviceGammaRamp并不能改变这种情况,我得到的只是几次这样的眨眼。如果我改变亮度变量,闪烁的亮度也会改变,因此我假设hdc和gArray变量分配正确。我试着寻找其他解决方案,但大多数都使用这种方法,似乎没有人有这个问题。关于这个问题有什么想法吗

UPD:似乎问题一直都与流量有关。它会注意到gamma的变化,并将其重置为以前的值。

以下是C#中的代码,可以设置屏幕亮度。你可以试一试

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); 
       } 
   } 
} 
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
名称空间亮度控制
{ 
公共部分类Form1:Form
{ 
[DllImport(“gdi32.dll”)]
专用不安全静态外部布尔设置设备Gammaramp(Int32 hdc,void*ramp);
私有静态bool initialized=false;
专用静态Int32 hdc;
私有静态INTA;
公共表格1()
{ 
初始化组件();
} 
私有静态void InitializeClass()
{ 
如果(已初始化)
返回;
hdc=Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32();
初始化=真;
} 
公共静态光亮度(整数亮度)
{ 
初始化类();
如果(亮度>255)
亮度=255;
如果(亮度<0)
亮度=0;
short*gArray=stackalloc short[3*256];
short*idx=gArray;
对于(int j=0;j<3;j++)
{ 
对于(int i=0;i<256;i++)
{ 
int arrayVal=i*(亮度+128);
如果(arrayVal>65535)
arrayVal=65535;
*idx=(短)arrayVal;
idx++;
} 
} 
bool retVal=SetDeviceGammaRamp(hdc,gArray);
返回返回;
} 
私有void trackBar1_滚动(对象发送方,事件参数e)
{ 
} 
私有无效按钮1\u单击(对象发送者,事件参数e)
{ 
a=轨迹栏1.值;
正确性(a);
} 
} 
} 
实际上,您的代码没有错误。如何使用此
dimScreen()
函数?也许你这样做的逻辑不正确


我在这里找到了一个带图片的教程:

看看Windows事件日志,视频驱动程序崩溃的可能性和自动恢复产生闪烁伪影的可能性。试试另一台机器。请注意,这不是调整屏幕亮度的好方法,例如,安全登录桌面不受影响。用代码可靠地控制亮度是一个难以实现的目标不幸的是,VESA监视器接口标准不是一个很好的标准。