• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

C# resize image and save

New Here ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

2.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Apr 26, 2018 Apr 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(pat

...

Votes

Translate

Translate
Adobe
Guide ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

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

            }

        }

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines