articles

Home / DeveloperSection / Articles / Dynamically loading image in Image control in ASP.NET

Dynamically loading image in Image control in ASP.NET

Dynamically loading image in Image control in ASP.NET

Anonymous User 121793 25-Feb-2011

Dynamically loading an image in Image control in ASP.NET

Here In this Article, We will tell you how to add an image in image control dynamically.

Steps to implement this task 

Please follow the following steps to implement the desired task
  •    Add two aspx files in the project and give named as Default.aspx and DynamicImage.aspx.
  •    Write down following aspx script code to design user interface on Default aspx.
<div style="height: 486px; width: 872px">     
        <asp:Image ID="Image1" runat="server" Height="432px" Width="866px" />
        <br />
        <asp:TextBox ID="TextBox1" runat="server"
            style="margin-left: 26px; margin-top: 17px" Width="183px"></asp:TextBox>
&nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            style="margin-left: 21px; margin-top: 15px" Text="Enter Image URL"
            Width="136px" />   
   </div> 
  •    At the click event of Button1 writes down the following code.           

        Image1.ImageUrl = "DynamicImage.aspx?path=" + TextBox1.Text;

  •    At the load event of DynamicImage.aspx write down the following code
  • protected void Page_Load(object sender, EventArgs e)
       {
              string path = Request.QueryString[0];
              if (path != null)
              {
                     System.Drawing.Bitmap img = new System.Drawing.Bitmap(path);
              img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
              }
       }
The output of the following code

Dynamically loading image in Image control in ASP.NET

After clicking Enter Image URL button following output will be displayed

Dynamically loading image in Image control in ASP.NET 

 


Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By