5 Replies Latest reply on Apr 26, 2018 12:44 PM by SuperMerlin

    C# resize image and save

    marcinz51839902 Level 1

      Hello!

       

      I look for a sample code that`s resize the image to lower and save it with the 12 (the highest) level quality.

      I want the sample to be in .NET C#.

      I was searching over the Adobe group and I didn`t find any sample.

       

      Can you help me?

      Can you paste me some C# sample?

       

      Thank you in advance

      Marcin

        • 1. Re: C# resize image and save
          SuperMerlin Level 5
          using System;
          
          namespace PSexample
          {
              class Program
              {
                  static void Main(string[] args)
                  {
                      dynamic app = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.Application"));
                      if (app.Documents.Count == 0) return;
                      app.Preferences.RulerUnits = 1; //pixels
                      //resize to a width of 1000 pixels with constraint
                      app.ActiveDocument.ResizeImage(1000 /*width*/, null/*height*/, 300/*resolution*/,5/* ResampleMethod.BICUBICSHARPER*/);
                      string saveFile = @"c:/utils/test.jpg";
                      dynamic jpgOpts = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.JPEGSaveOptions"));
                      jpgOpts.embedColorProfile = true;
                      jpgOpts.formatOptions = 1 /* FormatOptions.STANDARDBASELINE*/;
                      jpgOpts.matte = 1 /*MatteType.NONE*/;
                      jpgOpts.quality = 12; //1-12
                      app.ActiveDocument.SaveAs(saveFile, jpgOpts, true, 2); //2= lowercase
                      app.ActiveDocument.Close(2); /* Close document */
                  }
              }
          }
          
          
          

          Late binding example.

          1 person found this helpful
          • 2. Re: C# resize image and save
            marcinz51839902 Level 1

            Ok. Thank you. I really apprieciate it.

            But how can I open a jpg image from C# code in Adobe Photoshop.

            I would like to:

            a) have the filelist array of jpg files in Visual Studio Software

            b) open the image file (based on the array of a) ) in Photoshop, resize it and save it to output resized jpg file.

             

            Can you help me with opening jpg file in Photoshop based on C# filelist array ?


            Thank you again.
            Regards

            Marcin

            • 3. Re: C# resize image and save
              SuperMerlin Level 5
              using System;
              using System.IO;
              using System.Linq;
              using System.Text.RegularExpressions;
              
              namespace PSexample
              {
                  class Program
                  {
                      static void Main(string[] args)
                      {
                          dynamic app = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.Application"));
                          Regex reg = new Regex(@"(?i)\.(jpg|jpeg)$"); //select jpg files only    
                          //change folder to suit          
                              var files = Directory.GetFiles(@"d:/a test", "*.*").Where(path => reg.IsMatch(path)).ToList();
                          app.Preferences.RulerUnits = 1; //pixels
                          foreach (string file in files)
                          {
                              app.open(file);
                              string Name = app.ActiveDocument.Name;
                              //resize to a width of 300 pixels with constraint
                              app.ActiveDocument.ResizeImage(300 /*width*/, null/*height*/, 300/*resolution*/, 5/* ResampleMethod.BICUBICSHARPER*/);
                              string saveFile = @"c:/utils/"+ Name;
                              dynamic jpgOpts = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.JPEGSaveOptions"));
                              jpgOpts.embedColorProfile = true;
                              jpgOpts.formatOptions = 1 /* FormatOptions.STANDARDBASELINE*/;
                              jpgOpts.matte = 1 /*MatteType.NONE*/;
                              jpgOpts.quality = 12; //1-12
                              app.ActiveDocument.SaveAs(saveFile, jpgOpts, true, 2); //2= lowercase
                              app.ActiveDocument.Close(2); /* Close document */
                          }
                      }
                  }
              }
              
              
              
              
              • 4. Re: C# resize image and save
                marcinz51839902 Level 1

                Thank you I had to change the Identity from "The launching user" to "The interactive user" on the properties pane for Component services/My computer/DCOM Config/Photoshop ActionReference, because I got DCOM error.

                 

                But I have one more question to you.

                How to keep aspect ration while resizing?

                 

                Regards

                Marcin

                • 5. Re: C# resize image and save
                  SuperMerlin Level 5

                  You only supply the width or the height NOT BOTH!

                  app.ActiveDocument.ResizeImage(300 /*width*/, null/*height*/, 300/*resolution*/, 5/* ResampleMethod.BICUBICSHARPER*/);

                  As you see in this case the height has been set to null.