Click here to Skip to main content
Licence CPOL
First Posted 17 Jul 2006
Views 55,940
Bookmarked 29 times

Automate Photoshop

By | 17 Jul 2006 | Article
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.

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

  6. Open the Photoshop application when our form is loading. Set Photoshop as invisible (we still see it when it is starting, though).
    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.
    appRef.Quit()
  8. Finally, the last step - process every image in the folder.
    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)

About the Author

vadimas

Web Developer

Canada Canada

Member

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?


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSet Photoshop as invisible when iam using it from vb.net Pinmemberkrishnaramu1:17 10 Feb '11  
GeneralRe: Set Photoshop as invisible when iam using it from vb.net Pinmembertamilpuyal_282:26 14 Apr '11  
GeneralMy vote of 5 PinmemberYogesh Babarao Shiraskar7:15 15 Jul '10  
GeneralPhotoshop like image editor with a drag and drop image gallery Pinmemberikbegins23:34 1 Jun '10  
GeneralRe: Photoshop like image editor with a drag and drop image gallery Pinmembertamilpuyal_286:52 4 Jun '10  
GeneralPhotoshop CS2 COM Object problems Pinmemberblaine landish9:40 10 Jun '09  
GeneralRe: Photoshop CS2 COM Object problems Pinmembervadimas10:34 10 Jun '09  
QuestionHow to Handle Adjustment Dialog Boxes Pinmemberkamalraaja3:53 11 Apr '07  
Questionhow to undo and redo? Pinmemberkamalraaja20:14 21 Dec '06  
QuestionHow to change focus of Opened images in photoshop PinmemberSamatha.S1:26 20 Dec '06  
Generaladd menu in photoshop by vb code Pinmemberfarschadnaji5:58 17 Nov '06  
QuestionHi.. Thanks for this great code Pinmemberchingyen22:26 23 Aug '06  
Questionwithout photoshop Pinmembertomsmaily12320:55 2 Aug '06  
AnswerRe: without photoshop [modified] Pinmembertechnomanceraus21:10 2 Aug '06  
JokeRe: without photoshop [modified] PinmemberAviv2315:49 9 Aug '06  
GeneralRe: without photoshop Pinmembervadimas3:25 10 Aug '06  
GeneralMuch better solution PinmemberSAKryukov5:02 26 Jul '06  
GeneralDroplets PinmemberArkett22:21 25 Jul '06  
GeneralRe: Droplets Pinmembervadimas4:27 26 Jul '06  
GeneralRe: Droplets Pinmembermattj18:28 26 Jul '06  
GeneralRe: Droplets PinmemberZikovsky10:48 5 Sep '06  
GeneralRe: Droplets PinmemberSAKryukov7:17 10 Nov '09  
GeneralGreat ! PinmemberLaFéeClochette2:58 25 Jul '06  
GeneralRe: Great ! Pinmembervadimas4:13 25 Jul '06  
GeneralRe: Great ! PinmemberLaFéeClochette6:29 25 Jul '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 17 Jul 2006
Article Copyright 2006 by vadimas
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid