Note that there are some explanatory texts on larger screens.

plurals
  1. POcreate graphics of entire screen
    primarykey
    data
    text
    <p>I ame using a class to increase and decrease the gamma of all my screens, then i start the program and increase or decrease the gamma it works fine, but after a while (20 seconds or so) it does not work anymore, ive located the problem and it seems to be the <code>Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32();</code> i need to refresh this and then it works again. in the example code this is only done once during initialisation, but to make it work i have pasted the line inside the <code>SetBrightness()</code> method, so everytime it is refreshed. is it ok to do it like this or can i expect problems?</p> <p>This is the code:</p> <pre><code>public static class Brightness { [DllImport("gdi32.dll")] private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc, void* ramp); private static bool initialized = false; private static Int32 hdc; private static void InitializeClass() { if (initialized) return; //Get the hardware device context of the screen, we can do //this by getting the graphics object of null (IntPtr.Zero) //then getting the HDC and converting that to an Int32. hdc = Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32(); initialized = true; } public static unsafe bool SetBrightness(short brightness) { InitializeClass(); hdc = Graphics.FromHwnd(IntPtr.Zero).GetHdc().ToInt32(); if (brightness &gt; 255) brightness = 255; if (brightness &lt; 0) brightness = 0; short* gArray = stackalloc short[3 * 256]; short* 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; *idx = (short)arrayVal; idx++; } } //For some reason, this always returns false? bool retVal = SetDeviceGammaRamp(hdc, gArray); //Memory allocated through stackalloc is automatically free'd //by the CLR. return retVal; } } </code></pre> <p>This is how it is called:</p> <pre><code>short gammaValue = 128; void gammaUp_OnButtonDown(object sender, EventArgs e) { if (gammaValue &lt; 255) { gammaValue += 10; if (gammaValue &gt; 255) gammaValue = 255; Brightness.SetBrightness(gammaValue); } } void gammaDown_OnButtonDown(object sender, EventArgs e) { if (gammaValue &gt; 0) { gammaValue -= 10; if (gammaValue &lt; 0) gammaValue = 0; Brightness.SetBrightness(gammaValue); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload