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
...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
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.
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
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 */
}
}
}
}
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
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now