我正在尝试构建一个 Blazor Server Web 应用程序,该应用程序使用 ImageSharp 通过 ARGB 像素修改任何上传的图像。所以,我看到了这个网页(<https://docs.sixlabors.com/articles/imagesharp/pixelbuffers.html)。在“Parallel, Pixel-Format Agnostic Image Manipulation”部分,它有一个我想添加到我的网络应用程序的代码示例:
image.Mutate(c => c.ProcessPixelRowsAsVector4(row =>
{
for (int x = 0; x < row.Length; x++)
{
// We can apply any custom processing logic here
row[x] = Vector4.SquareRoot(row[x]);
}
}));
但是,我一直在想如何让这段代码在我的网络应用程序上运行。您能否在此代码中为我提供一个自定义处理逻辑的示例?或者,我可以使用其他更好的代码吗?
这是我的网络应用程序的 Razor 页面,供您参考。
@using BlazorServerApp1; @*project name*@
@using BlazorInputFile
@using SixLabors.ImageSharp;
@using SixLabors.ImageSharp.Formats;
@using SixLabors.ImageSharp.Processing;
@using SixLabors.ImageSharp.PixelFormats; @*<rgba32>*@
@using System;
@using System.IO;
@using System.Numerics; @*vector4*@
@page "/imageedit0"
<h1>Image Edit</h1>
<h3>Upload an image</h3>
@*button upload an image*@
<InputFile OnChange="HandleFileSelected_function" />
@*display an image, upload status, and filter button*@
@if (displayImage == true)
{
<h5>@status</h5>
<img src="@_ip.getBase64String_function()" style="width:400px" />
<p>
<button class="btn btn-primary" @onclick="Filter1">filter1</button> <br />
<button class="btn btn-primary" @onclick="Filter2">filter2</button> <br />
</p>
}
@code {
string status;
string Error;
bool displayImage = false;
ImageProcessing_CS _ip;
Image<Rgba32> image;
IImageFormat IIF1;
//filter1 = replace dark colors with red color
void Filter1()
{
for (int w = 0; w < image.Width; w++)
{
for (int h = 0; h < image.Height; h++)
{
if (image[w, h].R < 200 && image[w, h].G < 200 && image[w, h].B < 200)
image[w, h] = new Rgba32(255, image[w, h].G, image[w, h].B, image[w, h].A);
}
}
}
//how to add custom processing logic here?
void Filter2()
{
image.Mutate(c => c.ProcessPixelRowsAsVector4(row =>
{
for (int x = 0; x < row.Length; x++)
{
// We can apply any custom processing logic here
row[x] = Vector4.SquareRoot(row[x]);
}
}));
}
//uploads and initalizes image
async Task HandleFileSelected_function(IFileListEntry[] files)
{
var imageFile = files.FirstOrDefault();
if (imageFile != null)
{
var ms = new MemoryStream();
await imageFile.Data.CopyToAsync(ms);
try
{
image = Image.Load<Rgba32>(ms.ToArray(), out IIF1);
_ip = new ImageProcessing_CS(image, IIF1);
displayImage = true;
status = $"Finished loading {imageFile.Size} bytes from {imageFile.Name}";
}
catch (Exception e)
{
Error = $"Error occured while loading image {e.Message}";
}
}
}
}