DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

Programatically save an image/resource from a Web URL using C# and VB.NET

By Ashok Nalam on 17 Apr 2014 | Category: C# | Tagged: .NET C# ASP.NET VB.NET 
The snippet shows how to copy image from web URL to local machine using WebClient.DownloadFile method with C# and VB.NET examples.
  Discuss Add Comment   |  
Add rating Rate this resource   

Introduction

The snippet shows how to copy image from web URL to local machine using WebClient.DownloadFile method with C# and VB.NET examples.

Sample Image:

We will use below image to copy from web URL to local machine.
sample image

Image URL : http://dotnetmirror.com/Resources/Uploaded-Files/cd690a7d-1d60-4a85-8c14-183810d191fe/sample copy imaage.png

C# Example

using System;
using System.Net;
namespace DotNetMirror
{
    class SaveImageFromRemoteURL
    {
        public static void Main()
        {
            string remoteImageURLAddress = "http://dotnetmirror.com/Resources/Uploaded-Files/cd690a7d-1d60-4a85-8c14-183810d191fe/sample copy imaage.png";
            string localFileName = @"sample copy imaage.png";
using (WebClient webClient = new WebClient()) { webClient.DownloadFile(remoteImageURLAddress, localFileName); } Console.WriteLine("copied image from remote location"); Console.ReadLine(); } } }

VB.NET Example

Imports System.Net
Namespace DotNetMirror
    Class SaveImageFromRemoteURL
        Public Shared Sub Main()
            Dim remoteImageURLAddress As String = "http://dotnetmirror.com/Resources/Uploaded-Files/cd690a7d-1d60-4a85-8c14-183810d191fe/sample copy imaage.png"
            Dim localFileName As String = "c:\sample copy imaage.png"
Using webClient As New WebClient() webClient.DownloadFile(remoteImageURLAddress, localFileName) End Using Console.WriteLine("copied image from remote location") Console.ReadLine() End Sub End Class End Namespace

Output

Copies image from "http://dotnetmirror.com/Resources/Uploaded-Files/cd690a7d-1d60-4a85-8c14-183810d191fe/sample copy imaage.png" to local drive.
prints on console "copied image from remote location"

Explanation

WebClient has method DownloadFile which downloads the resource with the specified URI to a local file. We have to dispose webclient because webClient is implemented IDisposable interface.

Note: we have used example with image. You can specify any resource from web URL like .txt.
  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 9226  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.
 
cheap jordans|wholesale air max|wholesale jordans|wholesale jewelry|wholesale jerseys