Question

I am generating a System.Drawing.Bitmap on the fly in an ASP.NET Custom Web Server Control, and then I want to serve this bitmap as part of the WebResource, because I do not want to save it on the hosting computer.

Is there a way to instruct ASP.NET to serve the generated System.Drawing.Bitmap as part of it's WebResource? (therefore making it an "Embedded Resource")

Was it helpful?

Solution

Use an HTTP Handler. The handler is basically a piece of code executed when a resource with a specified extension is requested from your server. Since you want a dynamically generated image, you would do that in the code for the handler and return it as a response. Embedding wouldn't work, because you can only embed static information. Here are some links:

OTHER TIPS

I did that in several ASP.NET projects without an HTTP handler.

Say you have an image named "Fingerprint.jpg" and you put it in a subfolder named "Images" in your web control library whose namespace is "MyNamespace". Effectively the computed namespace of your image would be "MyNamespace.Images". Mark that image with build action "Embedded Resource".

Now let's assume that in this web control library you have a web control class named "MyNamespace.SampleWebControl".

In AssemblyInformation.cs I added something like this for each image:

[assembly: System.Web.UI.WebResource("MyNamespace.Images.Fingerprint.jpg", "image/jpg")]

where in the example above the Fingerpint.jpg image is stored in the folder named "Images" right underneath the root directory of the web control library.

Then in the asp.net page's codebehind you could use something like this:

string imgName = "MyNamespace.Images.Fingerprint.jpg";
Type ctrlType = typeof(MyNamespace.SampleWebControl);
string imageUrl = Page.ClientScript.GetWebResourceUrl(ctrlType, imgName);

And then you can use that imageUrl value as the image's URL in an Image Control or an HTML IMG tag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top