简体   繁体   中英

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.

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.

In VB.net all I had to do was the following...

 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...

 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

New version of code here now launches Photoshop but I cannot get it to open any documents.

 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 :

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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