0

I'm developing a basic webservice. User send a base 64 string and the serve must return an image.

I have this class :

public class myImage
{    
    public Byte[] Matrix { get; set; }        
    public int Width { get; set; }
    public int Height { get; set; }
}

The Matrix is a byte array containing the pixel value in greyscale.

I saw a lot of topic (like this one or this one) about converting byte array to Image, but it did not work for me. I added reference to System.Drawing, but I have the error :

The type name 'Image' could not be found in the namespace 'System.Drawing'. This type has been forwarded to assembly 'System.Drawing.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly.

I saw that I have to return a FileResult but I can't create the result if I can not use System.Drawing.Image :

[HttpPost]
[Route("CreateImage")]
public FileResult PostSealCryptItem([FromBody]String base64)
{
    MyImage myImg = createImg(base64);

    FileResult result = ?;
    return result;
}

How can I create a FileResult from my byte array ?

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
  • I think it should be a `FileResult` – Stefan Jan 14 '19 at 10:10
  • See: https://stackoverflow.com/questions/34853072/how-to-return-file-from-asp-net-5-web-api – Stefan Jan 14 '19 at 10:11
  • Possible duplicate of [How to return file from ASP.net 5 web api](https://stackoverflow.com/questions/34853072/how-to-return-file-from-asp-net-5-web-api) – Stefan Jan 14 '19 at 10:11
  • If using Asp Core File Result works. Check the answer below. I think there's something missing though. Make sure the base64 is formatted as follows: `data:image/png;base64,iVBORw0KGgoAAA ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4 //8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU 5ErkJggg==` – Neal Jan 14 '19 at 10:11
  • *Don't* return `System.Drawing.Image`. That class is used to manipulate images. You can't just return a pixel matrix either, you'll have to create the actual bytes you want to return first. Once you have the actual byte buffer or stream you can return them with `return File(bytes,...)` or `return File(stream,...)` – Panagiotis Kanavos Jan 14 '19 at 10:17
  • What are you trying to do in the first place? Converting a Base64 string into a byte buffer is easy, you can use `var buffer=Convert.FromBase64String()`. That doesn't tell you anything about the content though. If that content is already a valid image, you could just return it with `return File(buffer,theactualcontenttype)`. You'd need a way to detect the image type or cheat and use `application/octet-stream`. – Panagiotis Kanavos Jan 14 '19 at 10:24
  • If that's *not* an actual image, or if you want to convert it to a specific format like `png` or `jpg` you'll need a library to a) detect the content type and b) convert it to the desired format. I'd suggest using [ImageSharp](https://github.com/SixLabors/ImageSharp) for that, instead of `System.Drawing`. – Panagiotis Kanavos Jan 14 '19 at 10:26
  • @PanagiotisKanavos I edited question to be more clear. If just want to return a image (png or jpg) created from a byte array. My conversion from base 64 is ok. – A.Pissicat Jan 14 '19 at 10:31

1 Answers1

2

You can do it as follows.

public HttpResponseMessage  PostSealCryptItem([FromBody]String base64)
{
    MyImage myImg = createImg(base64);
    Image img = convertMyImgToImage(myImg);
    using(MemoryStream ms = new MemoryStream())
    {
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new ByteArrayContent(ms.ToArray());
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");

        return result;
    }
}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51