Click here to Skip to main content
15,881,898 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.5K   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

 
Questioncom object not listed Pin
doc-zoidberg20-Aug-15 22:22
doc-zoidberg20-Aug-15 22:22 
Questionhow to add different version photoshop refernce at the same time Pin
Member 1146806825-Feb-15 3:30
professionalMember 1146806825-Feb-15 3:30 
AnswerRe: how to add different version photoshop refernce at the same time Pin
tamilpuyal_2816-Mar-15 5:57
tamilpuyal_2816-Mar-15 5:57 
QuestionSet the ForeGround color or the BackGround color from a Drawing.Color Pin
Member 1113113712-Feb-15 10:50
Member 1113113712-Feb-15 10:50 
AnswerRe: Set the ForeGround color or the BackGround color from a Drawing.Color Pin
vadimas13-Feb-15 4:42
professionalvadimas13-Feb-15 4:42 
Questioncreateobject("Photoshop.Application") gives ActiveX error in VB Pin
solankinkdpr29-Jan-13 2:41
solankinkdpr29-Jan-13 2:41 
Questioninternal error in photoshop elements automation Pin
cooldj_0075-Nov-12 18:36
cooldj_0075-Nov-12 18:36 
GeneralSet Photoshop as invisible when iam using it from vb.net Pin
krishnaramu10-Feb-11 1:17
krishnaramu10-Feb-11 1:17 
GeneralRe: Set Photoshop as invisible when iam using it from vb.net Pin
tamilpuyal_2814-Apr-11 2:26
tamilpuyal_2814-Apr-11 2:26 
GeneralMy vote of 5 Pin
Yogesh Babarao Shiraskar15-Jul-10 7:15
Yogesh Babarao Shiraskar15-Jul-10 7:15 
GeneralPhotoshop like image editor with a drag and drop image gallery Pin
ikbegins1-Jun-10 23:34
ikbegins1-Jun-10 23:34 
GeneralRe: Photoshop like image editor with a drag and drop image gallery Pin
tamilpuyal_284-Jun-10 6:52
tamilpuyal_284-Jun-10 6:52 
GeneralPhotoshop CS2 COM Object problems Pin
blaine landish10-Jun-09 9:40
blaine landish10-Jun-09 9:40 
GeneralRe: Photoshop CS2 COM Object problems Pin
vadimas10-Jun-09 10:34
professionalvadimas10-Jun-09 10:34 
QuestionHow to Handle Adjustment Dialog Boxes Pin
kamalraaja11-Apr-07 3:53
kamalraaja11-Apr-07 3:53 
Questionhow to undo and redo? Pin
kamalraaja21-Dec-06 20:14
kamalraaja21-Dec-06 20:14 
QuestionHow to change focus of Opened images in photoshop Pin
Samatha.S20-Dec-06 1:26
Samatha.S20-Dec-06 1:26 
Generaladd menu in photoshop by vb code Pin
farschadnaji17-Nov-06 5:58
farschadnaji17-Nov-06 5:58 
QuestionHi.. Thanks for this great code Pin
chingyen23-Aug-06 22:26
chingyen23-Aug-06 22:26 
Questionwithout photoshop Pin
tomsmaily1232-Aug-06 20:55
tomsmaily1232-Aug-06 20:55 
AnswerRe: without photoshop [modified] Pin
technomanceraus2-Aug-06 21:10
technomanceraus2-Aug-06 21:10 
No you must have the Photoshop COM objects installed thats the point it just automates Photoshop's tasks if you didn't need Photoshop installed to do this stuff then why would anyone buy it!

-= Technomancer =-

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 

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.