C# Equivalent to this code

限于喜欢 提交于 2019-12-11 03:35:03

问题


var xPos = new UnitValue( 0.5,'px') ;
var yPos = new UnitValue( 0.5,'px');
var pixPos = [ xPos, yPos ];

I have used this

Tuple<PsUnits, PsUnits> tuple = new Tuple<PsUnits,PsUnits>(xpos,ypos);

but not working for me. Any idea ??

I made a class

 public class pixpos
  {
    float XPOS;
    float YPOS;
    public float xpos
    {
        get
        {
            return this.XPOS;
        }
        set
        {
            this.XPOS = value;
        }
    }
    public float ypos
    {
        get { return this.YPOS; }
        set { this.YPOS = value; }
    }
}   
     pixpos obj = new pixpos();
                    obj.xpos = xPos;
                    obj.ypos = yPos;

its not working either i have to pass it as an argument to the Colorsamples.Add();

 Photoshop.Application appRef = default(Photoshop.Application);
var mySampler = appRef.ActiveDocument.ColorSamplers.Add(ps);

回答1:


I had a quick look at the interop and the Add method takes an object. As @icbytes implies, it takes an array so you could probably get away with an array of boxed objects. The interop uses double (not float) all over so double is probably the type you want to use.

For you own curiosity you should loop through the ColorSamplers collection and see what underlying types there are contained inside it. The collection stores objects that implement ColorSampler (which contains a SolidColorClass property) so if you know what objects implement this you can create those types to pass into the Add method.

Set the preference to pixels first to assume all values you provide are pixel-based.

Photoshop.Application appRef = default(Photoshop.Application);
appRef.Preferences.RulerUnits = PsUnits.psPixels;

foreach (ColorSampler sampler in appRef.ActiveDocument.ColorSamplers)
{
  // Check to see what underlying type a sampler is so you can try
  // and make instances of this to pass into the Add method.
  Console.WriteLine(sampler.GetType().FullName);
}

// Try add an object array of double values, based on the error message implied units could work.
// 'D' with convert the number literal to a 'double'.
appRef.ActiveDocument.ColorSamplers.Add(new object[] { 0.5D, 0.5D } );



回答2:


According to this page the add method requires an array. Passing the argument as anything else surely will cause a crash/exception:

http://cssdk.adobesites.com/sdk/1.0/docs/WebHelp/references/csawlib/com/adobe/photoshop/ColorSamplers.html



来源:https://stackoverflow.com/questions/19177693/c-sharp-equivalent-to-this-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!