Cast to AnyBitmap

In order to use and manipulate any image file in IronDrawing, all files must be converted to an AnyBitmap file. For files from other platforms, IronDrawing can cast these files to AnyBitmap files. Below are examples of how image files from other platforms can be cast to an AnyBitmap file.

System.Drawing.Bitmap

A bitmap from System.Drawing can be cast to an AnyBitmap file. Begin by loading a System Drawing file from a file path using System.Drawing.Bitmap, then cast the file by assigning it to a variable initialized with the AnyBitmap type.

// Load a bitmap from System.Drawing and cast it to AnyBitmap
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(@"FILE_PATH");
AnyBitmap anyBitmap = bitmap; // Cast to AnyBitmap
// Load a bitmap from System.Drawing and cast it to AnyBitmap
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(@"FILE_PATH");
AnyBitmap anyBitmap = bitmap; // Cast to AnyBitmap
$vbLabelText   $csharpLabel

System.Drawing.Image

Casting images from System Image can be done by loading the file from a file path using System.Drawing.Image.FromFile and storing it in a variable with the AnyBitmap type.

// Load an image from System.Drawing and cast it to AnyBitmap
System.Drawing.Image image = System.Drawing.Image.FromFile(@"FILE_PATH");
AnyBitmap anyBitmap = image; // Cast to AnyBitmap
// Load an image from System.Drawing and cast it to AnyBitmap
System.Drawing.Image image = System.Drawing.Image.FromFile(@"FILE_PATH");
AnyBitmap anyBitmap = image; // Cast to AnyBitmap
$vbLabelText   $csharpLabel

SkiaSharp.SKBitmap

Casting a bitmap from SkiaSharp can be done by initializing a SkiaSharp.SKBitmap variable and storing the file loaded from a file path using SkiaSharp.SKBitmap.Decode. The file can then be cast to an AnyBitmap type.

// Load a bitmap from SkiaSharp and cast it to AnyBitmap
SkiaSharp.SKBitmap skBitmap = SkiaSharp.SKBitmap.Decode(@"FILE_PATH");
AnyBitmap anyBitmap = skBitmap; // Cast to AnyBitmap
// Load a bitmap from SkiaSharp and cast it to AnyBitmap
SkiaSharp.SKBitmap skBitmap = SkiaSharp.SKBitmap.Decode(@"FILE_PATH");
AnyBitmap anyBitmap = skBitmap; // Cast to AnyBitmap
$vbLabelText   $csharpLabel

SkiaSharp.SKImage

To load a SkiaSharp image, load the file using SkiaSharp.SKImage.FromBitmap from a decoded SKBitmap, then store it inside a SkiaSharp.SKImage variable. It can be cast to an AnyBitmap file by assigning it to a variable of the AnyBitmap type.

// Load an image from SkiaSharp and cast it to AnyBitmap
SkiaSharp.SKImage skImage = SkiaSharp.SKImage.FromBitmap(SkiaSharp.SKBitmap.Decode(@"FILE_PATH"));
AnyBitmap anyBitmap = skImage; // Cast to AnyBitmap
// Load an image from SkiaSharp and cast it to AnyBitmap
SkiaSharp.SKImage skImage = SkiaSharp.SKImage.FromBitmap(SkiaSharp.SKBitmap.Decode(@"FILE_PATH"));
AnyBitmap anyBitmap = skImage; // Cast to AnyBitmap
$vbLabelText   $csharpLabel

SixLabors

Images from SixLabors can be loaded from a file path using SixLabors.ImageSharp.Image.Load<SixLabors.ImageSharp.PixelFormats.Rgba32> and stored in a SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgba32> variable. It can then be cast to an AnyBitmap file.

// Load an image from SixLabors and cast it to AnyBitmap
var sixLaborsImage = SixLabors.ImageSharp.Image.Load<SixLabors.ImageSharp.PixelFormats.Rgba32>(@"FILE_PATH");
AnyBitmap anyBitmap = sixLaborsImage; // Cast to AnyBitmap
// Load an image from SixLabors and cast it to AnyBitmap
var sixLaborsImage = SixLabors.ImageSharp.Image.Load<SixLabors.ImageSharp.PixelFormats.Rgba32>(@"FILE_PATH");
AnyBitmap anyBitmap = sixLaborsImage; // Cast to AnyBitmap
$vbLabelText   $csharpLabel

MAUI Image

Casting a MAUI image to an AnyBitmap file can only be done in environments other than NET4.7.2. A MAUI image file can be loaded by reading the bytes of the image obtained from a file path using File.ReadAllBytes, then storing the bytes in a byte[] array. The bytes must be translated into an image using Microsoft.Maui.Graphics.Platform.PlatformImage.FromStream and can then be cast to an AnyBitmap file.

// Load an image from MAUI and cast it to AnyBitmap (works in environments other than NET4.7.2)
byte[] imageData = File.ReadAllBytes(@"FILE_PATH");
var mauiImage = Microsoft.Maui.Graphics.Platform.PlatformImage.FromStream(new MemoryStream(imageData));
AnyBitmap anyBitmap = mauiImage; // Cast to AnyBitmap
// Load an image from MAUI and cast it to AnyBitmap (works in environments other than NET4.7.2)
byte[] imageData = File.ReadAllBytes(@"FILE_PATH");
var mauiImage = Microsoft.Maui.Graphics.Platform.PlatformImage.FromStream(new MemoryStream(imageData));
AnyBitmap anyBitmap = mauiImage; // Cast to AnyBitmap
$vbLabelText   $csharpLabel