Reputation: 371
I am programmatically creating an image via C#, and I'm wondering how can I use this created image as a background of a div.
To explain it better, here's an example:
public string BackImage()
{
System.Drawing.Image img = //generated image goes here
var ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
return Convert.ToBase64String(ms.ToArray());
//should I return it in a different way?
}
This gives me the image that I can use, now how would I set this image to be a background of a div?
<div id="main">
</div>
I have tried using background-image:url(data:image/jpeg;base64,@Html.Action("BackImage"))"
but the browser starts freezing because of the image.
Is there some simpler way of doing it?
Thanks.
Upvotes: 0
Views: 71
Reputation: 1836
Two ways:
Simple: Save your image in a temp folder and in your css class or style definition use background-image: url('whatevertheurlis')
A bit more complex: Create an IHttpHandler implementation to deliver the generated image. The css/style background-image setting is different only in the Url part.
I've done both before and the choice was based on the requirements of the number of different images to create and lifetime.
Upvotes: 1