Skip to content

Automate Photoshop using C#

    To save myself 3 days worth of work I spent 2 hours reading the scripting guide for Photoshop and pounded this out at the same time. Surprisingly simple

     

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using ps = Photoshop;
    using PhotoshopTypeLibrary;
    using Excel = Microsoft.Office.Interop.Excel; 
    namespace AutoPhotoshopCard
    {
     public partial class Form1 : Form
     {
     private static Excel.Workbook MyBook = null;
     private static Excel.Application MyApp = null;
     private static Excel.Worksheet MySheet = null;
     String DB_PATH = "E:\\Downloads\\Sword Girls TCG (1).xlsx";
     ps.SolidColorClass white = new ps.SolidColorClass();
     ps.SolidColorClass black = new ps.SolidColorClass();
    
     public Form1()
     {
     InitializeComponent();
     }
     //Notes:
     //Card name font size 6 - 8pt (Centre align)
     //Card Effect Font size 4.2 - 5pt (Left Align)
     //Card Flavour Font size 3.2pt (Centre Align)
     //Card Stamina, Attack and Life Font size 14pt (Centre Align)
     private void button1_Click(object sender, EventArgs e)
     {
     photoshopRun();
     }
     public void photoshopRun()
     {
     MyApp = new Excel.Application();
     MyApp.Visible = false;
     MyBook = MyApp.Workbooks.Open(DB_PATH);
     MySheet = (Excel.Worksheet)MyBook.Sheets[1]; // Explicit cast is not required here
     int lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row; 
    
    
     //ps.SolidColorClass black = new ps.SolidColorClass();
     black.RGB.Red = 0;
     black.RGB.Green = 0;
     black.RGB.Blue = 0;
    
     //ps.SolidColorClass white = new ps.SolidColorClass();
     white.RGB.Red = 255;
     white.RGB.Green = 255;
     white.RGB.Blue = 255;
    
     ps.ApplicationClass app = new ps.ApplicationClass();
     app.Preferences.RulerUnits = ps.PsUnits.psPixels; // don't tell me this in centimeters
     app.DisplayDialogs = ps.PsDialogModes.psDisplayNoDialogs;
     app.Load("D:\\My Documents\\My Pictures\\SG Cards inserts\\character.psd");
     app.Load("D:\\My Documents\\My Pictures\\SG Cards inserts\\follower.psd");
     app.Load("D:\\My Documents\\My Pictures\\SG Cards inserts\\spell.psd");
     ps.Documents doc_arr = app.Documents;
    
     for (int i = 1; i <= lastRow; i++)
     {
     System.Array MyValues = (System.Array)MySheet.get_Range("A"+i, "P"+ i).Cells.Value;
    
     if (MyValues.GetValue(1, 6).ToString() == "Character")
     {
     app.ActiveDocument = doc_arr["character.psd"];
     character(doc_arr["character.psd"], MyValues);
     }
     else if (MyValues.GetValue(1, 6).ToString() == "Follower")
     {
     app.ActiveDocument = doc_arr["follower.psd"];
     follower(doc_arr["follower.psd"], MyValues);
     }
     else if (MyValues.GetValue(1, 6).ToString() == "Spell")
     {
     app.ActiveDocument = doc_arr["spell.psd"];
     spell(doc_arr["spell.psd"], MyValues);
     }
     textBox1.Text += MyValues.GetValue(1, 1).ToString() + " SAVED\r\n";
     }
    
    
     foreach (ps.Document doc in doc_arr)
     {
     Console.WriteLine(doc.FullName);
     Console.WriteLine( "{0} x {1}",doc.Width,doc.Height);
     textBox1.Text += doc.Name.ToString() + "\r\n";
     textBox1.Text += "\r\n" + doc.Width + " x " + doc.Height+"\r\n";
    
    
     }
     
     }
     public void character(ps.Document doc, System.Array MyValues)
     {
    
     if (MyValues.GetValue(1, 16) != null)
     {
     doc.ArtLayers["Effect"].TextItem.Contents = MyValues.GetValue(1, 16).ToString();
     doc.ArtLayers["Effect"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Effect"].TextItem.Size = 5;
     doc.ArtLayers["Effect"].TextItem.Color = black;
     }
     else
     {
     doc.ArtLayers["Effect"].TextItem.Contents = "";
     }
     doc.ArtLayers["Name"].TextItem.Contents = MyValues.GetValue(1, 1).ToString();
     doc.ArtLayers["Name"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Name"].TextItem.Size = 8;
     doc.ArtLayers["Name"].TextItem.Color = white;
    
     doc.ArtLayers["Life"].TextItem.Contents = MyValues.GetValue(1, 8).ToString();
     doc.ArtLayers["Life"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Life"].TextItem.Size = 14;
     doc.ArtLayers["Life"].TextItem.Color = white;
    
     if (MyValues.GetValue(1, 14) != null)
     {
     doc.ArtLayers["Flavour"].TextItem.Contents = MyValues.GetValue(1, 14).ToString();
     doc.ArtLayers["Flavour"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Flavour"].TextItem.Size = 3;
     doc.ArtLayers["Flavour"].TextItem.Color = black;
     }
     else
     {
     doc.ArtLayers["Flavour"].TextItem.Contents = "";
     }
     savePSD(doc, "D:\\My Documents\\My Pictures\\SG Cards inserts\\" + MyValues.GetValue(1, 4) + "\\" + MyValues.GetValue(1, 5)+".psd");
     savePNG(doc, "D:\\My Documents\\My Pictures\\SG Cards inserts\\" + MyValues.GetValue(1, 4) + "\\" + MyValues.GetValue(1, 5) + ".png");
     
     }
     public void follower(ps.Document doc, System.Array MyValues)
     {
    
     if (MyValues.GetValue(1, 16) != null)
     {
     doc.ArtLayers["Effect"].TextItem.Contents = MyValues.GetValue(1, 16).ToString();
     doc.ArtLayers["Effect"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Effect"].TextItem.Size = 5;
     doc.ArtLayers["Effect"].TextItem.Color = black;
     }
     else
     {
     doc.ArtLayers["Effect"].TextItem.Contents = "";
     }
     doc.ArtLayers["Name"].TextItem.Contents = MyValues.GetValue(1, 1).ToString();
     doc.ArtLayers["Name"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Name"].TextItem.Size = 8;
     doc.ArtLayers["Name"].TextItem.Color = white;
    
     doc.ArtLayers["Attack"].TextItem.Contents = MyValues.GetValue(1, 10).ToString();
     doc.ArtLayers["Attack"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Attack"].TextItem.Size = 14;
     doc.ArtLayers["Attack"].TextItem.Color = white;
    
     doc.ArtLayers["Stamina"].TextItem.Contents = MyValues.GetValue(1, 11).ToString();
     doc.ArtLayers["Stamina"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Stamina"].TextItem.Size = 14;
     doc.ArtLayers["Stamina"].TextItem.Color = white;
    
     if (MyValues.GetValue(1, 14) != null)
     {
     doc.ArtLayers["Flavour"].TextItem.Contents = MyValues.GetValue(1, 14).ToString();
     doc.ArtLayers["Flavour"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Flavour"].TextItem.Size = 3;
     doc.ArtLayers["Flavour"].TextItem.Color = black;
     }
     else
     {
     doc.ArtLayers["Flavour"].TextItem.Contents = "";
     }
     savePSD(doc, "D:\\My Documents\\My Pictures\\SG Cards inserts\\" + MyValues.GetValue(1, 4) + "\\" + MyValues.GetValue(1, 5) + ".psd");
     savePNG(doc, "D:\\My Documents\\My Pictures\\SG Cards inserts\\" + MyValues.GetValue(1, 4) + "\\" + MyValues.GetValue(1, 5) + ".png");
     
     }
     public void spell(ps.Document doc, System.Array MyValues)
     {
    
     if (MyValues.GetValue(1, 16) != null )
     {
     doc.ArtLayers["Effect"].TextItem.Contents = MyValues.GetValue(1, 16).ToString();
     doc.ArtLayers["Effect"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Effect"].TextItem.Size = 5;
     doc.ArtLayers["Effect"].TextItem.Color = black;
     }
     else
     {
     doc.ArtLayers["Effect"].TextItem.Contents = "";
     }
     doc.ArtLayers["Name"].TextItem.Contents = MyValues.GetValue(1, 1).ToString();
     doc.ArtLayers["Name"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Name"].TextItem.Size = 8;
     doc.ArtLayers["Name"].TextItem.Color = white;
    
     if (MyValues.GetValue(1, 14) != null)
     {
     doc.ArtLayers["Flavour"].TextItem.Contents = MyValues.GetValue(1, 14).ToString();
     doc.ArtLayers["Flavour"].TextItem.Font = "KozMinPr6N-Medium";
     doc.ArtLayers["Flavour"].TextItem.Size = 3;
     doc.ArtLayers["Flavour"].TextItem.Color = black;
     }
     else
     {
     doc.ArtLayers["Flavour"].TextItem.Contents = "";
     }
     savePSD(doc, "D:\\My Documents\\My Pictures\\SG Cards inserts\\" + MyValues.GetValue(1, 4) + "\\" + MyValues.GetValue(1, 5) + ".psd");
     savePNG(doc, "D:\\My Documents\\My Pictures\\SG Cards inserts\\" + MyValues.GetValue(1, 4) + "\\" + MyValues.GetValue(1, 5) + ".png");
     
     }
     public void savePSD(ps.Document doc, String path)
     {
     ps.PhotoshopSaveOptions options = new ps.PhotoshopSaveOptions();
     options.EmbedColorProfile = true;
     doc.SaveAs(path, options, true, ps.PsExtensionType.psLowercase);
    
     }
     public void savePNG(ps.Document doc, String path)
     {
     ps.PNGSaveOptions options = new ps.PNGSaveOptions();
     options.Interlaced = false;
     doc.SaveAs(path, options, true, ps.PsExtensionType.psLowercase);
    
     }
    
     }
    }