From b6461e46f408e9402b81abe0003a35ab9a4e324d Mon Sep 17 00:00:00 2001 From: SuhorukovAnton Date: Mon, 18 Oct 2021 14:24:05 +0300 Subject: [PATCH] net6: replacing System.Drawing with ImageSharp --- products/ASC.CRM/Server/ASC.CRM.csproj | 1 + .../Server/Classes/ContactPhotoManager.cs | 22 +++--- products/ASC.CRM/Server/Classes/Global.cs | 22 ++---- .../Server/Classes/OrganisationLogoManager.cs | 15 ++-- products/ASC.CRM/Server/Utils/PdfCreator.cs | 4 +- .../ASC.Files/Service/Thumbnail/Builder.cs | 19 ++--- products/ASC.People/Server/ASC.People.csproj | 1 + .../Server/Controllers/PeopleController.cs | 21 +++-- web/ASC.Web.Core/ASC.Web.Core.csproj | 1 + web/ASC.Web.Core/CommonPhotoManager.cs | 78 ++++++------------- .../Extensions/UserInfoExtension.cs | 3 +- web/ASC.Web.Core/Helpers/ImageHelpers.cs | 6 +- web/ASC.Web.Core/Users/UserPhotoManager.cs | 65 ++++++++-------- .../Users/UserPhotoThumbnailManager.cs | 68 ++++++++-------- .../Users/UserPhotoThumbnailSettings.cs | 8 +- .../WhiteLabel/TenantInfoSettings.cs | 11 +-- .../WhiteLabel/TenantWhiteLabelSettings.cs | 27 ++++--- 17 files changed, 168 insertions(+), 204 deletions(-) diff --git a/products/ASC.CRM/Server/ASC.CRM.csproj b/products/ASC.CRM/Server/ASC.CRM.csproj index 12a8a59d77..3a71070d69 100644 --- a/products/ASC.CRM/Server/ASC.CRM.csproj +++ b/products/ASC.CRM/Server/ASC.CRM.csproj @@ -42,6 +42,7 @@ + diff --git a/products/ASC.CRM/Server/Classes/ContactPhotoManager.cs b/products/ASC.CRM/Server/Classes/ContactPhotoManager.cs index a193448fbb..37e70b5908 100644 --- a/products/ASC.CRM/Server/Classes/ContactPhotoManager.cs +++ b/products/ASC.CRM/Server/Classes/ContactPhotoManager.cs @@ -26,8 +26,6 @@ using System; using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Net.Http; @@ -44,6 +42,9 @@ using ASC.Web.CRM.Configuration; using Microsoft.Extensions.Options; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats; + namespace ASC.Web.CRM.Classes { public class ResizeWorkerItem : DistributedTask @@ -274,14 +275,14 @@ namespace ASC.Web.CRM.Classes { var data = resizeWorkerItem.ImageData; using (var stream = new MemoryStream(data)) - using (var img = new Bitmap(stream)) + using (var img = Image.Load(stream, out var format)) { - var imgFormat = img.RawFormat; - if (fotoSize != img.Size) + var imgFormat = format; + if (fotoSize != img.Size()) { using (var img2 = CommonPhotoManager.DoThumbnail(img, fotoSize, false, false, false)) { - data = CommonPhotoManager.SaveToBytes(img2, Global.GetImgFormatName(imgFormat)); + data = CommonPhotoManager.SaveToBytes(img2, imgFormat); } } else @@ -610,14 +611,11 @@ namespace ASC.Web.CRM.Classes return ResizeToBigSize(imageData, tmpDirName); } - public ImageFormat CheckImgFormat(byte[] imageData) + public IImageFormat CheckImgFormat(byte[] imageData) { - using (var stream = new MemoryStream(imageData)) - using (var img = new Bitmap(stream)) + using (var img = Image.Load(imageData, out var format)) { - var format = img.RawFormat; - - if (!format.Equals(ImageFormat.Png) && !format.Equals(ImageFormat.Jpeg)) + if (!format.Name.Equals("PNG") && !format.Equals("JPEG")) throw new Exception(CRMJSResource.ErrorMessage_NotImageSupportFormat); return format; diff --git a/products/ASC.CRM/Server/Classes/Global.cs b/products/ASC.CRM/Server/Classes/Global.cs index 498feadc42..ef2eff329c 100644 --- a/products/ASC.CRM/Server/Classes/Global.cs +++ b/products/ASC.CRM/Server/Classes/Global.cs @@ -25,8 +25,6 @@ using System; -using System.Drawing; -using System.Drawing.Imaging; using System.Globalization; using System.IO; using System.Text; @@ -44,6 +42,9 @@ using ASC.Web.Studio.Core; using Microsoft.Extensions.Configuration; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats; + namespace ASC.Web.CRM.Classes { [Scope] @@ -215,25 +216,14 @@ namespace ASC.Web.CRM.Classes return br.ToArray(); } - public static string GetImgFormatName(ImageFormat format) + public static string GetImgFormatName(IImageFormat format) { - if (format.Equals(ImageFormat.Bmp)) return "bmp"; - if (format.Equals(ImageFormat.Emf)) return "emf"; - if (format.Equals(ImageFormat.Exif)) return "exif"; - if (format.Equals(ImageFormat.Gif)) return "gif"; - if (format.Equals(ImageFormat.Icon)) return "icon"; - if (format.Equals(ImageFormat.Jpeg)) return "jpeg"; - if (format.Equals(ImageFormat.MemoryBmp)) return "MemoryBMP"; - if (format.Equals(ImageFormat.Png)) return "png"; - if (format.Equals(ImageFormat.Tiff)) return "tiff"; - if (format.Equals(ImageFormat.Wmf)) return "wmf"; - - return "jpg"; + return format.Name.ToLower(); } public static byte[] SaveToBytes(Image img) { - return CommonPhotoManager.SaveToBytes(img, GetImgFormatName(img.RawFormat)); + return CommonPhotoManager.SaveToBytes(img); } private static readonly string[] Formats = new[] diff --git a/products/ASC.CRM/Server/Classes/OrganisationLogoManager.cs b/products/ASC.CRM/Server/Classes/OrganisationLogoManager.cs index bbb932eb0e..a251cbdd88 100644 --- a/products/ASC.CRM/Server/Classes/OrganisationLogoManager.cs +++ b/products/ASC.CRM/Server/Classes/OrganisationLogoManager.cs @@ -25,8 +25,6 @@ using System; -using System.Drawing; -using System.Drawing.Imaging; using System.IO; using ASC.Common; @@ -39,6 +37,9 @@ using ASC.Web.CRM.Configuration; using Microsoft.Extensions.Options; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats; + namespace ASC.Web.CRM.Classes { [Scope] @@ -91,14 +92,14 @@ namespace ASC.Web.CRM.Classes { var data = imageData; using (var stream = new MemoryStream(data)) - using (var img = new Bitmap(stream)) + using (var img = Image.Load(stream, out var format)) { - var imgFormat = img.RawFormat; - if (fotoSize != img.Size) + var imgFormat = format; + if (fotoSize != img.Size()) { using (var img2 = CommonPhotoManager.DoThumbnail(img, fotoSize, false, false, false)) { - data = CommonPhotoManager.SaveToBytes(img2, Global.GetImgFormatName(imgFormat)); + data = CommonPhotoManager.SaveToBytes(img2, imgFormat); } } else @@ -188,7 +189,7 @@ namespace ASC.Web.CRM.Classes } } - public String UploadLogo(byte[] imageData, ImageFormat imageFormat) + public String UploadLogo(byte[] imageData, IImageFormat imageFormat) { var photoPath = BuildFilePath("." + Global.GetImgFormatName(imageFormat)); diff --git a/products/ASC.CRM/Server/Utils/PdfCreator.cs b/products/ASC.CRM/Server/Utils/PdfCreator.cs index 8436a00d00..0dce247538 100644 --- a/products/ASC.CRM/Server/Utils/PdfCreator.cs +++ b/products/ASC.CRM/Server/Utils/PdfCreator.cs @@ -40,6 +40,7 @@ using ASC.Files.Core; using ASC.Web.Files.Services.DocumentService; using ICSharpCode.SharpZipLib.Zip; +using SixLabors.ImageSharp; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -389,8 +390,7 @@ namespace ASC.Web.CRM.Classes } else { - using (var stream = new MemoryStream(logo)) - using (var img = System.Drawing.Image.FromStream(stream)) + using (var img = Image.Load(logo)) { var cx = img.Width * 9525; //1px = 9525emu var cy = img.Height * 9525; //1px = 9525emu diff --git a/products/ASC.Files/Service/Thumbnail/Builder.cs b/products/ASC.Files/Service/Thumbnail/Builder.cs index b98251f3dd..f83bbe4fb0 100644 --- a/products/ASC.Files/Service/Thumbnail/Builder.cs +++ b/products/ASC.Files/Service/Thumbnail/Builder.cs @@ -17,8 +17,6 @@ using System; using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Drawing2D; using System.IO; using System.Linq; using System.Net; @@ -38,8 +36,11 @@ using ASC.Web.Files.Core; using ASC.Web.Files.Services.DocumentService; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; - +using Microsoft.Extensions.Options; + +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats.Png; + namespace ASC.Files.ThumbnailBuilder { [Singletone] @@ -323,20 +324,20 @@ namespace ASC.Files.ThumbnailBuilder private void Crop(IFileDao fileDao, File file, Stream stream) { - using (var sourceBitmap = new Bitmap(stream)) + using (var sourceImg = Image.Load(stream)) { - using (var targetBitmap = GetImageThumbnail(sourceBitmap)) + using (var targetImg = GetImageThumbnail(sourceImg)) { using (var targetStream = new MemoryStream()) { - targetBitmap.Save(targetStream, System.Drawing.Imaging.ImageFormat.Png); + targetImg.Save(targetStream, PngFormat.Instance); fileDao.SaveThumbnail(file, targetStream); } } } } - private Image GetImageThumbnail(Bitmap sourceBitmap) + private Image GetImageThumbnail(Image sourceBitmap) { //bad for small or disproportionate images //return sourceBitmap.GetThumbnailImage(config.ThumbnaillWidth, config.ThumbnaillHeight, () => false, IntPtr.Zero); @@ -366,7 +367,7 @@ namespace ASC.Files.ThumbnailBuilder var targetThumbnailSettings = new UserPhotoThumbnailSettings(point, size); - return UserPhotoThumbnailManager.GetBitmap(sourceBitmap, targetSize, targetThumbnailSettings, InterpolationMode.Bilinear); + return UserPhotoThumbnailManager.GetImage(sourceBitmap, targetSize, targetThumbnailSettings); } } } diff --git a/products/ASC.People/Server/ASC.People.csproj b/products/ASC.People/Server/ASC.People.csproj index 9d9e03c8ba..d7fe001363 100644 --- a/products/ASC.People/Server/ASC.People.csproj +++ b/products/ASC.People/Server/ASC.People.csproj @@ -25,6 +25,7 @@ + diff --git a/products/ASC.People/Server/Controllers/PeopleController.cs b/products/ASC.People/Server/Controllers/PeopleController.cs index 3cb414fe1d..7659e00a25 100644 --- a/products/ASC.People/Server/Controllers/PeopleController.cs +++ b/products/ASC.People/Server/Controllers/PeopleController.cs @@ -1,8 +1,6 @@  using System; using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Net.Http; @@ -47,6 +45,9 @@ using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats; + using SecurityContext = ASC.Core.SecurityContext; namespace ASC.Employee.Core.Controllers @@ -959,7 +960,7 @@ namespace ASC.Employee.Core.Controllers } } - catch (UnknownImageFormatException) + catch (Web.Core.Users.UnknownImageFormatException) { result.Success = false; result.Message = PeopleResource.ErrorUnknownFileImageType; @@ -2042,13 +2043,11 @@ namespace ASC.Employee.Core.Controllers private static void CheckImgFormat(byte[] data) { - ImageFormat imgFormat; - + IImageFormat imgFormat; try { - using var stream = new MemoryStream(data); - using var img = new Bitmap(stream); - imgFormat = img.RawFormat; + using var img = Image.Load(data, out var format); + imgFormat = format; } catch (OutOfMemoryException) { @@ -2056,12 +2055,12 @@ namespace ASC.Employee.Core.Controllers } catch (ArgumentException error) { - throw new UnknownImageFormatException(error); + throw new Web.Core.Users.UnknownImageFormatException(error); } - if (!imgFormat.Equals(ImageFormat.Png) && !imgFormat.Equals(ImageFormat.Jpeg)) + if (imgFormat.Name != "PNG" && imgFormat.Name != "JPEG") { - throw new UnknownImageFormatException(); + throw new Web.Core.Users.UnknownImageFormatException(); } } } diff --git a/web/ASC.Web.Core/ASC.Web.Core.csproj b/web/ASC.Web.Core/ASC.Web.Core.csproj index 0d838a3528..49684aba69 100644 --- a/web/ASC.Web.Core/ASC.Web.Core.csproj +++ b/web/ASC.Web.Core/ASC.Web.Core.csproj @@ -216,6 +216,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/web/ASC.Web.Core/CommonPhotoManager.cs b/web/ASC.Web.Core/CommonPhotoManager.cs index 5b16dc7c7f..6140715abd 100644 --- a/web/ASC.Web.Core/CommonPhotoManager.cs +++ b/web/ASC.Web.Core/CommonPhotoManager.cs @@ -24,12 +24,15 @@ */ -using System; -using System.Drawing; -using System.Drawing.Drawing2D; -using System.Drawing.Imaging; using System.IO; -using System.Linq; + +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Drawing.Processing; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; + namespace ASC.Web.Core { @@ -43,11 +46,11 @@ namespace ASC.Web.Core var realWidth = image.Width; var realHeight = image.Height; - var thumbnail = new Bitmap(width, height); + Image thumbnail; var maxSide = realWidth > realHeight ? realWidth : realHeight; var minSide = realWidth < realHeight ? realWidth : realHeight; - + var alignWidth = true; if (crop) alignWidth = (minSide == realWidth); else alignWidth = (maxSide == realWidth); @@ -65,37 +68,28 @@ namespace ASC.Web.Core if (rectangle) { + thumbnail = new Image(width, height); locationY = (int)((height / 2.0) - (finalHeigth / 2.0)); locationX = (int)((width / 2.0) - (finalWidth / 2.0)); - var rect = new Rectangle(locationX, locationY, finalWidth, finalHeigth); - - using var graphic = Graphics.FromImage(thumbnail); if (!transparent) { - graphic.Clear(Color.White); - graphic.SmoothingMode = SmoothingMode.HighQuality; + thumbnail.Mutate(x=> x.Clear(Color.White)); } - graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; - graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; - - using var wrapMode = new ImageAttributes(); - wrapMode.SetWrapMode(WrapMode.TileFlipXY); - graphic.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); + var point = new Point(locationX, locationY); + image.Mutate(y => y.Resize(finalWidth, finalHeigth)); + thumbnail.Mutate(x => x.DrawImage(image, point, 1)); } else { - thumbnail = new Bitmap(finalWidth, finalHeigth); + thumbnail = new Image(finalWidth, finalHeigth); - using var graphic = Graphics.FromImage(thumbnail); if (!transparent) { - graphic.Clear(Color.White); - graphic.SmoothingMode = SmoothingMode.HighQuality; + thumbnail.Mutate(x => x.Clear(Color.White)); } - graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; - graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; - graphic.DrawImage(image, 0, 0, finalWidth, finalHeigth); + image.Mutate(y => y.Resize(finalWidth, finalHeigth)); + thumbnail.Mutate(x => x.DrawImage(image, 1)); } return thumbnail; @@ -104,48 +98,24 @@ namespace ASC.Web.Core public static byte[] SaveToBytes(Image img) { using var memoryStream = new MemoryStream(); - img.Save(memoryStream, ImageFormat.Png); + img.Save(memoryStream, PngFormat.Instance); return memoryStream.ToArray(); } - public static byte[] SaveToBytes(Image img, string formatName) + public static byte[] SaveToBytes(Image img, IImageFormat imageFormat) { byte[] data; using (var memoryStream = new MemoryStream()) { - var encParams = new EncoderParameters(1); - encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100); - img.Save(memoryStream, GetCodecInfo(formatName), encParams); + img.Save(memoryStream, imageFormat); data = memoryStream.ToArray(); } return data; } - public static ImageCodecInfo GetCodecInfo(string formatName) + public static string GetImgFormatName(IImageFormat format) { - var mimeType = string.Format("image/{0}", formatName); - if (mimeType == "image/jpg") mimeType = "image/jpeg"; - var encoders = ImageCodecInfo.GetImageEncoders(); - var encoder = encoders.FirstOrDefault(e => e.MimeType.Equals(mimeType, StringComparison.InvariantCultureIgnoreCase)); - if (encoder != null) - { - return encoder; - } - return 0 < encoders.Length ? encoders[0] : null; - } - - public static string GetImgFormatName(ImageFormat format) - { - if (format.Equals(ImageFormat.Bmp)) return "bmp"; - if (format.Equals(ImageFormat.Emf)) return "emf"; - if (format.Equals(ImageFormat.Exif)) return "exif"; - if (format.Equals(ImageFormat.Gif)) return "gif"; - if (format.Equals(ImageFormat.Icon)) return "icon"; - if (format.Equals(ImageFormat.Jpeg)) return "jpeg"; - if (format.Equals(ImageFormat.Png)) return "png"; - if (format.Equals(ImageFormat.Tiff)) return "tiff"; - if (format.Equals(ImageFormat.Wmf)) return "wmf"; - return "jpg"; + return format.Name.ToLower(); } } } diff --git a/web/ASC.Web.Core/Extensions/UserInfoExtension.cs b/web/ASC.Web.Core/Extensions/UserInfoExtension.cs index 9094d4ce9a..603170b1e1 100644 --- a/web/ASC.Web.Core/Extensions/UserInfoExtension.cs +++ b/web/ASC.Web.Core/Extensions/UserInfoExtension.cs @@ -26,12 +26,13 @@ using System; using System.Collections.Generic; -using System.Drawing; using System.Text; using ASC.Web.Core.Users; using ASC.Web.Studio.Utility; +using SixLabors.ImageSharp; + namespace ASC.Core.Users { public static class UserInfoExtension diff --git a/web/ASC.Web.Core/Helpers/ImageHelpers.cs b/web/ASC.Web.Core/Helpers/ImageHelpers.cs index 4d6d995d76..be78bcc018 100644 --- a/web/ASC.Web.Core/Helpers/ImageHelpers.cs +++ b/web/ASC.Web.Core/Helpers/ImageHelpers.cs @@ -23,10 +23,7 @@ * */ - -using System.Drawing; -using System.Drawing.Drawing2D; -using System.Drawing.Imaging; +/* using System.IO; using ASC.Data.Storage; @@ -532,3 +529,4 @@ namespace ASC.Web.Studio.Helpers } } } +*/ \ No newline at end of file diff --git a/web/ASC.Web.Core/Users/UserPhotoManager.cs b/web/ASC.Web.Core/Users/UserPhotoManager.cs index e6f64e9925..ee5da48645 100644 --- a/web/ASC.Web.Core/Users/UserPhotoManager.cs +++ b/web/ASC.Web.Core/Users/UserPhotoManager.cs @@ -27,9 +27,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Drawing2D; -using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text.RegularExpressions; @@ -46,6 +43,10 @@ using ASC.Web.Core.Utility.Skins; using Microsoft.Extensions.Options; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Processing; + namespace ASC.Web.Core.Users { [Transient] @@ -350,7 +351,7 @@ namespace ASC.Web.Core.Users public bool UserHasAvatar(Guid userID) { var path = GetPhotoAbsoluteWebPath(userID); - var fileName = Path.GetFileName(path); + var fileName = System.IO.Path.GetFileName(path); return fileName != _defaultAvatar; } @@ -595,18 +596,17 @@ namespace ASC.Web.Core.Users SettingsManager.SaveForUser(settings, userId); } - private byte[] TryParseImage(byte[] data, long maxFileSize, Size maxsize, out ImageFormat imgFormat, out int width, out int height) + private byte[] TryParseImage(byte[] data, long maxFileSize, Size maxsize, out IImageFormat imgFormat, out int width, out int height) { if (data == null || data.Length <= 0) throw new UnknownImageFormatException(); if (maxFileSize != -1 && data.Length > maxFileSize) throw new ImageSizeLimitException(); - data = ImageHelper.RotateImageByExifOrientationData(data, Log); + //data = ImageHelper.RotateImageByExifOrientationData(data, Log); try { - using var stream = new MemoryStream(data); - using var img = new Bitmap(stream); - imgFormat = img.RawFormat; + using var img = Image.Load(data ,out var format); + imgFormat = format; width = img.Width; height = img.Height; var maxWidth = maxsize.Width; @@ -643,16 +643,16 @@ namespace ASC.Web.Core.Users height = maxHeight; } + var tmpW = width; + var tmpH = height; #endregion + using Image destRound = img.Clone(x => x.Resize(new ResizeOptions + { + Size = new Size(tmpW, tmpH), + Mode = ResizeMode.Stretch + })); - using var b = new Bitmap(width, height); - using var gTemp = Graphics.FromImage(b); - gTemp.InterpolationMode = InterpolationMode.HighQualityBicubic; - gTemp.PixelOffsetMode = PixelOffsetMode.HighQuality; - gTemp.SmoothingMode = SmoothingMode.HighQuality; - gTemp.DrawImage(img, 0, 0, width, height); - - data = CommonPhotoManager.SaveToBytes(b); + data = CommonPhotoManager.SaveToBytes(destRound); } return data; } @@ -704,13 +704,13 @@ namespace ASC.Web.Core.Users { var data = item.Data; using var stream = new MemoryStream(data); - using var img = Image.FromStream(stream); - var imgFormat = img.RawFormat; - if (item.Size != img.Size) + using var img = Image.Load(stream, out var format); + var imgFormat = format; + if (item.Size != img.Size()) { using var img2 = item.Settings.IsDefault ? CommonPhotoManager.DoThumbnail(img, item.Size, true, true, true) : - UserPhotoThumbnailManager.GetBitmap(img, item.Size, item.Settings); + UserPhotoThumbnailManager.GetImage(img, item.Size, item.Settings); data = CommonPhotoManager.SaveToBytes(img2); } else @@ -768,8 +768,8 @@ namespace ASC.Web.Core.Users if (store.IsFile(_tempDomainName, fileName)) { using var s = store.GetReadStream(_tempDomainName, fileName); - using var img = Image.FromStream(s); - var imgFormat = img.RawFormat; + using var img = Image.Load(s, out var format); + var imgFormat = format; byte[] data; if (img.Width != newWidth || img.Height != newHeight) @@ -805,26 +805,28 @@ namespace ASC.Web.Core.Users } - public Bitmap GetPhotoBitmap(Guid userID) + public Image GetPhotoImage(Guid userID, out IImageFormat format) { try { var data = UserManager.GetUserPhoto(userID); if (data != null) { - using var s = new MemoryStream(data); - return new Bitmap(s); + var img = Image.Load(data, out var imgFormat); + format = imgFormat; + return img; } } catch { } + format = null; return null; } - public string SaveThumbnail(Guid userID, Image img, ImageFormat format) + public string SaveThumbnail(Guid userID, Image img, IImageFormat format) { var moduleID = Guid.Empty; var widening = CommonPhotoManager.GetImgFormatName(format); - var size = img.Size; + var size = img.Size(); var fileName = string.Format("{0}{1}_size_{2}-{3}.{4}", (moduleID == Guid.Empty ? "" : moduleID.ToString()), userID, img.Width, img.Height, widening); var store = GetDataStore(); @@ -912,7 +914,7 @@ namespace ASC.Web.Core.Users /// /// Helper class for manipulating images. /// - public static class ImageHelper + /*public static class ImageHelper { /// /// Rotate the given image byte array according to Exif Orientation data @@ -925,7 +927,8 @@ namespace ASC.Web.Core.Users try { using var stream = new MemoryStream(data); - using var img = new Bitmap(stream); + using var img = Image.Load(stream); + var fType = RotateImageByExifOrientationData(img, updateExifData); if (fType != RotateFlipType.RotateNoneFlipNone) { @@ -1005,7 +1008,7 @@ namespace ASC.Web.Core.Users _ => RotateFlipType.RotateNoneFlipNone, }; } - } + }*/ public static class SizeExtend { diff --git a/web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs b/web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs index 325fe734f3..f94fc9156c 100644 --- a/web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs +++ b/web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs @@ -25,12 +25,14 @@ using System; -using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Drawing2D; +using System.Collections.Generic; using ASC.Core.Common.Settings; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Processing; + namespace ASC.Web.Core.Users { public class UserPhotoThumbnailManager @@ -53,13 +55,13 @@ namespace ASC.Web.Core.Users var resultBitmaps = new List(); - var img = thumbnailsData.MainImgBitmap(); + var img = thumbnailsData.MainImgBitmap(out var format); if (img == null) return null; foreach (var thumbnail in thumbnailsData.ThumbnailList()) { - thumbnail.Bitmap = GetBitmap(img, thumbnail.Size, thumbnailSettings); + thumbnail.Image = GetImage(img, thumbnail.Size, thumbnailSettings); resultBitmaps.Add(thumbnail); } @@ -71,32 +73,22 @@ namespace ASC.Web.Core.Users return thumbnailsData.ThumbnailList(); } - public static Bitmap GetBitmap(Image mainImg, Size size, UserPhotoThumbnailSettings thumbnailSettings, InterpolationMode interpolationMode = InterpolationMode.HighQualityBicubic) - { - var thumbnailBitmap = new Bitmap(size.Width, size.Height); - - var scaleX = size.Width / (1.0 * thumbnailSettings.Size.Width); - var scaleY = size.Height / (1.0 * thumbnailSettings.Size.Height); - - var rect = new Rectangle(-(int)(scaleX * (1.0 * thumbnailSettings.Point.X)), - -(int)(scaleY * (1.0 * thumbnailSettings.Point.Y)), - (int)(scaleX * mainImg.Width), - (int)(scaleY * mainImg.Height)); - - using (var graphic = Graphics.FromImage(thumbnailBitmap)) - { - graphic.InterpolationMode = interpolationMode; - graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; - graphic.SmoothingMode = SmoothingMode.HighQuality; - graphic.CompositingMode = CompositingMode.SourceCopy; - graphic.CompositingQuality = CompositingQuality.HighQuality; - - using var wrapMode = new System.Drawing.Imaging.ImageAttributes(); - wrapMode.SetWrapMode(WrapMode.TileFlipXY); - graphic.DrawImage(mainImg, rect, 0, 0, mainImg.Width, mainImg.Height, GraphicsUnit.Pixel, wrapMode); - } - - return thumbnailBitmap; + public static Image GetImage(Image mainImg, Size size, UserPhotoThumbnailSettings thumbnailSettings) + { + + var x = thumbnailSettings.Point.X > 0 ? thumbnailSettings.Point.X : 0; + var y = thumbnailSettings.Point.Y > 0 ? thumbnailSettings.Point.Y : 0; + var rect = new Rectangle(x, + y, + thumbnailSettings.Size.Width, + thumbnailSettings.Size.Height); + + Image destRound = mainImg.Clone(x => x.Crop(rect).Resize(new ResizeOptions + { + Size = size, + Mode = ResizeMode.Stretch + })); + return destRound; } } @@ -104,7 +96,7 @@ namespace ASC.Web.Core.Users { public Size Size { get; set; } public string ImgUrl { get; set; } - public Bitmap Bitmap { get; set; } + public Image Image { get; set; } } public class ThumbnailsData @@ -118,9 +110,11 @@ namespace ASC.Web.Core.Users UserPhotoManager = userPhotoManager; } - public Bitmap MainImgBitmap() - { - return UserPhotoManager.GetPhotoBitmap(UserId); + public Image MainImgBitmap(out IImageFormat format) + { + var img = UserPhotoManager.GetPhotoImage(UserId, out var imageFormat); + format = imageFormat; + return img; } public string MainImgUrl() @@ -164,8 +158,8 @@ namespace ASC.Web.Core.Users { foreach (var item in bitmaps) { - using var mainImgBitmap = MainImgBitmap(); - UserPhotoManager.SaveThumbnail(UserId, item.Bitmap, mainImgBitmap.RawFormat); + using var mainImgBitmap = MainImgBitmap(out var format); + UserPhotoManager.SaveThumbnail(UserId, item.Image, format); } } } diff --git a/web/ASC.Web.Core/Users/UserPhotoThumbnailSettings.cs b/web/ASC.Web.Core/Users/UserPhotoThumbnailSettings.cs index 506e453f2b..b4fb4c0281 100644 --- a/web/ASC.Web.Core/Users/UserPhotoThumbnailSettings.cs +++ b/web/ASC.Web.Core/Users/UserPhotoThumbnailSettings.cs @@ -23,12 +23,14 @@ * */ - -using System; -using System.Drawing; + + +using System; using ASC.Core.Common.Settings; +using SixLabors.ImageSharp; + namespace ASC.Web.Core.Users { [Serializable] diff --git a/web/ASC.Web.Core/WhiteLabel/TenantInfoSettings.cs b/web/ASC.Web.Core/WhiteLabel/TenantInfoSettings.cs index 39c80f0c62..629709ed7e 100644 --- a/web/ASC.Web.Core/WhiteLabel/TenantInfoSettings.cs +++ b/web/ASC.Web.Core/WhiteLabel/TenantInfoSettings.cs @@ -25,7 +25,6 @@ using System; -using System.Drawing; using System.Globalization; using System.IO; using System.Text.Json.Serialization; @@ -36,8 +35,10 @@ using ASC.Core.Common.Settings; using ASC.Data.Storage; using ASC.Web.Core.Utility.Skins; -using Microsoft.Extensions.Configuration; - +using Microsoft.Extensions.Configuration; + +using SixLabors.ImageSharp; + namespace ASC.Web.Core.WhiteLabel { [Serializable] @@ -130,9 +131,9 @@ namespace ASC.Web.Core.WhiteLabel } } using (var memory = new MemoryStream(data)) - using (var image = Image.FromStream(memory)) + using (var image = Image.Load(memory)) { - tenantInfoSettings.CompanyLogoSize = image.Size; + tenantInfoSettings.CompanyLogoSize = image.Size(); memory.Seek(0, SeekOrigin.Begin); store.Save(companyLogoFileName, memory); tenantInfoSettings.CompanyLogoFileName = companyLogoFileName; diff --git a/web/ASC.Web.Core/WhiteLabel/TenantWhiteLabelSettings.cs b/web/ASC.Web.Core/WhiteLabel/TenantWhiteLabelSettings.cs index fa8aa1ec98..97a881646d 100644 --- a/web/ASC.Web.Core/WhiteLabel/TenantWhiteLabelSettings.cs +++ b/web/ASC.Web.Core/WhiteLabel/TenantWhiteLabelSettings.cs @@ -26,7 +26,6 @@ using System; using System.Collections.Generic; -using System.Drawing; using System.Globalization; using System.IO; using System.Linq; @@ -42,10 +41,14 @@ using ASC.Data.Storage; using ASC.Web.Core.Users; using ASC.Web.Core.Utility.Skins; -using Microsoft.Extensions.Options; - -using TMResourceData; - +using Microsoft.Extensions.Options; + +using SixLabors.ImageSharp; + +using TMResourceData; + +using UnknownImageFormatException = SixLabors.ImageSharp.UnknownImageFormatException; + namespace ASC.Web.Core.WhiteLabel { [Serializable] @@ -322,9 +325,9 @@ namespace ASC.Web.Core.WhiteLabel #endregion using (var memory = new MemoryStream(data)) - using (var image = Image.FromStream(memory)) + using (var image = Image.Load(memory)) { - var logoSize = image.Size; + var logoSize = image.Size(); var logoFileName = BuildLogoFileName(type, logoFileExt, false); memory.Seek(0, SeekOrigin.Begin); @@ -523,15 +526,15 @@ namespace ASC.Web.Core.WhiteLabel private static void ResizeLogo(string fileName, byte[] data, long maxFileSize, Size size, IDataStore store) { //Resize synchronously - if (data == null || data.Length <= 0) throw new UnknownImageFormatException(); + if (data == null || data.Length <= 0) throw new UnknownImageFormatException("data null"); if (maxFileSize != -1 && data.Length > maxFileSize) throw new ImageWeightLimitException(); try { using var stream = new MemoryStream(data); - using var img = Image.FromStream(stream); - var imgFormat = img.RawFormat; - if (size != img.Size) + using var img = Image.Load(stream, out var format); + var imgFormat = format; + if (size != img.Size()) { using var img2 = CommonPhotoManager.DoThumbnail(img, size, false, true, false); data = CommonPhotoManager.SaveToBytes(img2); @@ -548,7 +551,7 @@ namespace ASC.Web.Core.WhiteLabel } catch (ArgumentException error) { - throw new UnknownImageFormatException(error); + throw new UnknownImageFormatException(error.Message); } }