在运行时将位图图像添加到cshtml mvc

gur*_*war 1 asp.net asp.net-mvc bitmap barcode

我有一个将在运行时生成的位图图像。我没有它的本地 url,因为它是在运行时以这种方式生成并返回给我的。有没有办法在运行时将此图像添加到 cshtml 文件或 html 文件?

public Bitmap GetBarcodeImage(string inputString)
    {

        Bitmap bitmap = new Bitmap(200,100);

        Graphics graphics = Graphics.FromImage(bitmap);

        graphics.Clear(Color.White);

        graphics.DrawString(inputString, new Font("Free 3 of 9",60,FontStyle.Regular), Brushes.Black, new PointF(0, 0));


        return bitmap;

    }
Run Code Online (Sandbox Code Playgroud)

bwb*_*ing 5

您可以创建一个返回 FileContentResult 的 asp.net mvc 操作,该操作将返回位图。像这样的东西:

public FileContentResult imageGenerate(string s)
    {
        Bitmap b = getBarcodeImage(s);
        ... get byte array from bitmap ...
        return new FileContentResult(bytes, "image/bmp");
    }
Run Code Online (Sandbox Code Playgroud)

然后,在您的另一个操作视图中,您将使用 imageGenerate 操作引用图像

<img src="<%=Url.Action("imageGenerate","SomeController", new {s = "asdf"}) %>" />  
Run Code Online (Sandbox Code Playgroud)