Class StbImage
A class that encompasses all features of stb_image.h in a safe way.
public class StbImage : IDisposable
- Inheritance
-
StbImage
- Implements
- Inherited Members
Properties
FlipVerticallyOnLoad
Set to flip the y-axis of loaded images on load.
public static bool FlipVerticallyOnLoad { set; }
Property Value
FlipVerticallyOnSave
Set to flip the y-axis of saved images.
public static bool FlipVerticallyOnSave { set; }
Property Value
Format
Internal image format.
public StbiImageFormat Format { get; }
Property Value
Height
Height of the image.
public int Height { get; }
Property Value
ImagePointer
Pointer to the image.
public IntPtr ImagePointer { get; }
Property Value
IsFloat
True if the image is a floating point image.
public bool IsFloat { get; }
Property Value
UnpremultiplyOnLoad
Set to unpremultiply images on load.
public static bool UnpremultiplyOnLoad { set; }
Property Value
Remarks
According to the stb_image documentation, only iPhone PNG images can come with premultiplied alpha.
Width
Width of the image.
public int Width { get; }
Property Value
WriteForcePngFilter
Force a filter on PNG filter when saving.
public static int WriteForcePngFilter { get; set; }
Property Value
Remarks
-1 for auto, 0 through 5 to pick a filter. Higher is more. Not thread safe.
WritePngCompressionLevel
Change the PNG compression level on save.
public static int WritePngCompressionLevel { get; set; }
Property Value
Remarks
Higher is more. Defaults to 8. Not thread safe.
WriteTgaEnableRLE
Enable run length encoding on TGA images on save.
public static bool WriteTgaEnableRLE { get; set; }
Property Value
Remarks
Not thread safe.
Methods
AsSpan<T>()
Get a safe span to the image pointer.
public ReadOnlySpan<T> AsSpan<T>() where T : unmanaged
Returns
- ReadOnlySpan<T>
A span to the image data.
Type Parameters
T
The pixel type.
Exceptions
- Exception
The image uses an unexpected image format.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
~StbImage()
protected ~StbImage()
Load(Stream, StbiImageFormat, bool)
Load an image.
public static StbImage Load(Stream stream, StbiImageFormat format = StbiImageFormat.Default, bool asFloat = false)
Parameters
stream
StreamThe stream to load from.
format
StbiImageFormatThe desired image format.
asFloat
bool
Returns
- StbImage
The image object.
Load(ReadOnlySpan<byte>, StbiImageFormat, bool)
Load an image.
public static StbImage Load(ReadOnlySpan<byte> span, StbiImageFormat format = StbiImageFormat.Default, bool asFloat = false)
Parameters
span
ReadOnlySpan<byte>The span of memory to load from.
format
StbiImageFormatThe desired image format.
asFloat
bool
Returns
- StbImage
The image object.
PeekInfo(Stream, out int, out int, out StbiImageFormat)
Peek image info from a stream.
public static bool PeekInfo(Stream stream, out int width, out int height, out StbiImageFormat format)
Parameters
stream
StreamThe stream to peek into.
width
intWidth of the image.
height
intHeight of the image.
format
StbiImageFormatThe image format.
Returns
- bool
True if the stream contained an image.
PeekInfo<T>(ReadOnlySpan<T>, out int, out int, out StbiImageFormat)
Peek image info from a span.
public static bool PeekInfo<T>(ReadOnlySpan<T> span, out int width, out int height, out StbiImageFormat format) where T : unmanaged
Parameters
span
ReadOnlySpan<T>The span to peek into.
width
intWidth of the image.
height
intHeight of the image.
format
StbiImageFormatThe image format.
Returns
- bool
True if the stream contained an image.
Type Parameters
T
TryLoad(out StbImage?, Stream, StbiImageFormat, bool)
Try loading an image, without raising exceptions.
public static bool TryLoad(out StbImage? image, Stream stream, StbiImageFormat format = StbiImageFormat.Default, bool asFloat = false)
Parameters
image
StbImageThe resulting image.
stream
StreamSource stream.
format
StbiImageFormatThe desired image format.
asFloat
bool
Returns
- bool
True on success.
TryLoad(out StbImage?, ReadOnlySpan<byte>, StbiImageFormat, bool)
Try loading an image, without raising exceptions.
public static bool TryLoad(out StbImage? image, ReadOnlySpan<byte> span, StbiImageFormat format = StbiImageFormat.Default, bool asFloat = false)
Parameters
image
StbImageThe resulting image.
span
ReadOnlySpan<byte>Source memory span.
format
StbiImageFormatThe desired image format.
asFloat
bool
Returns
- bool
True on success.
WriteBmp(Stream)
Write image to a BMP file.
public void WriteBmp(Stream dest)
Parameters
dest
StreamThe destination stream.
Remarks
Incurs a conversion cost if the image format is not a byte format. Affected by non-thread safe global options.
WriteBmp<T>(ReadOnlySpan<T>, int, int, StbiImageFormat, Stream, bool)
Write any image to a BMP file.
public static void WriteBmp<T>(ReadOnlySpan<T> data, int width, int height, StbiImageFormat format, Stream destination, bool isFloat = false) where T : unmanaged
Parameters
data
ReadOnlySpan<T>Span of pixel data.
width
intWidth of the pixel data in pixels.
height
intHeight of the pixel data in pixels.
format
StbiImageFormatColor format of the pixel data. Must not be StbiImageFormat().
destination
StreamThe destination stream.
isFloat
boolTrue if the pixel format in data is a floating point format.
Type Parameters
T
Any packed byte or float array structure.
Remarks
This will incur a conversion cost if the pixel format is not a byte format. Ignores the alpha channel. Affected by global non-thread safe options.
WriteHdr(Stream)
Write image to a PNG file.
public void WriteHdr(Stream dest)
Parameters
dest
StreamThe destination stream.
Remarks
Incurs a conversion cost if the image format is not a float format. Affected by non-thread safe global options.
WriteHdr<T>(ReadOnlySpan<T>, int, int, StbiImageFormat, Stream, bool)
Write any image to a PNG file.
public static void WriteHdr<T>(ReadOnlySpan<T> data, int width, int height, StbiImageFormat format, Stream destination, bool isFloat = false) where T : unmanaged
Parameters
data
ReadOnlySpan<T>Span of pixel data.
width
intWidth of the pixel data in pixels.
height
intHeight of the pixel data in pixels.
format
StbiImageFormatColor format of the pixel data. Must not be StbiImageFormat().
destination
StreamThe destination stream.
isFloat
boolTrue if the pixel format in data is a floating point format.
Type Parameters
T
Any packed byte or float array structure.
Remarks
This will incur a conversion cost if the pixel format is not a float format. Affected by global non-thread safe options.
WriteJpg(Stream, int)
Write image to a PNG file.
public void WriteJpg(Stream dest, int quality = 90)
Parameters
Remarks
Incurs a conversion cost if the image format is not a byte format. Ignores alpha channel. Affected by non-thread safe global options.
WriteJpg<T>(ReadOnlySpan<T>, int, int, StbiImageFormat, Stream, int, bool)
Write any image to a PNG file.
public static void WriteJpg<T>(ReadOnlySpan<T> data, int width, int height, StbiImageFormat format, Stream destination, int quality = 90, bool isFloat = false) where T : unmanaged
Parameters
data
ReadOnlySpan<T>Span of pixel data.
width
intWidth of the pixel data in pixels.
height
intHeight of the pixel data in pixels.
format
StbiImageFormatColor format of the pixel data. Must not be StbiImageFormat().
destination
StreamThe destination stream.
quality
intisFloat
boolTrue if the pixel format in data is a floating point format.
Type Parameters
T
Any packed byte or float array structure.
Remarks
This will incur a conversion cost if the pixel format is not a byte format. Ignores the alpha channel. Affected by global non-thread safe options.
WritePng(Stream)
Write image to a PNG file.
public void WritePng(Stream dest)
Parameters
dest
StreamThe destination stream.
Remarks
Incurs a conversion cost if the image format is not a byte format. Affected by non-thread safe global options.
WritePng<T>(ReadOnlySpan<T>, int, int, StbiImageFormat, Stream, bool)
Write any image to a PNG file.
public static void WritePng<T>(ReadOnlySpan<T> data, int width, int height, StbiImageFormat format, Stream destination, bool isFloat = false) where T : unmanaged
Parameters
data
ReadOnlySpan<T>Span of pixel data.
width
intWidth of the pixel data in pixels.
height
intHeight of the pixel data in pixels.
format
StbiImageFormatColor format of the pixel data. Must not be StbiImageFormat().
destination
StreamThe destination stream.
isFloat
boolTrue if the pixel format in data is a floating point format.
Type Parameters
T
Any packed byte or float array structure.
Remarks
This will incur a conversion cost if the pixel format is not a byte format. Affected by global non-thread safe options.
WriteTga(Stream)
Write image to a TGA file.
public void WriteTga(Stream dest)
Parameters
dest
StreamThe destination stream.
Remarks
Incurs a conversion cost if the image format is not a byte format. Ignores alpha channel. Affected by non-thread safe global options.
WriteTga<T>(ReadOnlySpan<T>, int, int, StbiImageFormat, Stream, bool)
Write any image to a TGA file.
public static void WriteTga<T>(ReadOnlySpan<T> data, int width, int height, StbiImageFormat format, Stream destination, bool isFloat = false) where T : unmanaged
Parameters
data
ReadOnlySpan<T>Span of pixel data.
width
intWidth of the pixel data in pixels.
height
intHeight of the pixel data in pixels.
format
StbiImageFormatColor format of the pixel data. Must not be StbiImageFormat().
destination
StreamThe destination stream.
isFloat
boolTrue if the pixel format in data is a floating point format.
Type Parameters
T
Any packed byte or float array structure.
Remarks
This will incur a conversion cost if the pixel format is not a byte format. Affected by global non-thread safe options.