Skip to main content
marcinz51839902
Participant
April 26, 2018
Answered

C# resize image and save

  • April 26, 2018
  • 1 reply
  • 2531 views

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

This topic has been closed for replies.
Correct answer SuperMerlin

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 */

            }

        }

    }

}

1 reply

SuperMerlin
Inspiring
April 26, 2018

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.

marcinz51839902
Participant
April 26, 2018

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

SuperMerlin
SuperMerlinCorrect answer
Inspiring
April 26, 2018

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 */

            }

        }

    }

}