Rubrik: HTML/Internet/Netzwerk · Sonstiges | VB-Versionen: VB4, VB5, VB6 | 31.03.03 |
Online-Bild in der Picture/ImageBox anzeigen Hier erfahren Sie, wie sich Bilder aus dem Internet in einer VB-Picture-/ImageBox anzeigen lassen. | ||
Autor: Dieter Otter | Bewertung: | Views: 24.491 |
www.tools4vb.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Haben Sie sich auch schon einmal gefragt, wie man Bilder aus dem Internet in einer VB-PictureBox bzw. ImageBox anzeigen kann?
Ihr erster Gedanke war evtl. folgende Variante:
Image1.Picture = LoadPicture("http://www.domain.de/bild1.jpg")
Leider unterstützt VB diese Variante nicht
Abhilfe schafft hier folgende Überlegung:
- Download des Bildes und lokal (temporär) zwischenspeichern
- temporäre Bilddatei über LoadPicture in die PictureBox/ImageBox laden
- Löschen der temporären Datei
Das ganze lässt sich dann in Visual Basic wie folgt umsetzen:
Option Explicit ' Benötigte API-Funktionen Private Declare Function URLDownloadToFile Lib "urlmon" _ Alias "URLDownloadToFileA" ( _ ByVal pCaller As Long, _ ByVal szURL As String, _ ByVal szFileName As String, _ ByVal dwReserved As Long, _ ByVal lpfnCB As Long) As Long Private Declare Function GetTempFileName Lib "kernel32" _ Alias "GetTempFileNameA" ( _ ByVal lpszPath As String, _ ByVal lpPrefixString As String, _ ByVal wUnique As Long, _ ByVal lpTempFileName As String) As Long
' Bild downloaden und zwischenspeichern Public Sub LoadWebPicture(oPicControl As Control, _ ByVal sURL As String, _ Optional ByVal sLocalFile As String = "", _ Optional ByVal bKillLocalFile As Boolean = True) Dim sTempFile As String Dim sTempPath As String Dim nResult As Long ' Falls kein lokaler Dateinamen zum Zwischenspeichern ' angegeben wurde, wird ein temporärer Dateiname ' erzeugt If sLocalFile = "" Then sTempPath = Environ$("TEMP") sTempFile = Space$(256) Call GetTempFileName(sTempPath, "pic", 0&, sTempFile) sLocalFile = Left$(sTempFile, InStr(sTempFile, Chr$(0)) - 1) End If ' Bilddatei downloaden nResult = URLDownloadToFile(0, sURL, sLocalFile, 0, 0) ' Bild in die Picture/ImageBox laden On Error Resume Next oPicControl.Picture = LoadPicture(sLocalFile) If Err <> 0 Then oPicControl.Picture = LoadPicture() On Error Goto 0 ' temporäre Datei ggf. löschen If bKillLocalFile Then Kill sLocalFile End Sub
Beispiel:
Starten Sie ein neues Projekt und fügen obigen Code z.B. in ein Modul oder der Form1 ein. Ziehen Sie auf die Form1 eine ImageBox, sowie einen CommandButton.
Private Sub Command1_Click() LoadWebPicture Image1, _ "http://www.vbarchiv.net/partner/images/cd2_200x166_1.jpg" End Sub