简体   繁体   English

C#从URL下载图像

[英]C# Download an image from URL

I use a website to get stats on wifi usage. 我使用一个网站获取有关wifi使用情况的统计信息。 The website creates an image of a graph representation of the data. 该网站创建数据图形表示的图像。 The way it does this is by the user, setting a date. 它的操作方式是由用户设置日期。 So for example, lets say it was last months statistics. 例如,可以说这是上个月的统计数据。 The website generates a URL which is then sent to the server and the server returns an image. 该网站生成一个URL,然后将其发送到服务器,服务器将返回图像。 an examples of the link is like this: 链接的示例如下:

https://www.example.com/graph/daily_usage?time_from=2015-06-01+00%3A00%3A00+%2B0100&time_to=2015-06-30+23%3A59%3A59+%2B0100&tsp=1436519988 https://www.example.com/graph/daily_usage?time_from=2015-06-01+00%3A00%3A00+%2B0100&time_to=2015-06-30+23%3A59%3A59+%2B0100&tsp=1436519988

The problem is, I am making a third party program that will download this image to be used in my program. 问题是,我正在制作一个第三方程序,该程序将下载此图像以供我的程序使用。 However, I cannot use the file. 但是,我无法使用该文件。 It is as if the file is corrupt or something. 就像文件损坏了一样。 I have tried a few methods but maybe someone can suggest a different approach. 我尝试了几种方法,但也许有人可以建议其他方法。 Basically, how do I download an image that is generated by a server from a URL link? 基本上,如何从URL链接下载服务器生成的图像?

PS 聚苯乙烯

Just noticed that if I download the file by right clicking through a browser and save, the image downloads with a size of 17.something kilobytes. 只是注意到,如果我通过浏览器右键单击下载文件并保存,则图像下载的大小为17千字节左右。 But if I use the WebClient method to download the image, it only downloads 1.5kb. 但是,如果我使用WebClient方法下载图像,则它只会下载1.5kb。 Why would that be? 为什么会这样呢? Seems like the WebClient method does not download completely. 似乎WebClient方法没有完全下载。

Currently my code 目前我的代码

if (hrefAtt == "Usage Graph")
{
    string url = element.getAttribute("src");
    WebClient client = new WebClient();
    client.DownloadFile(url, tempFolderPath + "\\" + currentAcc + "_UsageSummary.png");
    wd.AddImagesToDoc(tempFolderPath + "\\" + currentAcc + "_UsageSummary.png");
    wd.SaveDocument();
}

TempFolderPath is my desktop\\TempFolder\\ TempFolderPath是我的桌面\\ TempFolder \\

UPDATE Out of random, I decided to see the raw data of the file with notepad and interestingly, the image data was actually a copy of the websites homepage html code, not the raw data of the image :S how does that make sense? 更新随机地,我决定用记事本查看文件的原始数据,有趣的是,图像数据实际上是网站首页html代码的副本,而不是图像的原始数据:S,这有什么意义?

这将下载Google徽标:

var img = Bitmap.FromStream(new MemoryStream(new WebClient().DownloadData("https://www.google.co.uk/images/srpr/logo11w.png")));

First of all, you have to understand link texture. 首先,您必须了解链接纹理。 If all links are same or close to each other, you have to use substring/remove/datetime etc. methods to make your new request link. 如果所有链接相同或彼此接近,则必须使用substring / remove / datetime等方法来建立新的请求链接。 For example; 例如;

string today = DateTime.Now.ToShortDateString();
string generatedLink = @"http://www.yoururl.com/image + " + today + ".jpg";
string generatedFileName = @"C:\Usage\usage + " + today + ".jpg";

WebClient wClient = new WebClient();
wClient.DownloadFile(generatedLink, generatedFileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM