Wednesday, December 14, 2011

ASP.net load an image from embedded resource

Below is a simple method that I couldn't find anywhere else online that loads a .gif image from an embedded resource and returns the image as the web page response


private static void WriteGifImageToResponse(HttpContext context, string resourcePath)
{
    HttpResponse response = context.Response;
    response.ContentType = "image/gif";
 
    Assembly asm = Assembly.GetExecutingAssembly();
    using (Stream imageStream = asm.GetManifestResourceStream(resourcePath))
    {
        using (Image theImage = Image.FromStream(imageStream))
        {
            theImage.Save(context.Response.OutputStream, ImageFormat.Gif);
        }
    }
}