1 Reply Latest reply on Jan 4, 2012 12:48 PM by Paul Riggott

    Save for web by calling Export (exportDocument) from C# possible?

    Andreas Jansson Level 2

      I'm trying to call the Export function from C# and I'm having trouble to supply a valid file object.

      I picked up the old COM FileSystemObject, and tried (since that was used to work in VB6 and VBScript).

      In C# I have not yet got it to work. Any ideas?

       

      I have a piece of code making use of ExecuteAction to run a number of hardly understandable lines of code, executing Save For Web, for an image.

      I'm now trying to rewrite it to call the Export method of the document instead, since that would be so much easier to follow).

       

      This is my current test code

       

      // Using the export function (instead of hard-to-understand-output-script)
              private void SaveJpegForWebUsingExport(dynamic docRef, string filePath, int percentage)
              {
      
                  // Begin with deleting the destination image if it already existed on disk.
                  if (System.IO.File.Exists(filePath))
                  {
                      System.IO.File.Delete(filePath);
                  }
      
                  dynamic options = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.ExportOptionsSaveForWeb"));
                  options.quality = percentage;
                  options.format = 6; // JPEG = 6 (enl Object Model Viewe i Extenscript Toolkit)  .Type.GetTypeFromProgID("SaveDocumentType.JPEG");   // Save Format for the file
                  
                    // Create an empty file, to be able to referr to it using filesystemobject (is this really needed?)
                  var myFileEmpty = System.IO.File.Open(filePath,System.IO.FileMode.CreateNew);
                  myFileEmpty.Close();
      
                  var fs = new IWshRuntimeLibrary.FileSystemObject();
                  var myFile = fs.GetFile(filePath);
                
                 // ExportType.SAVEFORWEB = 2 (according to Object Model Viewe in Extenscript Toolkit)
                  docRef.Export(myFile, 2, options);
                  //docRef.Export(myFile.ParentFolder, 2, options);
      
              }
      

       

      This is the error I get, so obviously the File (I tested with folder as well) object is not recognized as a valid type by Photoshop:

       

      Error processing test1.eps: System.Runtime.InteropServices.COMException (0x80040

      4DA): Illegal argument - argument 1

      - File/Folder expected

         at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepI

      nfo& excepInfo, UInt32 argErr, String message)

         at CallSite.Target(Closure , CallSite , ComObject , Folder , Int32 , Object )

       

      Thanks,

      Andreas

        • 1. Re: Save for web by calling Export (exportDocument) from C# possible?
          Paul Riggott Level 6

          This seems to work...

           

           

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using ps = Photoshop;
          namespace ConsoleApplication1
          {
              class Program
              {
                  ps.Application app = new ps.ApplicationClass();
                  static void Main(string[] args)
                  {
                      Program mp = new Program();
                      string file = @"C:\Vids\frog.jpg";
                      mp.SaveJpegForWebUsingExport(file, 80);
                  }
                  private void SaveJpegForWebUsingExport(string filePath, int percentage)
                  {
                      // Begin with deleting the destination image if it already existed on disk.
                      if (System.IO.File.Exists(filePath))
                      {
                          System.IO.File.Delete(filePath);
                      }
                      dynamic options = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.ExportOptionsSaveForWeb"));
                      options.quality = percentage;
                      options.format = 6; // JPEG = 6 (enl Object Model Viewe i Extenscript Toolkit)  .Type.GetTypeFromProgID("SaveDocumentType.JPEG");   // Save Format for the file
                      // ExportType.SAVEFORWEB = 2 (according to Object Model Viewe in Extenscript Toolkit)
                      app.ActiveDocument.Export(filePath, 2, options);
                  }
              }
          }