SetDeviceGammaRamp api在通用windows应用中的使用

本文关键字:应用 windows SetDeviceGammaRamp api | 更新日期: 2023-09-27 18:05:30

我在一个控制台c#应用程序中使用了SetDeviceGammaRamp api,它运行得很好。但是,当在通用windows应用程序中使用相同的错误时,出现"CS1620参数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:参数2必须与'ref'关键字一起传递

我是c#和通用Windows应用程序的新手。请帮助。

SetDeviceGammaRamp api在通用windows应用中的使用

你不能使用滑块改变伽马值,因为当你试图改变它时它会自动向下滑动。应用程序不能引用滑动状态下的值。尝试先解决滑动问题。然后您可以使用注入方法更改Gamma值。