Skip to content

Class ImageList

Kristian Virtanen edited this page Oct 14, 2024 · 5 revisions

Description:

ImageList class provides methods to load, store, and retrieving images from local file paths or URLs. It maintains an internal dictionary (images) that associates each image with a unique name, allowing efficient access to the stored images.

Example codes:


Methods:

  • ImageList.LoadImage(fileNameOrUrl)
  • Loads an image from a file path or a URL.
  • The name of the image, which can be used to reference the loaded image in other methods, or an empty string if the image could not be loaded.

  • ImageList.GetWidthOfImage(imageName)
  • Retrieves the width of the stored image in pixels.

  • ImageList.GetHeightOfImage(imageName)
  • Retrieves the height of the stored image in pixels.

Top

Example code in C#:

// Create an alias for the SmallBasicOpenEditionDll namespace
using SB = SmallBasicOpenEditionDll;

namespace SmallBasicOpenEditionDll
{
    static class SB_Program
    {
        static void Main()
        {
            // Load an image from a local file (replace with a valid local path)
            string localImagePath = "C:\\Users\\ekvir\\Downloads\\newBackground.jpg";
            string imageName;

            try
            {
                Console.WriteLine($"Loading image from file: {localImagePath}");
                imageName = ImageList.LoadImage(localImagePath);
                Console.WriteLine($"Image loaded with name: {imageName}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error loading image: {ex.Message}");
                return;
            }

            // Retrieve image dimensions
            try
            {
                int width = ImageList.GetWidthOfImage(imageName);
                int height = ImageList.GetHeightOfImage(imageName);
                Console.WriteLine($"Image '{imageName}' dimensions - Width: {width}, Height: {height}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error retrieving image dimensions: {ex.Message}");
            }

            // Load an image from a URL (replace with a valid URL)
            string imageUrl = "https://codeforum.org/data/styles/97/styles/codeforum/xenforo/codeforum-logo.png";

            try
            {
                Console.WriteLine($"Loading image from URL: {imageUrl}");
                string urlImageName = ImageList.LoadImage(imageUrl);
                Console.WriteLine($"Image loaded from URL with name: {urlImageName}");

                // Retrieve dimensions for the image loaded from the URL
                int urlImageWidth = ImageList.GetWidthOfImage(urlImageName);
                int urlImageHeight = ImageList.GetHeightOfImage(urlImageName);
                Console.WriteLine($"Image '{urlImageName}' dimensions - Width: {urlImageWidth}, Height: {urlImageHeight}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error loading image from URL: {ex.Message}");
            }

            Console.WriteLine("All image operations completed.");
        }
    }
}

Expected output:

Loading image from file: C:\Users\ekvir\Downloads\newBackground.jpg
Image loaded with name: Image0
Image 'Image0' dimensions - Width: 1280, Height: 767
Loading image from URL: https://codeforum.org/data/styles/97/styles/codeforum/xenforo/codeforum-logo.png
Image loaded from URL with name: Image1
Image 'Image1' dimensions - Width: 2273, Height: 407
All image operations completed.

Top

Clone this wiki locally