Download image from image url and showing in Image control in asp.net with C#
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开发者_StackOverflow do that? Please suggest me right solution for this problem.
Thanks in advance.
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.
精彩评论