Skip to content

Commit

Permalink
Fix ImageSharp upgrade error
Browse files Browse the repository at this point in the history
  • Loading branch information
realLiangshiwei committed Oct 27, 2023
1 parent 24ed11a commit 745fa41
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Import Project="..\..\..\common.props" />

<PropertyGroup>
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
<TargetFramework>net7.0</TargetFramework>
<PackageId>Volo.Abp.Imaging.ImageSharp</PackageId>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ImageSharpCompressOptions()

PngEncoder = new PngEncoder {
CompressionLevel = PngCompressionLevel.BestCompression,
IgnoreMetadata = true
SkipMetadata = true
};

WebpEncoder = new WebpEncoder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public virtual async Task<ImageCompressResult<Stream>> TryCompressAsync(
return new ImageCompressResult<Stream>(stream, ImageProcessState.Unsupported);
}

var (image, format) = await Image.LoadWithFormatAsync(stream, cancellationToken);
var image = await Image.LoadAsync(stream, cancellationToken);

if (!CanCompress(format.DefaultMimeType))
if (!CanCompress(image.Metadata.DecodedImageFormat!.DefaultMimeType))
{
return new ImageCompressResult<Stream>(stream, ImageProcessState.Unsupported);
}

var memoryStream = await GetStreamFromImageAsync(image, format, cancellationToken);
var memoryStream = await GetStreamFromImageAsync(image, image.Metadata.DecodedImageFormat, cancellationToken);

if (memoryStream.Length < stream.Length)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public virtual async Task<ImageResizeResult<Stream>> TryResizeAsync(
return new ImageResizeResult<Stream>(stream, ImageProcessState.Unsupported);
}

var (image, format) = await Image.LoadWithFormatAsync(stream, cancellationToken);
var image = await Image.LoadAsync(stream, cancellationToken);

if (!CanResize(format.DefaultMimeType))
if (!CanResize(image.Metadata.DecodedImageFormat!.DefaultMimeType))
{
return new ImageResizeResult<Stream>(stream, ImageProcessState.Unsupported);
}
Expand All @@ -44,7 +44,7 @@ public virtual async Task<ImageResizeResult<Stream>> TryResizeAsync(

try
{
await image.SaveAsync(memoryStream, format, cancellationToken: cancellationToken);
await image.SaveAsync(memoryStream, image.Metadata.DecodedImageFormat, cancellationToken: cancellationToken);
memoryStream.Position = 0;
return new ImageResizeResult<Stream>(memoryStream, ImageProcessState.Done);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class ImageFormatHelper
{
public static IImageFormat GetImageRawFormat(Stream stream)
{
using (var image = Image.Load(stream, out var imageFormat))
using (var image = Image.Load(stream))
{
return imageFormat;
return image.Metadata.DecodedImageFormat;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private byte[] GenerateInternal(string stringText, CaptchaOptions options)
var random = new Random();
var startWith = (byte)random.Next(5, 10);
image.Mutate(ctx => ctx.BackgroundColor(Color.Transparent));
var fontFamily = SystemFonts.Families.FirstOrDefault(x => x.IsStyleAvailable(options.FontStyle))?.Name ?? SystemFonts.Families.First().Name;
var fontFamily = SystemFonts.Families.FirstOrDefault(x => x.GetAvailableStyles().Contains(options.FontStyle)).Name ?? SystemFonts.Families.First().Name;
var font = SystemFonts.CreateFont(fontFamily, options.FontSize, options.FontStyle);

foreach (var character in stringText)
Expand All @@ -118,15 +118,18 @@ private byte[] GenerateInternal(string stringText, CaptchaOptions options)
var color = options.TextColor[random.Next(0, options.TextColor.Length)];
var location = new PointF(startWith + position, random.Next(6, 13));
image.Mutate(ctx => ctx.DrawText(text, font, color, location));
position += TextMeasurer.Measure(character.ToString(), new RendererOptions(font, location)).Width;
position += TextMeasurer.MeasureSize(character.ToString(), new TextOptions (font)
{
Origin = location
}).Width;
}

//add rotation
var rotation = GetRotation(options);
image.Mutate(ctx => ctx.Transform(rotation));

// add the dynamic image to original image
var size = (ushort)TextMeasurer.Measure(stringText, new RendererOptions(font)).Width;
var size = (ushort)TextMeasurer.MeasureSize(stringText, new TextOptions(font)).Width;
var img = new Image<Rgba32>(size + 15, options.Height);
img.Mutate(ctx => ctx.BackgroundColor(Color.White));

Expand All @@ -139,7 +142,7 @@ private byte[] GenerateInternal(string stringText, CaptchaOptions options)
var y1 = random.Next(0, img.Height);
img.Mutate(ctx =>
ctx.DrawLines(options.TextColor[random.Next(0, options.TextColor.Length)],
ctx.DrawLine(options.TextColor[random.Next(0, options.TextColor.Length)],
RandomTextGenerator.GenerateNextFloat(options.MinLineThickness, options.MaxLineThickness),
new PointF[] { new PointF(x0, y0), new PointF(x1, y1) })
);
Expand All @@ -153,7 +156,7 @@ private byte[] GenerateInternal(string stringText, CaptchaOptions options)
var y0 = random.Next(0, img.Height);
img.Mutate(
ctx => ctx
.DrawLines(options.NoiseRateColor[random.Next(0, options.NoiseRateColor.Length)],
.DrawLine(options.NoiseRateColor[random.Next(0, options.NoiseRateColor.Length)],
RandomTextGenerator.GenerateNextFloat(0.5, 1.5), new PointF[] { new Vector2(x0, y0), new Vector2(x0, y0) })
);
});
Expand Down

0 comments on commit 745fa41

Please sign in to comment.