You may use the following method to retrieve the image from a specific URL on the web. The below C# function takes an image url, download the image from the url as a stream.
[insert_adsense]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
string imageURL = "http://i1.wp.com/mplaza.pm/wordpress/wp-content/uploads/2015/09/output_RBPLdO.gif?w=676";
downloadImage(imageURL);
downloadImageAsync(imageURL);
}
public static void downloadImage(string imageURL)
{
using (WebClient webClient = new WebClient())
{
var watch = System.Diagnostics.Stopwatch.StartNew();
byte [] data = webClient.DownloadData(imageURL);
using (MemoryStream imageData = new MemoryStream(data))
{
Console.WriteLine(imageData.Length + " bytes received");
}
watch.Stop();
Console.WriteLine("Total Execution Time :{0}", watch.ElapsedMilliseconds);
}
}
public static void downloadImageAsync(string imageURL)
{
using (WebClient webClient = new WebClient())
{
webClient.DownloadDataCompleted += DownloadDataCompleted;
var watch = System.Diagnostics.Stopwatch.StartNew();
webClient.DownloadDataAsync(new Uri(imageURL));
while (webClient.IsBusy)
{
Console.WriteLine("\twaiting...");
Thread.Sleep(10);
}
watch.Stop();
Console.WriteLine("Total Execution Time :{0}", watch.ElapsedMilliseconds);
}
}
public static void DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
using (MemoryStream imageData = new MemoryStream(e.Result))
{
Console.WriteLine(imageData.Length + " bytes received");
}
}
}
}
|
Hiç yorum yok:
Yorum Gönder