Introduction
The SkiaSharp SKImageInfo class provides essential metadata about an image, including its width, height, color type, alpha type, and color space. This information is fundamental for various image manipulation tasks within SkiaSharp, such as creating new images, decoding image data, and understanding the properties of existing images.
Key Properties
Width: The width of the image in pixels.Height: The height of the image in pixels.ColorType: Describes the pixel format (e.g.,RGBA_8888,RGB_565).AlphaType: Indicates how the alpha channel is represented (e.g.,Opaque,Premultiplied,Unpremultiplied).ColorSpace: Defines the color gamut of the image.RowBytes: The number of bytes per row of pixels.
Usage Scenarios
SKImageInfo is frequently used when:
- Creating a new
SKBitmaporSKSurfacewith specific dimensions and pixel formats. - Decoding image files to get their properties before loading the pixel data.
- Performing image transformations where the output image needs to have compatible properties.
- Interacting with native Skia graphics contexts.
Core API References
Constructors
Common ways to create an SKImageInfo instance:
SKImageInfo(int width, int height, SKColorType colorType, SKAlphaType alphaType, SKColorSpace colorSpace): The most complete constructor, allowing specification of all key properties.SKImageInfo(int width, int height, SKColorType colorType, SKAlphaType alphaType): Creates an info with the default color space (oftenSRGB).SKImageInfo(int width, int height): Creates an info with default color type (RGBA_8888) and alpha type (Opaque).
Methods
bool IsValid(): Checks if theSKImageInfois valid (e.g., width and height are non-negative).long BytesSize(): Calculates the total size in bytes required to store the image data based on this info.SKImageInfo WithWidth(int width): Returns a newSKImageInfowith the specified width, keeping other properties the same.SKImageInfo WithHeight(int height): Returns a newSKImageInfowith the specified height, keeping other properties the same.SKImageInfo WithColorType(SKColorType colorType): Returns a newSKImageInfowith the specified color type.SKImageInfo WithAlphaType(SKAlphaType alphaType): Returns a newSKImageInfowith the specified alpha type.SKImageInfo WithColorSpace(SKColorSpace colorSpace): Returns a newSKImageInfowith the specified color space.SKImageInfo With গুণ(SKColorType colorType, SKAlphaType alphaType): Returns a newSKImageInfowith specified color and alpha types.SKImageInfo With গুণ(int width, int height): Returns a newSKImageInfowith specified dimensions.
Code Examples
Creating an SKImageInfo
Here's how to create an SKImageInfo for a standard RGBA image:
using SkiaSharp;
// Create an SKImageInfo for a 1920x1080 RGBA image with opaque alpha
var imageInfo = new SKImageInfo(
width: 1920,
height: 1080,
colorType: SKColorType.Rgba8888,
alphaType: SKAlphaType.Opaque
);
Console.WriteLine($"Image Dimensions: {imageInfo.Width}x{imageInfo.Height}");
Console.WriteLine($"Color Type: {imageInfo.ColorType}");
Console.WriteLine($"Alpha Type: {imageInfo.AlphaType}");
Creating a Bitmap with SKImageInfo
Using an SKImageInfo to initialize a new SKBitmap:
using SkiaSharp;
var info = SKImageInfo.Create(256, 256); // Defaults to RGBA_8888, Opaque
var bitmap = new SKBitmap(info);
// Now you can draw onto the bitmap
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(SKColors.CornflowerBlue);
// ... draw other elements
}
// Use the bitmap for rendering or saving
Modifying SKImageInfo
Creating modified copies of an existing SKImageInfo:
using SkiaSharp;
var originalInfo = SKImageInfo.PlatformColorInfo(SKImageInfo.PlatformGenerationType.Explicit, 100, 50);
// Get info with a different color type
var changedColorInfo = originalInfo.WithColorType(SKColorType.Rgb565);
// Get info with a different size
var changedSizeInfo = originalInfo.With গুণ(200, 100);
Console.WriteLine($"Original Info: {originalInfo.Width}x{originalInfo.Height}, {originalInfo.ColorType}");
Console.WriteLine($"Changed Color Info: {changedColorInfo.Width}x{changedColorInfo.Height}, {changedColorInfo.ColorType}");
Console.WriteLine($"Changed Size Info: {changedSizeInfo.Width}x{changedSizeInfo.Height}, {changedSizeInfo.ColorType}");