Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SetDeviceGammaRamp (gdi32)
 
.
Summary

C# Signature:

[DllImport("gdi32.dll")]
public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);

User-Defined Types:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct RAMP
{
  [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
  public UInt16[] Red;
  [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
  public UInt16[] Green;
  [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
  public UInt16[] Blue;
}

Notes:

You need to define a 256 color "RAMP" to set this api. Check example for explanation.

2. Values from 1 to 256 are supported.

3. Also include this signature:

[DllImport("user32.dll")]

public static extern IntPtr GetDC(IntPtr hWnd);

Tips & Tricks:

Please add some!

Sample Code:

using System.Collections;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Test{

[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("gdi32.dll")]
public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct RAMP
{
  [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
  public UInt16[] Red;
  [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
  public UInt16[] Green;
  [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
  public UInt16[] Blue;
}

public static void SetGamma(int gamma)
{
  if (gamma <= 256 && gamma >= 1)
  {
   RAMP ramp = new RAMP();
   ramp.Red = new ushort[256];
   ramp.Green = new ushort[256];
   ramp.Blue = new ushort[256];
   for( int i=1; i<256; i++ )
   {
    int iArrayValue = i * (gamma + 128);

    if (iArrayValue > 65535)
    iArrayValue = 65535;
    ramp.Red[i] = ramp.Blue[i] = ramp.Green[i] = (ushort)iArrayValue;
   }
   SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref ramp);
  }
  }

public static void Main(string[] args)
{
  string ent = "";
  int g=0;
  while (ent != "EXIT")
   {
    Console.WriteLine("Enter new Gamma (or 'EXIT' to quit):");
    ent = Console.ReadLine();
    try{
    g=int.Parse(ent);
    SetGamma(g);
    }
    catch{
    //Here only to catch errors where input is not a number (EXIT, for example, is a string)        
    }
   }
}
}

Alternative Managed API:

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions