Photoshop operations in .Net

Brief:

Photoshop provides Interop assemblies that can be used in .Net and execute Photoshop commands. Photoshop has feature to record any operation and generate a js script file. This file can be used in .Net and perform any operation. We can also use existing operations provided by Photoshop like AutoContrast.

Need:

You have a bunch of pictures, but they don't look that great. You'd like to increase contrast, or improve colors. Luckily, you have Adobe Photoshop with its wonderful Auto Contrast function. No problem if it's all about tens of pictures. What if there are hundreds, or even thousands? Open every picture in Photoshop, apply adjustments, save it... Oh, it is boring. Why not just automate all these tasks and let Photoshop work hard.

How It Works:

Let's do it in eight easy steps:


1. Create a Windows Application project.
2. Add a reference to the Photoshop Object Library.
3. To use the Photoshop object in our application, import the Photoshop namespace:

using ps = Photoshop;

4. Declare the two objects we are going to work with (the Photoshop Application object and the Photoshop Document object):

var applic = new Photoshop.Application();
var doc = applic.ActiveDocument;


5. Now, we should define the folder containing our pictures and the folder for the improved pictures. Add two FolderBrowseDialog controls (input and output folders can be the same, the pictures will just be overwritten; however, it is not always such a good idea). Also, add the necessary TextBoxes and Button controls.
Add Button controls to start the process and to finish the application.
6. Open the Photoshop application when our form is loading. Set Photoshop as invisible (we still see it when it is starting, though).

applic.Visible = false;
applic.DisplayDialogs = PsDialogModes.psDisplayNoDialogs;
applic.PlaybackDisplayDialogs = PsDialogModes.psDisplayNoDialogs;


7. On the application exit event, don't forget to quit Photoshop as well.

applic.Quit();

8. Finally, the last step - process every image in the folder.

string[] files = IO.Directory.GetFiles(txtFrom.Text, "*.jpg");
string MapPath = AppDomain.CurrentDomain.BaseDirectory;
  foreach(string fl in files)
  {
    applic.Load(sbPath.ToString());
    var doc = applic.ActiveDocument;
    var currentLayer = doc.ActiveLayer();
    currentLayer.AutoContrast();
    var jpg = ps.JPEGSaveOptions();
    jpg.Quality = 8;
    doc.SaveAs(txtDest.Text + IO.Path.GetFileName(fl), jpg, False, 2);
    doc.Close();
  }

Boston Byte Grabs a Spot in Clutch’s List of Top Software Developers in Massachusetts

Boston Byte is a collective of highly-skilled and highly-professional developers dedicated to solving your technological ...