TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Save Image from Another Website to our Website
Parvez Khan
Oct 10
2014
Code
1.9
k
0
2
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
public
void
FindSaveImages(
string
imgName)
{
//create temp folder
string
tempDir = Server.MapPath(
"../temp"
);
if
(!Directory.Exists(tempDir))
{
Directory.CreateDirectory(tempDir);
}
//get download url from web.config
string
remoteFileUrl = ConfigurationManager.AppSettings.Get(
"DownloadImgUrl"
);
//cal DownloadImageFromUrl function and get image from another websites
System.Drawing.Image image = DownloadImageFromUrl(remoteFileUrl + imgName);
string
rootPath = tempDir;
string
fileName1 = System.IO.Path.Combine(rootPath, imgName);
image.Save(fileName1);
//image will save iin your temp folder
}
//send http request and get image from another websites
public
System.Drawing.Image DownloadImageFromUrl(
string
imageUrl)
{
System.Drawing.Image image =
null
;
try
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl);
webRequest.AllowWriteStreamBuffering =
true
;
webRequest.Timeout = 30000;
System.Net.WebResponse webResponse = webRequest.GetResponse();
System.IO.Stream stream = webResponse.GetResponseStream();
image = System.Drawing.Image.FromStream(stream);
webResponse.Close();
}
catch
(Exception ex)
{
return
null
;
}
return
image;
}
C#
Website
Web Services in C#