terminando .NET

This commit is contained in:
José Henrique Ivanchechen 2023-08-20 17:21:58 -03:00
parent f8acb38f99
commit 2390f8fb8d
5 changed files with 90 additions and 6 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
.vs
bin
obj
obj
*.png

View File

@ -4,7 +4,7 @@ using TCC.Services;
namespace TCC.Controllers
{
[ApiController]
[Route("")]
[Route("image")]
public class ImageController : ControllerBase
{
private ImageService ImageService { get; set; }
@ -16,13 +16,13 @@ namespace TCC.Controllers
}
[HttpPost("blur")]
public async Task<IActionResult> BlurImage()
public async Task<IActionResult> BlurImage([FromQuery] int radius)
{
MemoryStream mstream = new MemoryStream();
await HttpContext.Request.Body.CopyToAsync(mstream);
mstream.Position = 0;
var result = ImageService.BoxBlurImage(mstream, 5);
var result = ImageService.BoxBlurImage(mstream, radius);
var blurredImageStream = new MemoryStream();
result.Write(blurredImageStream);
@ -31,5 +31,38 @@ namespace TCC.Controllers
return File(blurredImageStream, "image/png");
}
[HttpGet("load-image")]
public async Task<IActionResult> GetSimpleImage()
{
var result = ImageService.GetSimpleImage();
var imageStream = new MemoryStream();
result.Write(imageStream);
imageStream.Position = 0;
return File(imageStream, "image/png");
}
[HttpGet("load-big-image")]
public async Task<IActionResult> GetBigImage()
{
var result = ImageService.GetBigImage();
var imageStream = new MemoryStream();
result.Write(imageStream);
imageStream.Position = 0;
return File(imageStream, "image/png");
}
[HttpPost("save-big-image")]
public async Task<IActionResult> SaveBigImage()
{
MemoryStream mstream = new MemoryStream();
await HttpContext.Request.Body.CopyToAsync(mstream);
mstream.Position = 0;
return Ok();
}
}
}

16
ImageHelper.cs Normal file
View File

@ -0,0 +1,16 @@
using ImageMagick;
namespace tcc_app
{
public static class ImageHelper
{
public static MagickImage SimpleImage;
public static MagickImage BigImage;
static ImageHelper()
{
SimpleImage = new MagickImage("simpleimage.png");
BigImage = new MagickImage("bigimage.png");
}
}
}

View File

@ -1,3 +1,5 @@
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using TCC.Services;
namespace TCC
@ -12,6 +14,12 @@ namespace TCC
builder.Services.AddControllers();
builder.Services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
});
var app = builder.Build();
app.MapControllers();

View File

@ -1,4 +1,5 @@
using ImageMagick;
using tcc_app;
namespace TCC.Services
{
@ -10,6 +11,14 @@ namespace TCC.Services
{
var image = new MagickImage(imageStream);
var blurredImage = new MagickImage(image);
blurredImage = BoxBlurImageSeparable(image, blurredImage, radius, 0);
blurredImage = BoxBlurImageSeparable(image, blurredImage, 0, radius);
return blurredImage;
}
private MagickImage BoxBlurImageSeparable(MagickImage image, MagickImage blurredImage, int radiusX, int radiusY)
{
var pixels = image.GetPixels();
var blurredPixels = blurredImage.GetPixelsUnsafe();
@ -20,9 +29,9 @@ namespace TCC.Services
long rTotal = 0, gTotal = 0, bTotal = 0;
int pixelCount = 0;
for (int offsetY = -radius; offsetY <= radius; offsetY++)
for (int offsetY = -radiusY; offsetY <= radiusY; offsetY++)
{
for (int offsetX = -radius; offsetX <= radius; offsetX++)
for (int offsetX = -radiusX; offsetX <= radiusX; offsetX++)
{
int newX = x + offsetX;
int newY = y + offsetY;
@ -45,5 +54,22 @@ namespace TCC.Services
return blurredImage;
}
public void SaveImage(Stream fileStream)
{
var file = File.Create("image.png");
fileStream.CopyToAsync(file);
file.Close();
}
public MagickImage GetSimpleImage()
{
return ImageHelper.SimpleImage;
}
public MagickImage GetBigImage()
{
return ImageHelper.BigImage;
}
}
}