Automating Photoshop With C#

2 min read

I recently dumped PhotoImpact and took the Photoshop learning curve plung… special thanks to the catalyst which was my co-workers heckling. Jerks. Don't knock it till you rock it.

I use C# on some websites to fool around with images. Cropping, resizing, filters, conversions, etc. I've coded about a dozen filters and they're okay, but not great. And they're really slow. If only I could run Photoshop filters from code…

How To Run a Photoshop From C#

Here's our goal:

  1. Open an image in Photoshop
  2. Run an action to filter the image
  3. Save the new image somewhere new
  4. Close the image

Step 1) Install Photoshop & Visual Studio

Really, if you clicked on an article called "Automating Photoshop With C#" then you've probably got step 1 done. The only real bit of information here is that I don't know of any way to do Photoshop automation without it installed.

So if you need to run the automation on a different computer (like a server), then you'll have to install Photoshop

Step 2) Cut a Hole…

Wait, wrong algorithm.

Step 2) Reference the Photoshop Assemblies

Bust open your Visual Studio solution, right-click your project, and choose "Add Reference…".

Choose the COM tab, then choose the following & click "OK".

Adobe Photoshop CS5 Object Library

Adobe Photoshop CS5 Type Library

You might have CS3 or CS4 if you're behind the times. You may also have a braided belt.

Step 3) Copy & Paste My Code

public static void RunAction(string orig, string target, string action, string group)</p>
<pre><code>ApplicationClass app = new ApplicationClass();

var doc = app.Open(orig);

app.DoAction(action, group);

var options = new BMPSaveOptions();

doc.SaveAs(target, options, true, PsExtensionType.psLowercase);



Step 4) Execute the Code

Here's how I run the code:</p>




Prologue: A Few Things To Notice

Target File Type

In the target path, I'm giving the file a "bmp" extension. This does not indicate that Photoshop should save it as a Bitmap. In fact, Photoshop will completely ignore the extension.

It determines the new filetype according to the "options" parameter passed into the SaveAs method. In this example, it's a BMPSaveOptions class, but you could use PNGSaveOptions for .png or use PhotoshopSaveOptions for .psd

Each of these classes has format specific properties like compression, palette, etc.

What is the Action Name or Group?

The action name and action group are literally the name of the action shown in Photoshop and the name of its folder. We used "Sepia" and "Default" in our example:

Where To Go From Here

Obviously your use case isn't exactly the same as mine, so feel empowered to mess with this script.

I haven't found any excellent documentation, but if I do I'll post it.

It seems that the JavaScript API is very similar to the COM api, so you may check out Adobe's JavaScript Documentation. Also you can check out my post from a few weeks ago: Automating Photoshop With JavaScript.

I've even noticed a "app.DoJavaScript()" method in C#, so you could probably just write your scripts in the (better documented, easier to debug) JavaScript then use C# to kick it off.

Leave a Reply

Your email address will not be published. Required fields are marked *