gurrawar
gurrawar

Reputation: 363

Add bitmap image to cshtml mvc at runtime

I have a bit map image that I will generate during runtime. I do not have a local url for it as it is generated on runtime this way and returned to me. Is there a way to add this image to cshtml file or html file at runtime?

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;

    }

Upvotes: 1

Views: 3363

Answers (1)

bwbrowning
bwbrowning

Reputation: 6530

You could create an asp.net mvc action that returns a FileContentResult that will return the bitmap. Something like this:

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

Then in your view of another action you would reference the image using the imageGenerate action

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

Upvotes: 5

Related Questions