Using Images embedded in Project’s Assembly
Few months ago, I wrote a blog “Working with embedded resources in Project’s Assembly" explaining how you can embed resources in the project's assembly.
Now, in this blog I will show one simple example on how to use these image files to bind in an IMG tag in your web page.
Lets say we have the following image resources in our project’s assembly:
Now, in order to get the image in the IMG element in your ASPX website, you can create an Generic Handler or ASPX page that will be called inside the SRC attribute of the IMG tag.
I will go with Generic Handler so that I will define logic to check for QueryString parameter.
Here is one example how we will call the images from the resources
<img src=”Image.ashx?img=avatar.jpg" alt=”my avatar image” />
If you look at the underlined part, you will see that I have Image.ashx Generic Handler and an img=avatar.jpg Query String.
Here is how I’ve implemented the Generic Handler
{
ImageFormat format;
System.IO.Stream stream;
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["img"] != null)
{
string image = context.Request.QueryString["img"];
try
{
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
//Get the first found resource from the assembly
var imageName = (from i in asm.GetManifestResourceNames()
where i.Contains(image)
select i).ToList().First();
// Get the Image stream from the embedded image
stream = asm.GetManifestResourceStream(imageName);
// Create an Image object from the stream
using (System.Drawing.Image img = System.Drawing.Image.FromStream(stream))
{
//will get the last string in the split (its the image extension)
var extension = imageName.Split('.').Last();
switch (extension.ToLower())
{
case "jpg": format = ImageFormat.Jpeg; break;
case "gif": format = ImageFormat.Gif; break;
case "bmp": format = ImageFormat.Bmp; break;
case "png": format = ImageFormat.Png; break;
default: format = ImageFormat.Jpeg; break;
}
// Save the Image stream to the output stream
img.Save(context.Response.OutputStream, format);
}
}
catch (Exception ex)
{
//throw exception
}
finally
{
stream.Close();
stream.Dispose();
context.Response.End();
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
What I do is first checking for the img Query String. If it doesn’t exists, the image won’t get shown.
If the img parameter exists, using System.Reflection.Assembly I’ve created an asm object of the assembly.
With the next LINQ statement, I just get the FIRST found image from the Assembly’s ManifestResourceNames with condition only if it contains such name passed in the Query String.
(You can change the logic here depending of how you want to retrieve the assembly names)
Next, I’m loading the found assembly name in an Image object.
The last operation is checking the Extension of the image and saving the Image in the OutputStream with the found image format.
So, for the following image element
<img src=”Image.ashx?img=avatar.jpg" alt=”my avatar image” />
the result is
Refering to the @Mark's comment, I forgot to mention that this way may have some security issues. Thus, if implemeting using Generic Handler, please make some additional restrictions and checks inside your code logic.
The purpose of this blog is to show how you can use Generic Handler for this. However, you can also refer the following method for better usage which .NET framework provides to us: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getwebresourceurl.aspx - Thanks Mark for your remarks and contribution ;).
And that’s it.
Hope this helps.
Regards,
Hajan