public static async Task ApplyPreviewsAsync([NotNull] string acRoot, [NotNull] string carName, [NotNull] string source, bool resize, [CanBeNull] AcPreviewImageInformation information, IProgress <Tuple <string, double?> > progress = null, CancellationToken cancellation = default(CancellationToken)) { var files = Directory.GetFiles(source, "*.bmp"); for (var i = 0; i < files.Length; i++) { var file = files[i]; var id = Path.GetFileNameWithoutExtension(file); var skinDirectory = FileUtils.GetCarSkinDirectory(acRoot, carName, id); if (!Directory.Exists(skinDirectory)) { continue; } progress?.Report(new Tuple <string, double?>(id, (double)i / files.Length)); await Task.Run(() => { ApplyPreview(file, Path.Combine(skinDirectory, "preview.jpg"), resize, information); }, cancellation); if (cancellation.IsCancellationRequested) { return; } } try { Directory.Delete(source); } catch (Exception) { // ignored } }
public static void ApplyPreviews([NotNull] string acRoot, [NotNull] string carName, [NotNull] string source, bool resize, [CanBeNull] AcPreviewImageInformation information) { foreach (var file in Directory.GetFiles(source, "*.bmp")) { var skinDirectory = FileUtils.GetCarSkinDirectory(acRoot, carName, Path.GetFileNameWithoutExtension(file)); if (!Directory.Exists(skinDirectory)) { continue; } ApplyPreview(file, Path.Combine(skinDirectory, "preview.jpg"), resize, information); } }
public static void ApplyPreviewImageMagick(string source, string destination, double maxWidth, double maxHeight, [CanBeNull] AcPreviewImageInformation information) { using (var image = new MagickImage(source)) { if (maxWidth > 0d || maxHeight > 0d) { var k = Math.Max(maxHeight / image.Height, maxWidth / image.Width); image.Interpolate = PixelInterpolateMethod.Catrom; image.FilterType = FilterType.Lanczos; image.Sharpen(); image.Resize((int)(k * image.Width), (int)(k * image.Height)); image.Crop((int)maxWidth, (int)maxHeight, Gravity.Center); } image.Quality = 95; image.Density = new Density(96, 96); if (File.Exists(destination)) { try { File.Delete(destination); } catch (UnauthorizedAccessException) { Thread.Sleep(200); File.Delete(destination); } } var profile = new ExifProfile(); profile.SetValue(ExifTag.Software, AcToolsInformation.Name); if (information?.ExifStyle != null) { profile.SetValue(ExifTag.Artist, information.ExifStyle); } if (information?.Name != null) { profile.SetValue(ExifTag.ImageDescription, information.Name); } image.AddProfile(profile); image.Write(destination); } }
public static void ApplyPreview(string source, string destination, double maxWidth, double maxHeight, [CanBeNull] AcPreviewImageInformation information, bool keepOriginal = false) { if (File.Exists(destination)) { File.Delete(destination); } if (IsMagickSupported) { ApplyPreviewImageMagick(source, destination, maxWidth, maxHeight, information); } else { var encoder = ImageCodecInfo.GetImageDecoders().First(x => x.FormatID == ImageFormat.Jpeg.Guid); var parameters = new EncoderParameters(1) { Param = { [0] = new EncoderParameter(Encoder.Quality, 100L) } }; if (maxWidth > 0d || maxHeight > 0d) { using (var bitmap = new Bitmap((int)maxWidth, (int)maxHeight)) using (var graphics = Graphics.FromImage(bitmap)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var image = Image.FromFile(source)) { var k = Math.Max(maxHeight / image.Height, maxWidth / image.Width); graphics.DrawImage(image, (int)(0.5 * ((int)maxWidth - k * image.Width)), (int)(0.5 * ((int)maxHeight - k * image.Height)), (int)(k * image.Width), (int)(k * image.Height)); } new ExifWorks(bitmap) { Software = AcToolsInformation.Name, Description = information?.Name, Artist = information?.ExifStyle }.Dispose(); bitmap.Save(destination, encoder, parameters); } } else { using (var image = (Bitmap)Image.FromFile(source)) { image.Save(destination, encoder, parameters); } } } GCHelper.CleanUp(); if (!keepOriginal) { try { File.Delete(source); } catch (Exception) { // ignored } } }
public static void ApplyPreview(string source, string destination, bool resize, [CanBeNull] AcPreviewImageInformation information) { if (resize) { ApplyPreview(source, destination, CommonAcConsts.PreviewWidth, CommonAcConsts.PreviewHeight, information); } else { ApplyPreview(source, destination, 0, 0, information); } }