Table of Contents

Class Bitmap

Namespace
Avalonia.Media.Imaging
Assembly
Avalonia.Base.dll

Holds a bitmap image.

public class Bitmap : IImage, IDisposable, IImageBrushSource
Inheritance
Bitmap
Implements
Derived
Inherited Members

Constructors

Bitmap(IBitmapImpl)

Initializes a new instance of the Bitmap class.

protected Bitmap(IBitmapImpl impl)

Parameters

impl IBitmapImpl

A platform-specific bitmap implementation. Bitmap class takes the ownership.

Bitmap(PixelFormat, AlphaFormat, nint, PixelSize, Vector, int)

Initializes a new instance of the Bitmap class.

public Bitmap(PixelFormat format, AlphaFormat alphaFormat, nint data, PixelSize size, Vector dpi, int stride)

Parameters

format PixelFormat

The pixel format.

alphaFormat AlphaFormat

The alpha format.

data nint

The pointer to the source bytes.

size PixelSize

The size of the bitmap in device pixels.

dpi Vector

The DPI of the bitmap.

stride int

The number of bytes per row.

Bitmap(Stream)

Initializes a new instance of the Bitmap class.

public Bitmap(Stream stream)

Parameters

stream Stream

The stream to read the bitmap from.

Bitmap(string)

Initializes a new instance of the Bitmap class.

public Bitmap(string fileName)

Parameters

fileName string

The filename of the bitmap.

Properties

AlphaFormat

public virtual AlphaFormat? AlphaFormat { get; }

Property Value

AlphaFormat?

Dpi

Gets the dots per inch (DPI) of the image.

public Vector Dpi { get; }

Property Value

Vector

Remarks

Note that Skia does not currently support reading the DPI of an image so this value will always be 96dpi on Skia.

Format

public virtual PixelFormat? Format { get; }

Property Value

PixelFormat?

PixelSize

Gets the size of the bitmap, in device pixels.

public PixelSize PixelSize { get; }

Property Value

PixelSize

Size

Gets the size of the image, in device independent pixels.

public Size Size { get; }

Property Value

Size

Methods

CopyPixels(PixelRect, nint, int, int)

public virtual void CopyPixels(PixelRect sourceRect, nint buffer, int bufferSize, int stride)

Parameters

sourceRect PixelRect
buffer nint
bufferSize int
stride int

CopyPixels(ILockedFramebuffer, AlphaFormat)

Copies pixels to the target buffer and transcodes the pixel and alpha format if needed.

public void CopyPixels(ILockedFramebuffer buffer, AlphaFormat alphaFormat)

Parameters

buffer ILockedFramebuffer

The target buffer.

alphaFormat AlphaFormat

The alpha format.

Exceptions

NotSupportedException

CreateScaledBitmap(PixelSize, BitmapInterpolationMode)

Creates a Bitmap scaled to a specified size from the current bitmap.

public Bitmap CreateScaledBitmap(PixelSize destinationSize, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)

Parameters

destinationSize PixelSize

The destination size.

interpolationMode BitmapInterpolationMode

The BitmapInterpolationMode to use should any scaling be required.

Returns

Bitmap

An instance of the Bitmap class.

DecodeToHeight(Stream, int, BitmapInterpolationMode)

Loads a Bitmap from a stream and decodes at the desired height. Aspect ratio is maintained. This is more efficient than loading and then resizing.

public static Bitmap DecodeToHeight(Stream stream, int height, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)

Parameters

stream Stream

The stream to read the bitmap from. This can be any supported image format.

height int

The desired height of the resulting bitmap.

interpolationMode BitmapInterpolationMode

The BitmapInterpolationMode to use should any scaling be required.

Returns

Bitmap

An instance of the Bitmap class.

DecodeToWidth(Stream, int, BitmapInterpolationMode)

Loads a Bitmap from a stream and decodes at the desired width. Aspect ratio is maintained. This is more efficient than loading and then resizing.

public static Bitmap DecodeToWidth(Stream stream, int width, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)

Parameters

stream Stream

The stream to read the bitmap from. This can be any supported image format.

width int

The desired width of the resulting bitmap.

interpolationMode BitmapInterpolationMode

The BitmapInterpolationMode to use should any scaling be required.

Returns

Bitmap

An instance of the Bitmap class.

Dispose()

public virtual void Dispose()

Save(Stream, int?)

Saves the bitmap to a stream.

public void Save(Stream stream, int? quality = null)

Parameters

stream Stream

The stream.

quality int?

The optional quality for compression. The quality value is interpreted from 0 - 100. If quality is null the default quality setting is applied.

Save(string, int?)

Saves the bitmap to a file.

public void Save(string fileName, int? quality = null)

Parameters

fileName string

The filename.

quality int?

The optional quality for compression. The quality value is interpreted from 0 - 100. If quality is null the default quality setting is applied.