简体   繁体   中英

How can i accept and return image in dotnet core API

I have a dotnet core api that accepts string and returns a qr code(as image). I can see the image on the browser. I have another project that consumes the api but i don't know how to get the image

    //This the code that accepts string and retuns image as qr code
    [Route("generate")]
    [HttpGet]
    public IActionResult Process(string context = "play")
    {

        var content = context;
        var width = 200;
        var height = 200;
        var barcodeWriterPixelData = new ZXing.BarcodeWriterPixelData
        {
            Format = ZXing.BarcodeFormat.QR_CODE,
            Options = new QrCodeEncodingOptions
            {
                Height = height,
                Width = width,
                Margin = 0
            }
        };
        var memoryStream = new MemoryStream();
        var pixelData = barcodeWriterPixelData.Write(content);
        using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height,
                System.Drawing.Imaging.PixelFormat.Format24bppRgb))
        {
            var bitmapData = bitmap.LockBits(
                                        new Rectangle(0, 0, pixelData.Width, pixelData.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                        System.Drawing.Imaging.PixelFormat.Format32bppRgb
                                        );
            try
            {
                System.Runtime.InteropServices.Marshal.Copy
                    (pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
            }
            finally
            {
                bitmap.UnlockBits(bitmapData);
            }
            bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
            memoryStream.Seek(0, SeekOrigin.Begin);




            return File(memoryStream, "image/png");

        }

    }

//This is the code that consumes the api but i don't know how to get the image from it
public class HomeController : Controller
{
   public QR_API _myapi = new QR_API();

    public async Task<ActionResult<JsonResult>> Index()
    {

        HttpClient client = _myapi.Initial();
        HttpResponseMessage res =  await client.GetAsync("generate");
        if (res.IsSuccessStatusCode)
        {
            return Json(res);
        }
        return Json("Not Working");
    }

AS you can see i can get the image in the api how can i retrieve it from the http rsponse message

one thing you can do is to read your image from HttpResponseMessage's content as byteArray

var image = response.Content.ReadAsByteArrayAsync().Result;

then return it as byte[] property in json to you UI side as

public byte[] barCodeImage { get; set; }

In More detail:

Add one response dto class which will have image property and other property base you requirements like this

    public class ResponseDTO
    {
     public int statuscode { get; set; }
     public string errormessage { get; set; }
     public string someproperty { get; set; }
     public byte[] barCodeImage { get; set; }//this one is ur image porperty
    }

then your

    public async Task<ActionResult<ResponseDTO>> Index()
    {
        var resp = new ResponseDTO() { statuscode = 200 };//creating response object
        try
        {
            HttpClient client = _myapi.Initial();
            HttpResponseMessage res = await client.GetAsync("generate");
            if (res.IsSuccessStatusCode)
            {
                HttpResponseMessage response = await client.GetAsync(builder.Uri);
                //read your image from HttpResponseMessage's content as byteArray
                var image = response.Content.ReadAsByteArrayAsync().Result;
                //Setting ur byte array to property of class which will convert into json later
                resp.barCodeImage = image;
                resp.someproperty = "some other details you want to send to UI";


            }
        }
        catch (Exception e)
        {
            //In case you got error
            resp.statuscode = 500;
            resp.errormessage = e.Message;

        }
        return resp;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM