VB.Net Basics: How to get and display online images

xiaoxiao2021-03-06  64

In VB.NET, the picture is displayed very simple: picturebox1.image = image.fromfile (filename) But this method cannot be used to display pictures on the Internet, because Image.FroMFile does not support URI format. So what do you do? In fact, .NET prepared a very convenient way for us. In .NET, online resources (pictures, animations, sounds, web pages, etc.) are obtained by system.net.WebRequest and System.Net.WebResponse. Simply put, the steps to get pictures on the Internet are like this (take http://images.sohu.com/logo1.gif as an example): 1. Build a WebRequest by URI (see WebRequest constructor): DIM WR AS WebRequest = WebRequest.create ("http://images.sohu.com/logo1.gif") 2. Then get a WebResponse: Dim Res-WebResponse = Wr.GetResponse 3. through the WebResponse.getresponse 3. Get the stream to create Bitmap: Dim Bmp AS New Bitmap (Res.GetResponseSstream) 4. This can be displayed: PictureBox1.Image = BMP how, no complex. There are two points to note, one is to packet this code with the try, because the server returns 404 and other errors will trigger an exception; second, it is best to put this process in a separate thread, so as not to respond without the program interface. System.Net.WebRequest and System.Net.WebResponse can not only be used to get the Internet image, but can also be used to get other resources. If you want to know more, go to see other information such as MSDN.

Author Blog:

http://blog.9cbs.net/yidinghe/

转载请注明原文地址:https://www.9cbs.com/read-120032.html

New Post(0)