Saturday, May 9, 2009

Set Screen Brightness/Contrast in C#

Stumble del.icio.us Reddit MyWeb! Facebook Google bookmark

I loaded Microsoft Windows Vista Business operating System on my old Toshiba Tecra A4 laptop and then realized that my Fn key is no longer working. The software and drivers that came with my laptop were not compatible with Vista. Alas, the one thing that I missed the most was setting up the brightness using Fn+F6 key. Usually when Windows starts, my LCD screen is just so bright that I have to adjust the brightness every time I start my laptop. Now, that leisure was gone. So I thought why not give it a try in C#. Found an API on MSDN and used it here in C# to make it work for me. It's not that great a program but a stupid slider to just reduce the Gamma component to give you a feeling that brightness is reduced. Actually the contrast is reduced. See the full code here. You can just copy and paste the full code in a Form class in a new Windows project and hit F5. If one of you is interested in just the executable, let me know via your comment here so I can send you the exe if you so desire.







1 // No copyrights (c). Use it as you wish!!

2

3 using System;

4 using System.Runtime.InteropServices;

5 using System.Windows.Forms;

6

7 namespace Yours.Truly

8 {

9 // *************************************************************************

10 /// <summary>

11 /// A form class to demnstrate the brightness adjustment for computer

12 /// monitors.

13 /// </summary>

14 /// <remarks>

15 /// <para>

16 /// It uses the Interop <see cref="SetDeviceGammaRamp"/> call to set the

17 /// Gamma component to adjust the screen contrast.

18 /// </para>

19 /// <para>

20 /// The <see cref="SetDeviceGammaRamp"/> function sets the gamma ramp on

21 /// direct color display boards having drivers that support downloadable

22 /// gamma ramps in hardware.

23 /// </para>

24 /// <para>

25 /// Direct color display modes do not use color lookup tables and are

26 /// usually 16, 24, or 32 bit. Not all direct color video boards support

27 /// loadable gamma ramps. SetDeviceGammaRamp succeeds only for devices with

28 /// drivers that support downloadable gamma ramps in hardware.

29 /// </para>

30 /// </remarks>

31 public class SetBrightness : Form

32 {

33 // *********************************************************************

34 /// <summary>

35 /// A structure of gamma corrections.

36 /// </summary>

37 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]

38 private struct RAMP

39 {

40 [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]

41 public UInt16[] Red;

42 [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]

43 public UInt16[] Green;

44 [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]

45 public UInt16[] Blue;

46 }

47

48 [DllImport("gdi32.dll")]

49 private static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);

50

51 [DllImport("user32.dll")]

52 static extern IntPtr GetDC(IntPtr hWnd);

53

54 /// <summary> Required designer variable.</summary>

55 private System.ComponentModel.IContainer components = null;

56 private TrackBar m_trackBar;

57

58 private static RAMP s_ramp = new RAMP();

59

60 // *********************************************************************

61 /// <summary>

62 /// Initializes a new instance of <see cref="SetBrightness"/> class.

63 /// </summary>

64 public SetBrightness ()

65 {

66 InitializeComponent();

67 }

68

69 // *********************************************************************

70 /// <summary>

71 /// Clean up any resources being used.

72 /// </summary>

73 /// <param name="disposing">true if managed resources should be

74 /// disposed; otherwise, false.</param>

75 protected override void Dispose (bool disposing)

76 {

77 if (disposing && (components != null))

78 {

79 components.Dispose();

80 }

81 base.Dispose(disposing);

82 }

83

84 // *********************************************************************

85 /// <summary>

86 /// Handles the <see cref="TrackBar.ValueChanged"/> event for the TrackBar

87 /// control. The value of the track bar control sets the Gamma value for

88 /// the device.

89 /// </summary>

90 /// <param name="sender">The object that initiated the event.</param>

91 /// <param name="e">An <see cref="EventArgs"/> object containing event

92 /// data</param>

93 private void HandleValueChanged (object sender, EventArgs e)

94 {

95 SetGamma(m_trackBar.Value);

96 }

97

98 // *********************************************************************

99 /// <summary>

100 /// Main entry point for the application.

101 /// </summary>

102 /// <param name="args">An array of optional arguments.</param>

103 static void Main (string[] args)

104 {

105 SetBrightness mainForm = new SetBrightness();

106 Application.Run(mainForm);

107 }

108

109 // *********************************************************************

110 /// <summary>

111 /// Sets the Gamma corection at the specified <paramref name="gamma"/>

112 /// value.

113 /// </summary>

114 /// <param name="gamma">New Gamma value to be set.</param>

115 private static void SetGamma(int gamma)

116 {

117 s_ramp.Red = new ushort[256];

118 s_ramp.Green = new ushort[256];

119 s_ramp.Blue = new ushort[256];

120

121 for( int i=1; i<256; i++ )

122 {

123 // gamma is a value between 3 and 44

124 s_ramp.Red[i] = s_ramp.Green[i] = s_ramp.Blue[i] =

125 (ushort)(Math.Min(65535,

126 Math.Max(0, Math.Pow((i+1) / 256.0, gamma*0.1) * 65535 + 0.5)));

127 }

128 // Now set the value.

129 SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref s_ramp);

130 }

131

132 #region Windows Form Designer generated code

133

134 /// <summary>

135 /// Required method for Designer support - do not modify

136 /// the contents of this method with the code editor.

137 /// </summary>

138 private void InitializeComponent ()

