C# 改变屏幕亮度 张宇轩
2010-08-29 03:12
344 查看
using System;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
namespace wgscd
{
public static class Win32
{
[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();
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++;
}
}
//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;
}
}
}
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
namespace wgscd
{
public static class Win32
{
[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();
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++;
}
}
//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;
}
}
}
相关文章推荐
- Ubuntu 10.10 调节屏幕亮度及改变启动界面分辨率
- ubuntu12.04 无法改变 thinkpad屏幕亮度
- android 改变屏幕亮度
- C# 调节屏幕亮度
- 控件:拖动条 --- SeekBar(改变屏幕亮度)
- 视频屏幕左半部分上下滑动改变亮度,右半部分上下滑动改变声音
- Android调整屏幕亮度和改变屏幕亮度调整模式的代码
- 通过伽马矫正改变计算机屏幕亮度封装代码示例
- Android 改变屏幕亮度时需添加权限
- c# 改变无边框窗体大小 张宇轩
- 只改变当前程序android屏幕亮度
- seekbar改变屏幕亮度
- c#图像处理、图片拼接、图片裁剪、图片缩放、图上添加形状、屏幕截图、图片反色、改变图片色彩度全解
- Android中 视频屏幕左半部分上下滑动改变亮度右半部分上下滑动改变声音
- C# 调节屏幕亮度 .
- C#图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果,滤波,图像截取) 对图片的处理 : 亮度调整 抓屏 翻转 随鼠标画矩形
- Ubuntu 屏幕亮度无法改变 问题的探究
- Android应用: 改变当前屏幕背光亮度
- 改变屏幕Brightness(亮度)
- android 动态改变屏幕亮度和声音大小方式