StbImageNext is the safe fork of StbImageSharp
In other words, it's a cross-platform .NET library for decoding images.
| Format | Decode | Notes |
|---|---|---|
| PNG | Yes | |
| JPEG / JPG | Yes | |
| BMP | Yes | |
| GIF | Yes | Including animated frames |
| TGA | Yes | |
| PSD | Yes | |
| HDR | Yes | Radiance format, floating-point |
https://www.nuget.org/packages/StbImageNext/
Add the namespace at the top of your file:
using StbImageNext;
using System.IO;using var stream = File.OpenRead("photo.jpg");
using var image = ImageResult.FromStream(stream);
// image.Width -> pixel width
// image.Height -> pixel height
// image.Data -> decoded pixel data (byte array)
// image.Comp -> color components of the decoded data
// image.SourceComp -> color components of the source filebyte[] imageData = File.ReadAllBytes("photo.png");
using var image = ImageResult.FromMemory(imageData);You can request that the decoded image be converted to a specific component layout:
using var stream = File.OpenRead("photo.jpg");
using var image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);Available ColorComponents values:
Grey(1)GreyAlpha(2)RedGreenBlue(3)RedGreenBlueAlpha(4)
If requiredComponents is omitted or null, the image is decoded in its native format.
If you only need dimensions and format (e.g. to validate an upload), use ImageInfo:
using var stream = File.OpenRead("photo.jpg");
var info = ImageInfo.FromStream(stream);
Console.WriteLine($"{info.Width}x{info.Height}, {info.ColorComponents}");Flip the decoded image vertically (top-to-bottom) in place:
image.VerticalFlip();Convert a standard 8-bit-per-channel image to floating-point HDR:
using var stream = File.OpenRead("photo.png");
using var ldr = ImageResult.FromStream(stream);
var hdr = ldr.ToHdr(gamma: 2.2f, scale: 1.0f);
// hdr.Data contains float pixel valuesConvert a high-dynamic-range floating-point image back to 8-bit:
using var stream = File.OpenRead("image.hdr");
using var hdr = ImageResultFloat.FromStream(stream);
var ldr = hdr.ToLdr(gamma_i: 1.0f / 2.2f, scale_i: 1.0f);
// ldr.Data contains 8-bit pixel valuesEnumerate the frames of an animated GIF:
using var stream = File.OpenRead("animation.gif");
foreach (var frame in ImageResult.AnimatedGifFramesFromStream(stream))
{
Console.WriteLine($"Frame: {frame.Width}x{frame.Height}, delay: {frame.DelayInMs}ms");
// frame.Data contains the decoded pixels for this frame
}You can also request a specific component format for animated frames:
foreach (var frame in ImageResult.AnimatedGifFramesFromStream(stream, ColorComponents.RedGreenBlue))
{
// ...
}Decode an HDR (Radiance .hdr) image directly as floating-point data:
using var stream = File.OpenRead("scene.hdr");
using var hdr = ImageResultFloat.FromStream(stream);You can load an image with StbImageNext and upload it into a MonoGame Texture2D for use in a game.
When you request ColorComponents.RedGreenBlueAlpha, the decoded pixel data is guaranteed to be 4 bytes per pixel (RGBA), matching MonoGame's Color struct exactly. You can upload the byte array directly without any conversion:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StbImageNext;
using System.IO;
// Load the image as RGBA (4 bytes per pixel)
using var stream = File.OpenRead("photo.png");
using var image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
// Create the MonoGame texture
var texture = new Texture2D(GraphicsDevice, image.Width, image.Height);
// Upload directly - image.Data is byte[] with RGBA layout matching Color
texture.SetData(image.Data);
// Use the texture in your game
// _spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, Color.White);Public Domain or MIT
- Clone this repo.
- Open StbImageNext.sln.
If this project is useful for you, you can support the development:
- Boosty: https://boosty.to/rds1983
- Telegram Wallet: https://t.me/rds1983
| Blockchain | Supported Tokens | Wallet Address |
|---|---|---|
| TON Network | GRAM (native), USDT (TON) | UQCQy6tFInPvqinE44zHY4R0rYS3niaBikkqiSyGmyoAMwyO |
| BSC | BNB (native), USDT (BEP20) | 0x4205d5B12e4edc2D30E97A398A627c83872084E9 |
| Solana | SOL (native), USDT (SPL) | GjHtV9mP4eJNmi8p5bwsHtb5WV2zFu3v5VhiZ1bvddGV |
| TRON | TRX (native), USDT (TRC-20) | TM2SbB9eRQ99MP6d85a3yJSCWhqeV46crh |
| Bitcoin | BTC (native) | bc1qygcxzyhz0m34m9anj27rdydjekkv5ymxa9f927 |