我有以下代码
public static class GammaRamp
{
[DllImport("gdi32.dll")]
private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc, ushort* ramp);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);
private static Int32 hdc;
//some other things
public static unsafe void SetGammaRamp(int aBrightness, int aTemperature)
{
//...some other things calculating rgbArray but not important for the question
foreach (System.Windows.Forms.Screen screen in System.Windows.Forms.Screen.AllScreens)
{
//Get the hardware device context of the screen
hdc = CreateDC(screen.DeviceName, null, null, IntPtr.Zero).ToInt32();
SetDeviceGammaRamp(hdc, rgbArray);
}
}
}
将 CreateDC 结果转换为 ToInt32 而不是保留 IntPtr 然后调用 DeleteDC 是一个好习惯吗?我应该在 CreateDC 之后使用 DeleteDC 方法,但这是否一定在我的代码中?我希望这段代码不会出现某种内存问题。
呼如林
相关分类