HomeSearch

VB.NET Image Type

Use the Image type from the System.Drawing namespace to handle image files like PNGs.
Image. 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. We look at the specific VB.NET code for figuring out image dimensions.

Tip: The Image type is an abstract data type that represents an image. It returns Width and Height and other information about an image.

Also: The Image type stores the image's data in memory after it is read in from the disk.

Example. To start, this program imports the System.Drawing namespace. If this creates an error, go to Add Reference to add the System.Drawing assembly. Upon execution the program loads an image located in a specific file on the disk.
VB.NET program that opens Image 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 Module Output 180 110 {Width=180, Height=110}
After Image.FromFile returns the Image reference, we can access the Width, Height and PhysicalDimension properties. These tell us what the size of the image is. We do not need to read in the bytes from the image or anything else.

Warning: The Image type does not handle all image formats. It works on many popular formats such as PNG and JPG.

Summary. Often when developing programs that use image data, height and width must be determined so that the data can correctly fit into the surrounding content or UI. With the Image type in the System.Drawing namespace, this is possible.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls