Bilddatei aus dem Internet Herunterladen

Funktionsbeispiel




Imports System.Net
Public
Class Form1
Inherits
System.Windows.Forms.Form
#Region
" Vom Windows Form Designer generierter Code "
Public
Sub
New()
MyBase.New()
Dieser Aufruf ist für den Windows
Form-Designer erforderlich.
InitializeComponent()
Initialisierungen nach dem Aufruf
InitializeComponent() hinzufügen
End
Sub
Die Form überschreibt den Löschvorgang der
Basisklasse, um Komponenten zu bereinigen.
Protected
Overloads
Overrides
Sub Dispose(ByVal
disposing As
Boolean)
If disposing
Then
If
Not (components
Is
Nothing)
Then
components.Dispose()
End
If
End
If
MyBase.Dispose(disposing)
End
Sub
Für Windows Form-Designer erforderlich
Private components
As
System.ComponentModel.IContainer
HINWEIS: Die folgende Prozedur ist für den
Windows Form-Designer erforderlich
Sie kann mit dem Windows Form-Designer
modifiziert werden.
Verwenden Sie nicht den Code-Editor zur
Bearbeitung.
Friend
WithEvents TxtUrl
As
System.Windows.Forms.TextBox
Friend
WithEvents BtnStart
As
System.Windows.Forms.Button
Friend
WithEvents PicImage
As
System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()>
Private
Sub InitializeComponent()
Me.TxtUrl =
New
System.Windows.Forms.TextBox()
Me.BtnStart =
New
System.Windows.Forms.Button()
Me.PicImage =
New
System.Windows.Forms.PictureBox()
Me.SuspendLayout()

TxtUrl

Me.TxtUrl.Anchor =
((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) _
Or
System.Windows.Forms.AnchorStyles.Right)
Me.TxtUrl.Location =
New
System.Drawing.Point(32, 8)
Me.TxtUrl.Name =
"TxtUrl"
Me.TxtUrl.Size =
New
System.Drawing.Size(232, 20)
Me.TxtUrl.TabIndex = 1
Me.TxtUrl.Text =
"http://www."

BtnStart

Me.BtnStart.Anchor =
System.Windows.Forms.AnchorStyles.Bottom
Me.BtnStart.Location =
New
System.Drawing.Point(112, 240)
Me.BtnStart.Name =
"BtnStart"
Me.BtnStart.TabIndex = 2
Me.BtnStart.Text =
"Starten"

PicImage

Me.PicImage.Anchor =
(((System.Windows.Forms.AnchorStyles.Top
Or System.Windows.Forms.AnchorStyles.Bottom) _
Or
System.Windows.Forms.AnchorStyles.Left) _
Or
System.Windows.Forms.AnchorStyles.Right)
Me.PicImage.BorderStyle
= System.Windows.Forms.BorderStyle.FixedSingle
Me.PicImage.Location =
New
System.Drawing.Point(8, 40)
Me.PicImage.Name =
"PicImage"
Me.PicImage.Size =
New
System.Drawing.Size(280, 192)
Me.PicImage.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PicImage.TabIndex = 3
Me.PicImage.TabStop =
False

Form1

Me.AutoScaleBaseSize =
New
System.Drawing.Size(5, 13)
Me.ClientSize =
New
System.Drawing.Size(292, 266)
Me.Controls.AddRange(New
System.Windows.Forms.Control() {Me.PicImage,
Me.BtnStart,
Me.TxtUrl})
Me.Name = "Form1"
Me.Text = "ImageFromWeb"
Me.ResumeLayout(False)
End
Sub
#End
Region
Private
Sub Form1_Load(ByVal
sender As
System.Object, ByVal
e As
System.EventArgs) Handles
MyBase.Load
End
Sub




Private
Sub BtnStart_Click(ByVal
sender As
System.Object, ByVal
e As
System.EventArgs) Handles
BtnStart.Click
PicImage.Image = GetImage(TxtUrl.Text)
End
Sub
Funktion Image Herunterladen
Private
Function GetImage(ByVal
Url As
String)
As Image
Try
Dim Img
As Image
Image in Img speichern
Dim WebReq
As WebRequest =
WebRequest.Create(Url)
WebReq.Timeout = 10000
Vorgang nach 10 Sekunden beenden
Dim WebRes
As WebResponse =
WebReq.GetResponse
Dim Strm
As IO.Stream =
WebRes.GetResponseStream Stream in Strm
speichern
Img = Image.FromStream(Strm)
Strm.Close()
GetImage = Img Ausgabe
als GetImage
Catch Ex
As Exception
MsgBox(Ex.Message) Bei
Fehler Nachricht anzeigen
End
Try
End
Function
End
Class