How to display an image in vbscript?
Categories:
Displaying Images in VBScript: A Comprehensive Guide

Learn various methods to display images in VBScript applications, from embedding in HTML to using ActiveX controls and external viewers.
VBScript, primarily used for client-side scripting in web browsers (Internet Explorer) and Windows Script Host (WSH) environments, doesn't have a direct, built-in function to 'display an image' in the same way a modern programming language might. Instead, it leverages other technologies like HTML, ActiveX controls, or external applications. This article explores the common and effective ways to achieve image display within a VBScript context, catering to different application scenarios.
Method 1: Embedding Images in HTML (for Web Pages)
When VBScript is used within an HTML page, the most straightforward way to display an image is to manipulate the Document Object Model (DOM) to insert or modify an <img>
tag. VBScript can dynamically create image elements, set their source, and append them to the document.
<!DOCTYPE html>
<html>
<head>
<title>VBScript Image Display</title>
<script type="text/vbscript">
Sub DisplayImage()
Dim imgElement
Set imgElement = document.createElement("img")
imgElement.src = "https://via.placeholder.com/150"
imgElement.alt = "Placeholder Image"
imgElement.style.border = "2px solid blue"
imgElement.style.margin = "10px"
document.body.appendChild imgElement
End Sub
</script>
</head>
<body onload="DisplayImage()">
<h1>Image Displayed via VBScript</h1>
</body>
</html>
VBScript code to dynamically create and display an image within an HTML page.
Method 2: Using ActiveX Controls (for WSH or IE)
In Windows Script Host (WSH) environments or within Internet Explorer, VBScript can interact with ActiveX controls. The Microsoft Forms 2.0 Image
control (often referred to as Forms.Image
) is a common choice for displaying images. This control provides properties to load and display various image formats.
flowchart TD A[VBScript Execution] --> B{CreateObject("Forms.Image.1")} B --> C[Set .Picture property] C --> D[LoadPicture("C:\path\to\image.jpg")] D --> E[Display Image (e.g., in UserForm or HTML)]
Process flow for displaying an image using the Forms.Image ActiveX control.
' Example for WSH (requires a host application like an HTA or a custom form)
' This code snippet demonstrates the core logic, but a full WSH example
' would require creating a GUI element to host the image control.
' In an HTA (HTML Application) file:
' <HTML>
' <HEAD>
' <HTA:APPLICATION WINDOWSTATE="normal" BORDER="thin" />
' <TITLE>Image Viewer</TITLE>
' <SCRIPT LANGUAGE="VBScript">
' Sub Window_OnLoad
' Set img = document.createElement("img")
' img.id = "myImage"
' document.body.appendChild img
' DisplayImage
' End Sub
'
' Sub DisplayImage
' Dim objImage
' Set objImage = CreateObject("Forms.Image.1")
' ' Load an image (replace with your actual path)
' objImage.Picture = LoadPicture("C:\Users\Public\Pictures\Sample Pictures\Koala.jpg")
'
' ' To display it, you'd typically embed it in an HTML element
' ' For a simple HTA, you might need to convert it or use a different approach
' ' This example focuses on loading into the ActiveX control.
' ' For direct display in an HTA, it's often easier to use an <img> tag directly.
' ' The Forms.Image control is more common in VBA UserForms or specific IE scenarios.
' End Sub
' </SCRIPT>
' </HEAD>
' <BODY>
' <H1>Image Loaded (check script for details)</H1>
' </BODY>
' </HTML>
' For a more direct WSH approach without an HTA, you'd typically launch an external viewer.
' The Forms.Image control is best used within a container that supports it, like an HTA
' with an <object> tag or a VBA UserForm.
Conceptual VBScript code for using the Forms.Image ActiveX control. Note that direct WSH display requires a host.
Method 3: Launching an External Image Viewer (for WSH)
When working with VBScript in a Windows Script Host (WSH) environment, if the goal is simply to open an image file for viewing, the most robust and simplest approach is to launch an external image viewer application. This leverages the operating system's default file associations.
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
Dim imagePath
imagePath = "C:\Users\Public\Pictures\Sample Pictures\Desert.jpg" ' Replace with your image path
' Check if the file exists before attempting to open
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(imagePath) Then
WshShell.Run imagePath, 1, False ' 1 for normal window, False for not waiting
WScript.Echo "Image opened successfully: " & imagePath
Else
WScript.Echo "Error: Image file not found at " & imagePath
End If
Set fso = Nothing
Set WshShell = Nothing
VBScript code to open an image file using the default system viewer.
Method 4: Displaying in an HTA (HTML Application)
HTML Applications (HTAs) combine the power of HTML for UI with VBScript (or JScript) for scripting, running as trusted applications on a Windows machine. This allows for a rich user interface where images can be easily embedded using standard HTML <img>
tags, with VBScript controlling their properties.
<HTML>
<HEAD>
<HTA:APPLICATION WINDOWSTATE="normal" BORDER="thin" />
<TITLE>HTA Image Viewer</TITLE>
<SCRIPT LANGUAGE="VBScript">
Sub Window_OnLoad()
' Set initial image source
document.getElementById("myImage").src = "https://via.placeholder.com/200/FF0000/FFFFFF?text=HTA+Image"
End Sub
Sub ChangeImage()
Dim newImagePath
newImagePath = InputBox("Enter new image URL or local path:", "Change Image", "https://via.placeholder.com/200/0000FF/FFFFFF?text=New+Image")
If newImagePath <> "" Then
document.getElementById("myImage").src = newImagePath
End If
End Sub
</SCRIPT>
</HEAD>
<BODY>
<h1>HTA Image Display Example</h1>
<img id="myImage" src="" alt="Dynamic Image" style="border: 3px solid green; margin: 15px;">
<br>
<button onclick="ChangeImage()">Change Image</button>
</BODY>
</HTML>
An HTA example demonstrating dynamic image display and modification using VBScript.