使用后期绑定从COM对象获取类/类型

时间:2014-01-13 23:40:44

标签: c# com photoshop late-binding

我遇到COM兼容性问题。这个链接已经非常有用,COM interface Photoshop compatibility issue,解释了COM兼容性问题可以通过后期绑定来解决。我试过这个,但我现在对如何使用我以前通过COM对象访问的类型感到困惑。

原始代码:

//This works with late binding
m_Application = new Photoshop.Application();
var refe = new Photoshop.ActionReference();

refe.PutProperty(m_Application.CharIDToTypeID("Prpr"), m_Application.CharIDToTypeID("NmbL"));

//This is the problem area
var ColorlendMode = Photoshop.PsBlendMode.psColorBlend;

var visibleLayers = new List<Photoshop.ArtLayer>();

后期绑定代码:

//works...
dynamic m_Application = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.Application"));
dynamic refe = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.ActionReference"));

refe.PutProperty(m_Application.CharIDToTypeID("Prpr"), m_Application.CharIDToTypeID("NmbL"));

//PROBLEM:
//Doesn't compile
Type BlendModeType = Type.GetTypeFromProgID("Photoshop.PsBlendMode").psColorBlend;

//returns null
//Type BlendModeType = Type.GetTypeFromProgID("Photoshop.PsBlendMode");
//Type artLayerType = Type.GetTypeFromProgID("Photoshop.ArtLayer");

编译erorr是:

'System.Type' does not contain a definition for 'psColorBlend'

那么如何获得我以前使用Photoshop COM对象获得的枚举PsBlendMode(在我的项目中添加为Reference)?

4 个答案:

答案 0 :(得分:3)

尝试此代码..您将获得混合模式名称为字符串类型..

 Public Enum EBlendModes
    None = 0
    psSoftLight = 13
    psScreen = 9
    psNormalBlend = 2
    psMultiply = 5
    psColorBurn = 6
    psDissolve = 3
    psHardMix = 26
    psHardLight = 14
    psDarken = 4
    psDifference = 18
    psColorBlend = 22
    psPinLight = 17
    psOverlay = 12
    psVividLight = 15
    psLighterColor = 27
    psLinearLight = 16
    psDarkerColor = 28
    psLinearDodge = 11
    psLinearBurn = 7
    psExclusion = 19
    psLuminosity = 23
    psSubtract = 29
    psPassThrough = 1
    psColorDodge = 10
    psDivide = 30
    psSaturationBlend = 21
    psLighten = 8
    psHue = 20
End Enum
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        Dim _appref = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.Application"))
        Dim _blndmod As EBlendModes = CInt(_appref.activedocument.Activelayer.blendmode)
        MsgBox(_blndmod.ToString)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

结果是:PsNormalBlend .... 你是否期待这样.............

答案 1 :(得分:0)

我认为你不能做你想要为枚举做的事情。你可以做的最接近的事情就是使用反射来遍历枚举,找到你想要的字符串。

看到这个: http://www.codeproject.com/Tips/550160/Getting-enum-value-from-another-class-via-Reflecti

尝试运行此功能,看看它是否打印出psColorBlend的值:

var fieldsArray = Type.GetTypeFromProgID("Photoshop.PsBlendMode").GetFields(BindingFlags.Public | BindingFlags.Static);

foreach (var fInfo in fieldsArray)
{
    var ulngValue = (ulong)Convert.ChangeType(fInfo.GetValue(null), typeof(ulong));
    Console.WriteLine(fInfo.Name.ToString(CultureInfo.InvariantCulture) + " : " + ulngValue.ToString(CultureInfo.InvariantCulture));
}  

答案 2 :(得分:0)

不同的想法:

只需定义自己的枚举来表示PSColorBlen并使用

public enum PsBlendMode
{
    psColorBlend = 22
}

var ColorlendMode = PsBlendMode.psColorBlend;

当你把它传递给一个电话时,你可能需要转换成一个int。

请在此处查看常量值列表:http://fs.mis.kuas.edu.tw/~wchsieh/photoshopy9.py

答案 3 :(得分:0)

你不能在没有任何文件的情况下获得混合模式值或没有选择任何图层...你必须选择一个图层然后你可以得到BlendMode值,否则你不能..因为BlendMode是一个“Artlayer {接口}“属性