0

I have a problem that I want to show an image in Image control by downloading image from Image url, but I don't know how to do that? Please suggest me right solution for this problem.

Thanks in advance.

1
  • 2
    can't you simply use <asp:Image ImageUrl="urltoimage.com" runat="server"/> ? Commented Sep 14, 2011 at 18:31

1 Answer 1

1

I'm not sure exactly what you mean, so if this is not a solution to your issue please disregard this answer.

I assume what you mean is that you wish to download an image from a URL in the codebehind, store the image locally and serve this image to the browser.

To do this you can use the following:

Markup

<asp:Image ID="image1" runat="server" />

CodeBehind

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string imageName = "downloaded-image.png";
        string imagePath = Path.Combine(Server.MapPath(@"~\Images"), imageName);
        string imageUrl = "https://encrypted.google.com/images/logos/ssl_logo.png";

        WebClient client = new WebClient();
        client.DownloadFile(imageUrl, imagePath);

        image1.ImageUrl = string.Format(@"~\Images\{0}", imageName);
    }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.