net6: replacing System.Drawing with ImageSharp
This commit is contained in:
parent
735a9b60cf
commit
b6461e46f4
@ -42,6 +42,7 @@
|
||||
<PackageReference Include="LumenWorksCsvReader" Version="4.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="6.0.0-preview.7.21377.19" />
|
||||
<PackageReference Include="SharpZipLib" Version="1.3.2" />
|
||||
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0008" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -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;
|
||||
|
@ -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[]
|
||||
|
@ -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));
|
||||
|
||||
|
@ -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
|
||||
|
@ -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<T> fileDao, File<T> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="6.0.0-preview.7.21377.19" />
|
||||
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0008" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -216,6 +216,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0008" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -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<Rgba32>(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<Rgba32>(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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
@ -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
|
||||
/// <summary>
|
||||
/// Helper class for manipulating images.
|
||||
/// </summary>
|
||||
public static class ImageHelper
|
||||
/*public static class ImageHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 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
|
||||
{
|
||||
|
@ -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<ThumbnailItem>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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]
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user