如何在PictureBox - 特定URL中显示Stream(Responce)中的图像

时间:2023-01-24 15:25:01

here is my codes :

这是我的代码:

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.flowercity.com/");
    req.Method = "GET";
    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    req.ContentType = "text/html; charset=utf-8";
    req.Timeout = 25000;

    HttpWebResponse res = (HttpWebResponse)req.GetResponse();

    Stream Stream = res.GetResponseStream();
    StreamReader reader = new StreamReader(Stream);
    string reader_str = reader.ReadToEnd();

how can i grab loaded images in Stream and show them one by one in a PictureBox?

如何在Stream中抓取加载的图像并在PictureBox中逐个显示它们?

Edit 1 :
please see my url, it's not an image, it's a web site and we have a web site in Stream.
so the codes below does not work :

编辑1:请看我的网址,它不是图片,它是一个网站,我们在Stream中有一个网站。所以下面的代码不起作用:

    Image img = Image.FromStream(Stream);
    PictureBox1.Image = img;

Edit 2 :
i can not use HtmlAgilityPack for getting image urls.
there is no specific url for every image and need to show previous loaded images in stream.

编辑2:我无法使用HtmlAgilityPack获取图片网址。每个图像都没有特定的URL,需要在流中显示以前加载的图像。

1 个解决方案

#1


2  

As stated in the comment by vcsjones, your stream is that of the index page of the website. Within it are image tags . Like your browser does for you when you navigate to the site in question, you need to download the images separately.

正如vcsjones的评论中所述,您的流是网站索引页面的流。其中包括图像标签。当您导航到相关网站时,就像您的浏览器为您所做的那样,您需要单独下载图像。

The most reliable method for doing that is probably to use regular expressions to search the webpage (the one you're loading in your example code) for those links. You can obviously try to load the page as an XML document but XML parsers are very strict on validity. Most webpage authors unfortunately aren't so a more "loose" search of the webpage for your links probably works best.

最可靠的方法是使用正则表达式搜索这些链接的网页(您在示例代码中加载的那个)。您显然可以尝试将页面加载为XML文档,但XML解析器的有效性非常严格。遗憾的是,大多数网页作者对网页的“松散”搜索可能效果最好。

Your code should work if you feed it a direct image link. Your challenge now is to get those direct links from the page you're loading.

如果您提供直接图像链接,您的代码应该可以正常工作。您现在面临的挑战是从您正在加载的页面获取这些直接链接。

Edit

编辑

Your webpage looks something like this:

您的网页看起来像这样:

<html>
<title>welcome to the flower page</title>
<img src="flower.jpg"/>
</html>

(shortened for simplicity's sake)

(为简单起见缩短了)

You need to read through the text you are getting to find where the images are stored on the webserver and then open a separate http connection to download each image.

您需要通读文本,找到图像存储在Web服务器上的位置,然后打开单独的http连接以下载每个图像。

#1


2  

As stated in the comment by vcsjones, your stream is that of the index page of the website. Within it are image tags . Like your browser does for you when you navigate to the site in question, you need to download the images separately.

正如vcsjones的评论中所述,您的流是网站索引页面的流。其中包括图像标签。当您导航到相关网站时,就像您的浏览器为您所做的那样,您需要单独下载图像。

The most reliable method for doing that is probably to use regular expressions to search the webpage (the one you're loading in your example code) for those links. You can obviously try to load the page as an XML document but XML parsers are very strict on validity. Most webpage authors unfortunately aren't so a more "loose" search of the webpage for your links probably works best.

最可靠的方法是使用正则表达式搜索这些链接的网页(您在示例代码中加载的那个)。您显然可以尝试将页面加载为XML文档,但XML解析器的有效性非常严格。遗憾的是,大多数网页作者对网页的“松散”搜索可能效果最好。

Your code should work if you feed it a direct image link. Your challenge now is to get those direct links from the page you're loading.

如果您提供直接图像链接,您的代码应该可以正常工作。您现在面临的挑战是从您正在加载的页面获取这些直接链接。

Edit

编辑

Your webpage looks something like this:

您的网页看起来像这样:

<html>
<title>welcome to the flower page</title>
<img src="flower.jpg"/>
</html>

(shortened for simplicity's sake)

(为简单起见缩短了)

You need to read through the text you are getting to find where the images are stored on the webserver and then open a separate http connection to download each image.

您需要通读文本,找到图像存储在Web服务器上的位置,然后打开单独的http连接以下载每个图像。