In this article I will explain with an example, how to export Image to Excel file in ASP.Net using C# and VB.Net.
First the Absolute URL of the Image file will be determined and then the Image element will be rendered as HTML and then the HTML will be written to the Response Stream and downloaded as Excel file in ASP.Net.
 
 
HTML Markup
The HTML Markup consists of an Image control and a Button.
<asp:Image ID="Image1" ImageUrl="~/Images/Koala.jpg" runat="server" Height="300"
    Width="350" />
<br />
<br />
<asp:Button Text="Export" OnClick="ExportExcel" runat="server"/>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 
Exporting Image to Excel file in ASP.Net
When the Export Button is clicked, the ExportExcel event handler is executed which first converts the Relative URL to Absolute URL using the GetAbsoluteUrl function.
Relative URL
This URL is path of the file which is relative to the Website Root folder. Example ~/images/Koala.jpg is a Relative URL.
Relative URL will work fine within the Website but it cannot be used in cases when image has to be displayed in Excel file since when Excel application is opened, the Image will be downloaded from server.
Absolute URL
Absolute URL is the complete URL to the file with the domain name or IP Address. Example http://localhost:9857/Export_Image_Excel/Images/Koala.jpg is an Absolute URL.
Absolute URL can be used within the website as well in some other external application like Word, Excel, etc.
 
Exporting Image file to Excel
In order to export the Image file as Excel, a dynamic ASP.Net Table is created and the Image control is added to it.
Then the ASP.Net Table is rendered into an HTML string and finally the HTML string is written to the Response Stream and downloaded as Excel file.
C#
protected void ExportExcel(object sender, EventArgs e)
{
    //Convert the Relative Url to Absolute Url and set it to Image control.
    Image1.ImageUrl = this.GetAbsoluteUrl(Image1.ImageUrl);
 
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            //Create a Table.
            Table table = new Table();
 
            //Add Image control to the Table Cell.
            TableRow row = new TableRow();
            row.Cells.Add(new TableCell());
            row.Cells[0].Controls.Add(Image1);
            table.Rows.Add(row);
 
            //Render the Table as HTML.
            table.RenderControl(hw);
 
            //Export the Table to Excel.
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Images.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
 
            //Write the HTML string to Response.
            Response.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
    }
}
 
public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}
 
private string GetAbsoluteUrl(string relativeUrl)
{
    relativeUrl = relativeUrl.Replace("~/", string.Empty);
    string[] splits = Request.Url.AbsoluteUri.Split('/');
    if (splits.Length >= 2)
    {
        string url = splits[0] + "//";
        for (int i = 2; i < splits.Length - 1; i++)
        {
            url += splits[i];
            url += "/";
        }
 
        return url + relativeUrl;
    }
    return relativeUrl;
}
 
VB.Net
Protected Sub ExportExcel(sender As Object, e As EventArgs)
    'Convert the Relative Url to Absolute Url and set it to Image control.
    Image1.ImageUrl = Me.GetAbsoluteUrl(Image1.ImageUrl)
 
    Using sw As New StringWriter()
        Using hw As New HtmlTextWriter(sw)
            'Create a Table.
            Dim table As New Table()
 
            'Add Image control to the Table Cell.
            Dim row As New TableRow()
            row.Cells.Add(New TableCell())
            row.Cells(0).Controls.Add(Image1)
            table.Rows.Add(row)
 
            'Render the Table as HTML.
            table.RenderControl(hw)
 
            'Export the Table to Excel.
            Response.Clear()
            Response.Buffer = True
            Response.AddHeader("content-disposition", "attachment;filename=Images.xls")
            Response.Charset = ""
            Response.ContentType = "application/vnd.ms-excel"
 
            'Write the HTML string to Response.
            Response.Write(sw.ToString())
            Response.Flush()
            Response.End()
        End Using
    End Using
End Sub
 
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    ' Verifies that the control is rendered
End Sub
 
Private Function GetAbsoluteUrl(relativeUrl As String) As String
    relativeUrl = relativeUrl.Replace("~/", String.Empty)
    Dim splits As String() = Request.Url.AbsoluteUri.Split("/")
    If splits.Length >= 2 Then
        Dim url As String = splits(0) + "//"
        For i As Integer = 2 To splits.Length - 2
            url += splits(i)
            url += "/"
        Next
 
        Return url & relativeUrl
    End If
    Return relativeUrl
End Function
 
 
Screenshots
The Image file
Export Image to Excel file in ASP.Net using C# and VB.Net
 
The exported Excel file
Export Image to Excel file in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads