Read Bytes from AnyBitmap

Working with AnyBitmap Files

Users have the option of manipulating AnyBitmap files in terms of bytes. The IronDrawing library provides methods to Get, Export, and Read bytes of an AnyBitmap file.

The image file to be read can be loaded using a file path with AnyBitmap.FromFile(@"FILE_PATH") and stored in an AnyBitmap variable. This variable can then be manipulated to get bytes using the .GetBytes() method, export bytes with an option to change the image format and degree of image loss using the .ExportBytes(AnyBitmap.ImageFormat.Format, 10) method, and read all bytes of the file using the File.ReadAllBytes(@"FILE_PATH") method.

Example Code

using System;
using IronDrawing; // Assuming this is the correct library for AnyBitmap
using System.IO;

class Program
{
    static void Main()
    {
        // Define the file path for the image to be loaded
        string filePath = @"path\to\your\image.bmp";

        // Load the image into an AnyBitmap object
        AnyBitmap bitmap = AnyBitmap.FromFile(filePath);

        // Getting bytes from the AnyBitmap
        byte[] imageBytes = bitmap.GetBytes();
        Console.WriteLine("Bytes retrieved from AnyBitmap.");

        // Exporting bytes with specified format and quality
        byte[] exportedBytes = bitmap.ExportBytes(AnyBitmap.ImageFormat.Jpeg, 10); // JPEG format with quality factor 10
        Console.WriteLine("Exported bytes with specified format and quality.");

        // Reading all bytes directly from the file
        byte[] fileBytes = File.ReadAllBytes(filePath);
        Console.WriteLine("Read all bytes from the file.");

        // Printing number of bytes retrieved in each case
        Console.WriteLine($"Image Bytes Length: {imageBytes.Length}");
        Console.WriteLine($"Exported Bytes Length: {exportedBytes.Length}");
        Console.WriteLine($"File Bytes Length: {fileBytes.Length}");
    }
}
using System;
using IronDrawing; // Assuming this is the correct library for AnyBitmap
using System.IO;

class Program
{
    static void Main()
    {
        // Define the file path for the image to be loaded
        string filePath = @"path\to\your\image.bmp";

        // Load the image into an AnyBitmap object
        AnyBitmap bitmap = AnyBitmap.FromFile(filePath);

        // Getting bytes from the AnyBitmap
        byte[] imageBytes = bitmap.GetBytes();
        Console.WriteLine("Bytes retrieved from AnyBitmap.");

        // Exporting bytes with specified format and quality
        byte[] exportedBytes = bitmap.ExportBytes(AnyBitmap.ImageFormat.Jpeg, 10); // JPEG format with quality factor 10
        Console.WriteLine("Exported bytes with specified format and quality.");

        // Reading all bytes directly from the file
        byte[] fileBytes = File.ReadAllBytes(filePath);
        Console.WriteLine("Read all bytes from the file.");

        // Printing number of bytes retrieved in each case
        Console.WriteLine($"Image Bytes Length: {imageBytes.Length}");
        Console.WriteLine($"Exported Bytes Length: {exportedBytes.Length}");
        Console.WriteLine($"File Bytes Length: {fileBytes.Length}");
    }
}
Imports System
Imports IronDrawing ' Assuming this is the correct library for AnyBitmap
Imports System.IO

Friend Class Program
	Shared Sub Main()
		' Define the file path for the image to be loaded
		Dim filePath As String = "path\to\your\image.bmp"

		' Load the image into an AnyBitmap object
		Dim bitmap As AnyBitmap = AnyBitmap.FromFile(filePath)

		' Getting bytes from the AnyBitmap
		Dim imageBytes() As Byte = bitmap.GetBytes()
		Console.WriteLine("Bytes retrieved from AnyBitmap.")

		' Exporting bytes with specified format and quality
		Dim exportedBytes() As Byte = bitmap.ExportBytes(AnyBitmap.ImageFormat.Jpeg, 10) ' JPEG format with quality factor 10
		Console.WriteLine("Exported bytes with specified format and quality.")

		' Reading all bytes directly from the file
		Dim fileBytes() As Byte = File.ReadAllBytes(filePath)
		Console.WriteLine("Read all bytes from the file.")

		' Printing number of bytes retrieved in each case
		Console.WriteLine($"Image Bytes Length: {imageBytes.Length}")
		Console.WriteLine($"Exported Bytes Length: {exportedBytes.Length}")
		Console.WriteLine($"File Bytes Length: {fileBytes.Length}")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation

  • Loading an Image:
    The AnyBitmap.FromFile method is used to load an image file from a specified file path into an AnyBitmap object.

  • Getting Bytes:
    The GetBytes() method retrieves the byte array representation of the AnyBitmap image.

  • Exporting Bytes:
    The ExportBytes() method exports the image as a byte array with the option to specify the ImageFormat (e.g., JPEG, PNG) and the quality factor (if applicable to the format).

  • Reading File Bytes:
    The File.ReadAllBytes() method reads the entire file as a byte array directly from the file path.

This code provides a basic understanding of how to manipulate image files at the byte level using AnyBitmap from the assumed IronDrawing library in C#.