我在一个控制台c#应用程序中使用了SetDeviceGammaRamp api,它运行良好。但是当在通用Windows应用程序中使用相同时出现错误“CS1620 Argument 2必须与'ref'关键字一起传递”。
我的要求是使用滑块控件更改Gamma值。
代码区:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Runtime.InteropServices;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace Brightness
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
[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 MainPage()
{
this.InitializeComponent();
}
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);
}
}
private void Slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
Slider slider = sender as Slider;
if (slider != null)
{
int p = (int)slider.Value;
SetGamma(p);
}
}
}
}
enter code here
错误:CS1620:必须使用'ref'关键字
传递参数2我是C#和Universal Windows App的新手。 请帮忙。
答案 0 :(得分:0)
您无法使用滑块更改伽玛值,因为当您尝试更改时,它会自动向下滑动。应用程序无法参考滑动条件的值。首先尝试解决滑动问题。然后,您可以使用注入方法更改Gamma值。