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>
<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
After clicking Enter Image URL button following output will be displayed
Emerson Carvalho
12-Jul-2012Hello Arun Singh,
I solve my problem creating a Handler.ashx.
I call the Handler in the page like this:
<asp:Image ID="myImage" ImageUrl="~/ImageHandler.ashx?id=1" runat="server" />
And the handler will return the image without save the file.
Anonymous User
12-Jul-2012I think you want to assign bitmap image in asp image control directly without saving image file in system. Let me tell you it is not possible to directly set the image in asp image control!! because of unlike System.Windows.Forms.PictureBox, System.Web.UI.WebControls.Image does not have Bitmap property. System.Web.UI.WebControls.Image is Server version of html img tag (Its takes ImageUrl and renders as "<img src='/images/xyz.jpg' />").
To perform this task. Firstly you have to save the bitmap to a file, then assign that file to the Image1.ImageUrl, then load that file. As above article is showing.
Please marked as answer if this post will resolved your problem.
Emerson Carvalho
11-Jul-2012I'm trying to create a image using the System.Drawing.Graphics and display in
a control on the page, but I don't know how do this without save the image
on the System Files.
I want to set the new image to a specific "<asp:Image" on the page
Part of my code:
Anonymous User
11-Jul-2012Could you explain your question in detail actually what are you trying to do?
Emerson Carvalho
09-Jul-2012Like this: