我如何在dotnet core API中接受和返回图像

问题描述 投票:1回答:1

我有一个dotnet核心api,它接受字符串并返回一个qr代码(作为图像)。我可以在浏览器中看到图像。我有另一个使用api的项目,但我不知道如何获取图像

在此处输入代码

    //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

c# asp.net-core .net-core httpresponse qr-code
1个回答
0
投票

您可以做的是从HttpResponseMessage的内容中读取图像作为byteArray

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

然后将其作为json中的byte []属性返回给您的UI端]

public byte [] barCodeImage {get;组; }

更详细:

添加一个响应dto类,它将具有图像属性和您要求的其他属性基,像这样

    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
    }

然后您的

    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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.