An Image has a width and a height. With the Image type, found in System.Drawing
, we load an image file and quickly determine these values.
The Image type is an abstract
data type that represents an image. It returns the data, Width and Height and other information about an image.
To start, this program imports the System.Drawing
namespace. If this creates an error, go to Add Reference to add the System.Drawing
assembly.
Image.FromFile
returns the Image reference, we can access the Width, Height and PhysicalDimension
properties.Imports System.Drawing Module Module1 Sub Main() ' Load Image from file. Dim i As Image = Image.FromFile("C:\background1.png", False) ' Print dimensions of Image. Console.WriteLine(i.Width) Console.WriteLine(i.Height) Console.WriteLine(i.PhysicalDimension) End Sub End Module180 110 {Width=180, Height=110}
Image does not handle all image formats, but it works on common ones such as PNG and JPG. We can use Image to read in width and height, and output that to HTML.
Image can be helpful for getting image information. Often height and width must be determined so that the data can correctly fit into the surrounding content or UI.