139 {

140 this.m_trackBar = new System.Windows.Forms.TrackBar();

141 ((System.ComponentModel.ISupportInitialize)(this.m_trackBar)).BeginInit();

142 this.SuspendLayout();

143 //

144 // m_trackBar

145 //

146 this.m_trackBar.Dock = System.Windows.Forms.DockStyle.Fill;

147 this.m_trackBar.Location = new System.Drawing.Point(0,0);

148 this.m_trackBar.Maximum = 44;

149 this.m_trackBar.Minimum = 3;

150 this.m_trackBar.Name = "m_trackBar";

151 this.m_trackBar.RightToLeft = System.Windows.Forms.RightToLeft.Yes;

152 this.m_trackBar.RightToLeftLayout = true;

153 this.m_trackBar.Size = new System.Drawing.Size(277,42);

154 this.m_trackBar.TabIndex = 0;

155 this.m_trackBar.Value = 18;

156 this.m_trackBar.ValueChanged += new System.EventHandler(this.HandleValueChanged);

157 //

158 // SetBrightness

159 //

160 this.AutoScaleDimensions = new System.Drawing.SizeF(6F,13F);

161 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

162 this.ClientSize = new System.Drawing.Size(277,42);

163 this.Controls.Add(this.m_trackBar);

164 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;

165 this.MaximizeBox = false;

166 this.MinimizeBox = false;

167 this.Name = "SetBrightness";

168 this.Opacity = 0.8;

169 this.ShowIcon = false;

170 this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;

171 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

172 this.Text = "SetBrightness";

173 this.TopMost = true;

174 ((System.ComponentModel.ISupportInitialize)(this.m_trackBar)).EndInit();

175 this.ResumeLayout(false);

176 this.PerformLayout();

177

178 }

179

180 #endregion

181

182 } // end class SetBrightness

183 } // end namespace Yours.Truly


30 comments:

  1. Very nice. Question as to why you are limiting the gamma value to between 3 and 44. Did notice the SetDeviceGammaRamp fails if venture outside this range. Is this due to a limitation in the display driver? Wondering if is possible to darken the display further to almost black?

    ReplyDelete
  2. It was purely an experiment. The hit and trial exposed the fact that if you set the value to less than 3 or greater than 44, it does not make any difference. There may be a reason on such a gamma range, but I'm not sure if it's because of the display drivers. However, this very program might not work for all kinds of display drivers and may need some testing and modification in the code.

    ReplyDelete
  3. Please can you send me the exe ?

    ReplyDelete
  4. IF you can send me the exe my email is :
    arisvelouxiotis@gmail.com

    Thank you very much !

    ReplyDelete
  5. Thank you very much for your post!
    Do you know if there is any way to change brightness only on a single form instead of entire screen?

    ReplyDelete
  6. plz send me an exe file to slynny09@gmail.com. Thanks !

    ReplyDelete
  7. plz send me an open source file(C#)to
    komlllcf@hotmail.com Thanks !

    ReplyDelete
  8. time waste, its not working :-(

    ReplyDelete
  9. works fine on my computer. thx a lot!

    ReplyDelete
  10. I need This, send me it

    ReplyDelete
  11. Please send it to me
    davewalterclark@gmail.com

    ReplyDelete
  12. i really need this please send it to me
    ph3rd13_b1gm4n@yahoo.co.id

    ReplyDelete
  13. eieieie... please, this is really cool.. Nice job.. Could you send me?
    mix89@hotmail.it
    thank you so much

    ReplyDelete
  14. Hi, I need it for my project. If you could send me that, it would be excellent. thanks...

    yuriy418@mail.ru

    ReplyDelete
  15. plz send me exe file i needed it i also lost function keys my mail address : Rakeshphani@gamil.com waiting for u r mail plz

    ReplyDelete
  16. Could I have the .exe please rsmith20@o2.co.uk many thanks

    ReplyDelete
  17. can i have the source and the exe file? I can't compile the code above

    hw121693@gmail.com

    Many thanks

    ReplyDelete
  18. Can you send the project to me? My e-mail is robson_dias@compal.com

    ReplyDelete
  19. sinan.4406@yandex.com do you send project ?

    ReplyDelete
  20. Pls send me the exe file, I need it for my laptop very much.
    email : masterqdung@yahoo.com, masterqdung@gmail.com

    thanks much

    ReplyDelete
  21. Hey can you please send me the code @ harryp1992@gmail.com?

    ReplyDelete
  22. It'd be nice if this was more easily accesable

    ReplyDelete
  23. Hi there,
    can you make the program or class which can control the color temperature . for e.g
    color temperature (Example:6500k or 3000K)

    ReplyDelete
  24. Please send me an exe file to serkansusev@gmail.com Thanks !

    ReplyDelete
  25. also can copy that code because of that numbers :3

    ReplyDelete
  26. The Best Casino Roll | Casino Roll
    Our selection of casino games includes 생활 바카라 slots, 태평양 먹튀 roulette, and blackjack, including 블랙 잭 만화 bingo, 넷마블 바카라 table games, live 벳 인포 승무패 계산기 dealer games and live dealer games.

    ReplyDelete
  27. Casino Archives - Dr. mcd.com
    The 문경 출장안마 casino at the 순천 출장마사지 Quinault Beach Resort is 영천 출장샵 celebrating its 100th 김해 출장안마 birthday. The Quinault 세종특별자치 출장마사지 Beach Resort features modern rooms with plush Dec 21, 2021 · Uploaded by mcd

    ReplyDelete

Please leave your opinion...