如何在带有用户输入URL的Picturebox中显示图像? [英] How do I display an image in a Picturebox with a User Input URL?

查看:86
本文介绍了如何在带有用户输入URL的Picturebox中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始制作简单的HTTP图像浏览器时遇到一些麻烦。我提示用户输入图像的URL,然后单击按钮并在图片框中显示图像。我习惯于编写控制台应用程序,并且是Windows Forms命令的新手,这就是我寻求帮助的原因。


**另外但不是必需的,是包含图像的状态代码,以及谷歌网络服务器返回的相关标题,这些标题显示在文本框中。对不起,如果这太模糊了,我就完全迷失了,任何事情都会有所帮助。谢谢。

Having a bit of trouble getting started on making a simple HTTP image browser. I prompt the user for a URL for an image, and then click a button and display the image in a picturebox. I'm used to writing console applications, and am brand new to the Windows Forms commands, which is why I'm looking for some help.

**In addition, but not necessary, is the inclusion of a status code of the image, as well as associated headers that are returned by googles web server, which are displayed in textboxes. Sorry if this is too vague, I am just completely lost, and anything would help. Thanks.

推荐答案

要从HTTP获取图像,您需要下载它。您甚至不需要将其下载到临时文件( https://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx ),因为你可以读取一些图像流。

请查看所有方法 System.Net.WebClient.Download *

https://msdn.microsoft.com/en-us/library/system .net.webclient%28v = vs.110%29.aspx



因此,正如您所看到的,不仅可以将图像下载到临时文件,您也可以将其下载到内存中,直接从内存流中读取图像。但是,这不是最好的方法。



更好的是,你可以使用类 System.Net.HttpWebRequest ,获取HTTP响应并打开响应流,以从以下位置读取图像: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.110%29.aspx



从流中,您可以阅读图像: https://msdn.microsoft.com/en-us/library/system.drawing.image.fromstream%28v=vs.110%29.aspx



最后,您可以将您读取的图像分配给属性 PictureBox.Image https://msdn.microsoft.com/en-us/library/system .windows.forms.picturebox.image%28v = vs.110%29.aspx



-SA
To get an image from HTTP, you need to download it. You don't even need to download it to temporary file (https://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx), as you can read the image to some stream.
Please see all the methods System.Net.WebClient.Download*:
https://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.110%29.aspx.

So, as you can see, not only you can download image to a temporary file, you can also download it to memory, to read the image directly from memory stream. However, this is not the best way.

Better yet, you can use the class System.Net.HttpWebRequest, get an HTTP response and open the response stream, to read the image from: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.110%29.aspx.

From the stream, you can read the image: https://msdn.microsoft.com/en-us/library/system.drawing.image.fromstream%28v=vs.110%29.aspx.

Finally, you can assign the image you read to the property PictureBox.Image: https://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image%28v=vs.110%29.aspx.

—SA


这篇关于如何在带有用户输入URL的Picturebox中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