Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / Visual Basic
Article

Automate Photoshop

Rate me:
Please Sign up or sign in to vote.
4.40/5 (10 votes)
17 Jul 2006CPOL2 min read 126.6K   3.2K   30   32
How to automate Photoshop with VB.NET.

Introduction

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 CS2 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 while we are lying on the beach?

Using the code

Let's do it in eight easy steps:

  1. Create a Windows Application project.
  2. Add a reference to the Photoshop Object Library.

    Image 1

  3. To use the Photoshop object in our application, import the Photoshop namespace:
    VB
    Imports Photoshop
  4. Declare the two objects we are going to work with (the Photoshop Application object and the Photoshop Document object):
    VB
    Dim appRef As Photoshop.Application
    Dim currentDoc As Photoshop.Document
  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.

    Image 2

  6. Open the Photoshop application when our form is loading. Set Photoshop as invisible (we still see it when it is starting, though).
    VB
    appRef = New ApplicationClass
    appRef.Visible = False
    appRef.DisplayDialogs = PsDialogModes.psDisplayNoDialogs
    appRef.PlaybackDisplayDialogs = PsDialogModes.psDisplayNoDialogs
  7. On the application exit event, don't forget to quit Photoshop as well.
    VB
    appRef.Quit()
  8. Finally, the last step - process every image in the folder.
    VB
    Dim files() As String = IO.Directory.GetFiles(txtFrom.Text, "*.jpg")
    For Each fl As String In files
       currentDoc = appRef.Open(fl)
       Dim currentLayer As Photoshop.ArtLayer = _
           CType(currentDoc.ActiveLayer, Photoshop.ArtLayer)
       currentLayer.AutoContrast()
       'currentLayer.AutoLevels()
       Dim jpeg As New Photoshop.JPEGSaveOptions
       jpeg.Quality = 8
       currentDoc.SaveAs(txtTo.Text + _
          IO.Path.GetFileName(fl), jpeg, False, 2)
       currentDoc.Close()
    Next

Enjoy your life, and when you are back, you will find all your pictures just perfect.

Points of interest

We applied only one Auto Contrast adjustment feature. Sure there is zillion possibilities in Photoshop to improve the image we could use in our application. Just do a research of the Photoshop Document object.

Photoshop has a feature called batch processing. It could be extremely useful in our automation process. However, for some unknown reason, I could not make it work.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
Lifelong software developer. From IBM mainframes to Microsoft .Net, and lots in between.
Born and raised in Vilnius, Lithuania. Today live in Toronto, Ontario. Tomorrow - who knows?

Comments and Discussions

 
AnswerRe: without photoshop [modified] Pin
technomanceraus2-Aug-06 21:10
technomanceraus2-Aug-06 21:10 
JokeRe: without photoshop [modified] Pin
Aviv239-Aug-06 15:49
Aviv239-Aug-06 15:49 
GeneralRe: without photoshop Pin
vadimas10-Aug-06 3:25
professionalvadimas10-Aug-06 3:25 
GeneralMuch better solution Pin
Sergey Alexandrovich Kryukov26-Jul-06 5:02
mvaSergey Alexandrovich Kryukov26-Jul-06 5:02 
GeneralDroplets Pin
Arkett25-Jul-06 22:21
Arkett25-Jul-06 22:21 
GeneralRe: Droplets Pin
vadimas26-Jul-06 4:27
professionalvadimas26-Jul-06 4:27 
GeneralRe: Droplets Pin
mattj126-Jul-06 8:28
mattj126-Jul-06 8:28 
GeneralRe: Droplets Pin
Zikovsky5-Sep-06 10:48
Zikovsky5-Sep-06 10:48 
Yes, this example could be done with actions, however it shows how to proceed for more complex functions that might require conditional choices. Actions can only do the same thing over and over, whereas you could imagine more intelligent choices with a custom script.
GeneralRe: Droplets Pin
Sergey Alexandrovich Kryukov10-Nov-09 7:17
mvaSergey Alexandrovich Kryukov10-Nov-09 7:17 
GeneralGreat ! Pin
LaFéeClochette25-Jul-06 2:58
LaFéeClochette25-Jul-06 2:58 
GeneralRe: Great ! Pin
vadimas25-Jul-06 4:13
professionalvadimas25-Jul-06 4:13 
GeneralRe: Great ! Pin
LaFéeClochette25-Jul-06 6:29
LaFéeClochette25-Jul-06 6:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.