diff --git a/StudioClient/DownloadHelper.cs b/StudioClient/DownloadHelper.cs index 4e08a90..6015c2d 100644 --- a/StudioClient/DownloadHelper.cs +++ b/StudioClient/DownloadHelper.cs @@ -7,12 +7,12 @@ namespace SkylabStudio { public class PhotoOptions { - public List? Bgs { get; set; } + public List? Bgs { get; set; } public bool? ReturnOnError { get; set; } public PhotoOptions() { - Bgs = new List(); + Bgs = new List(); ReturnOnError = false; } } @@ -29,6 +29,19 @@ public DownloadAllPhotosResult() ErroredPhotos = new List(); } } + + public class BgImageResult + { + public string BgName { get; set; } + public Image BgImage { get; set; } + + public BgImageResult(string bgName, Image bgImage) + { + BgName = bgName; + BgImage = bgImage; + } + } + public partial class StudioClient { /// @@ -36,16 +49,16 @@ public partial class StudioClient /// /// The profile associated to the job. /// List of downloaded background images. - private async Task?> DownloadBgImages(dynamic profile) + private async Task?> DownloadBgImages(dynamic profile) { - List tempBgs = new List(); + List tempBgs = new List(); List bgPhotos = ((JArray) profile!.photos).Where(photo => photo["jobId"] != null).ToList(); foreach (dynamic bg in bgPhotos) { byte[] bgBuffer = await DownloadImageAsync(bg.originalUrl.Value); Image bgImage = Image.NewFromBuffer(bgBuffer); - tempBgs.Add(bgImage); + tempBgs.Add(new BgImageResult(bg.name.Value, bgImage)); } return tempBgs; @@ -92,7 +105,7 @@ public partial class StudioClient /// The profile associated to the job. /// List of background images. /// True if the operation is successful; otherwise, false. - private async Task DownloadReplacedBackgroundImage(string fileName, Image inputImage, string outputPath, dynamic? profile = null, List? bgs = null) + private async Task DownloadReplacedBackgroundImage(string fileName, Image inputImage, string outputPath, dynamic? profile = null, List? bgs = null) { try { @@ -108,8 +121,8 @@ private async Task DownloadReplacedBackgroundImage(string fileName, Image if (bgs != null && bgs.Count > 0){ for (int i = 0; i < bgs.Count; i++) { - string newFileName = i == 0 ? $"{Path.GetFileNameWithoutExtension(fileName)}.{outputFileType}": $"{Path.GetFileNameWithoutExtension(fileName)} ({i+1}).{outputFileType}"; - Image resizedBgImage = bgs[i].ThumbnailImage(inputImage.Width, inputImage.Height, crop: Enums.Interesting.Centre); + string newFileName = $"{Path.GetFileNameWithoutExtension(fileName)} ({bgs[i].BgName}).{outputFileType}"; + Image resizedBgImage = bgs[i].BgImage.ThumbnailImage(inputImage.Width, inputImage.Height, crop: Enums.Interesting.Centre); Image resultImage = resizedBgImage.Composite2(rgbCutout, Enums.BlendMode.Over); resultImage.WriteToFile(Path.Combine(outputPath, newFileName)); } @@ -142,7 +155,7 @@ public async Task DownloadAllPhotos(JArray photosList, List successPhotos = new List(); List erroredPhotos = new List(); - List bgs = new List(); + List bgs = new List(); try { profile = await GetProfile(profile.id.Value); @@ -234,7 +247,7 @@ public async Task> DownloadPhoto(long photoId, string output bool replaceBackground = Convert.ToBoolean(profile.replaceBackground.Value); bool isDualFileOutput = Convert.ToBoolean(profile.dualFileOutput.Value); bool enableStripPngMetadata = Convert.ToBoolean(profile.enableStripPngMetadata.Value); - List? bgs = options?.Bgs; + List? bgs = options?.Bgs; // Load output image byte[] imageBuffer = await DownloadImageAsync(photo.retouchedUrl.Value);