Search code examples
c#graphicsasp.net-core-webapiasp.net-core-6.0imagesharp

How can I get image content as pixel and create image from it in ASP.NET Core 6 Web API?


I try to save my images on my server, but I can't let my server save file and virus because of that I want to get image content as pixels of rgb and after that I create image by myself.

I can't use bitmap (or other type in C# like bitmapImage, ... etc) and I don't know how I can do this with sixlabors.ImageSharp.

I have some code that I tried but I can't implement the exact logic that I want (code shown here):

[HttpPost("[action]")]
public async Task<IActionResult> Get([FromForm] ImageFormat file)
{
    await using var memoryStream = new MemoryStream();
    await file.File.CopyToAsync(memoryStream);

    IImageFormat format;

    using (var image = Image.Load(memoryStream.ToArray(), out format))
    {
        using (var output = new MemoryStream())
        {
            image.Save(output, format);
            var responseType = format.Name.ToLower();
            return File(output.ToArray(), "application/octet-stream", file.File.FileName);
        }
    }

    return null;
}

Can anybody help me with this problem?


Solution

    1. i don't see a reason to convert image into image: there are several format zip-algorythms etc.wich you have to support in that case. example jpg is not bitmap, there is convertion issue - quality of image becomes less each conversion time. Image itself is not executable - it can be used only as container for virus body, can't harm your OSystem itself, another executable part should works somewhere.
    2. But even if you would like to store images on disk, in other format - you can convert image to base64 text (one line of code, like example) - it less harmful and well known way to work with any file type. you can zip image by cszip, you can change file name and extension to hide file type. I don't see a reasson to convert one image to another for this scenario/task.