简体   繁体   English

C# 单击事件打开文件对话框,在 Photoshop 中启动文件并运行操作

[英]C# Click event to open file dialogue, launch files in photoshop and run an action

Again I find myself struggling with C# after coming from a limited vb.net knowledge base.在来自有限的 vb.net 知识库之后,我再次发现自己在 C# 中苦苦挣扎。

I am attempting to press a button on my application that will open a file dialogue and open the selected files in photoshop and run an action on them.我试图在我的应用程序上按下一个按钮,该按钮将打开一个文件对话框并在 photoshop 中打开选定的文件并对它们运行一个操作。

In VB.net all I had to do was the following...在 VB.net 中,我所要做的就是以下......

 Private launchFiles As OpenFileDialog
Private Sub OpenInPS_btn_Click(sender As Object, e As EventArgs) Handles OpenInPS_btn.Click

    launchFiles = New OpenFileDialog
    launchFiles.Title = "Select files"
    launchFiles.CheckFileExists = True
    launchFiles.Multiselect = True
    launchFiles.RestoreDirectory = False
    Dim appRef
    appRef = CreateObject("Photoshop.Application")

    If launchFiles.ShowDialog = Windows.Forms.DialogResult.OK Then
        For Each fl In launchFiles.FileNames
            appRef.Open(fl)
            appRef.DoAction("JDE Brandstore", "Render to Brandstore V3")
    End If

End Sub

However, when I attempt to convert this to C# like so...但是,当我尝试将其转换为 C# 时...

 private OpenFileDialog launchFiles;


    private void BTN_Photoshop_Click(object sender, EventArgs e)
    {
        launchFiles = new OpenFileDialog();
        launchFiles.Title = "Select files";
        launchFiles.CheckFileExists = true;
        launchFiles.Multiselect = true;
        launchFiles.RestoreDirectory = false;
        object appRef;
        appRef = CreateObject("Photoshop.Application");
        if (launchFiles.ShowDialog == Forms.DialogResult.OK)
        {
            foreach (var fl in launchFiles.FileNames)
            {
                appRef.Open(fl);
                appRef.DoAction("Job1", "CropperSpec");
            }
        }
    }

I got a whole host of errors that I don't currently understand/know how to resolve.我遇到了一大堆我目前不理解/不知道如何解决的错误。 在此处输入图像描述

Would anyone happen to know where I am going wrong and perhaps (here comes the cheeky request...) provide a working code block for me with explanation as to where I have gone wrong and what your code is doing to make it work.有谁会碰巧知道我哪里出错了,也许(这是厚颜无耻的请求......)为我提供一个工作代码块,解释我哪里出错了,以及你的代码正在做什么以使其工作。

---Update--- - -更新 - -

Thanks for the Assist @Mat J感谢@Mat J 的协助

New version of code here now launches Photoshop but I cannot get it to open any documents.此处的新代码版本现在启动 Photoshop,但我无法打开任何文档。

 private OpenFileDialog launchFiles;


    private void BTN_Photoshop_Click(object sender, EventArgs e)
    {
        launchFiles = new OpenFileDialog();
        launchFiles.Title = "Select files";
        launchFiles.CheckFileExists = true;
        launchFiles.Multiselect = true;
        launchFiles.RestoreDirectory = false;

        Type PhotoshopType = Type.GetTypeFromProgID("Photoshop.Application");
        object PhotoshopInst = Activator.CreateInstance(PhotoshopType);
        PhotoshopType.InvokeMember("Visible", BindingFlags.SetProperty, null, PhotoshopInst, new object[1] { true });
        if (launchFiles.ShowDialog() == DialogResult.OK)
        {
            foreach (var fl in launchFiles.FileNames)
            {
                PhotoshopType.open(fl);
            }
        }
    }

在此处输入图像描述 Massive thanks万分感谢

Provided you use .NET 4 or later, you can declare PhotoshopInst as dynamic :如果您使用 .NET 4 或更高版本,您可以将PhotoshopInst声明为dynamic

dynamic PhotoshopInst = Activator.CreateInstance(PhotoshopType);

Then you will be able to call methods you know the underlying type supports without the compiler complaining.然后,您将能够调用您知道底层类型支持的方法,而不会引起编译器的抱怨。

PhotoshopInst.Open(fl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM