从Bitmap对象将图像渲染到ASP.NET MVC网页上

时间:2022-09-24 23:45:23

I have code which has generated an image of a Qr Code into the .NET Bitmap object. The code which generates this is called when a button is pressed on the page, like so:

我有代码生成了一个Qr代码的图像到.NET Bitmap对象。在页面上按下按钮时会调用生成此代码的代码,如下所示:

    public ActionResult GenerateQrCode()
    {
        Bitmap qrCodeImage = Generate();
        return RedirectToAction("QrCodeGenerator");
    }

It's a method in an MVC controller for the page.

它是页面的MVC控制器中的一种方法。

When the button is pressed, an image is generated, and we are navigated back to the page.

按下按钮时,将生成图像,然后我们将导航回页面。

So from here, what steps do I need to take to output this bitmap image to my web page (a .cshtml file). Bearing in mind that I am using ASP.NET MVC.

因此,从这里开始,我需要采取哪些步骤将此位图图像输出到我的网页(.cshtml文件)。请记住我正在使用ASP.NET MVC。

One thing I seen online is that people were saving the image to the 'Response.OutputStream'. I'm not quite sure if that is relevant to ASP.NET MVC.

我在网上看到的一件事是人们将图像保存到'Response.OutputStream'。我不太确定这是否与ASP.NET MVC相关。

Thanks

1 个解决方案

#1


5  

The controller action need to return a FileStreamResult. This is how you do it

控制器动作需要返回FileStreamResult。这就是你如何做到的

public ActionResult GenerateQrCode()
{
   Bitmap qrCodeImage = Generate();
   //write your own methode to convert your bit map to byte array, here is a link
   //http://*.com/questions/7350679/convert-a-bitmap-into-a-byte-array-in-c
   byte[] byteArray = ConvertToByteArray(qrCodeImage);
   return File(byteArray, "image/jpeg");
} 

And in your view, you can do something like this

在您看来,您可以做这样的事情

<img src="@Url.Action("GenerateQrCode")" alt="qr code" />

#1


5  

The controller action need to return a FileStreamResult. This is how you do it

控制器动作需要返回FileStreamResult。这就是你如何做到的

public ActionResult GenerateQrCode()
{
   Bitmap qrCodeImage = Generate();
   //write your own methode to convert your bit map to byte array, here is a link
   //http://*.com/questions/7350679/convert-a-bitmap-into-a-byte-array-in-c
   byte[] byteArray = ConvertToByteArray(qrCodeImage);
   return File(byteArray, "image/jpeg");
} 

And in your view, you can do something like this

在您看来,您可以做这样的事情

<img src="@Url.Action("GenerateQrCode")" alt="qr code" />