### Get Image Information using StbImageSharp Source: https://github.com/stbsharp/stbimagesharp/blob/master/README.md Retrieves image information such as width, height, and color components from a stream without loading the entire image data. Returns null if the image type is not supported. ```csharp ImageInfo? info = ImageInfo.FromStream(imageStream); ``` -------------------------------- ### Get Image Info from Stream with StbImageSharp Source: https://context7.com/stbsharp/stbimagesharp/llms.txt Retrieves metadata about an image, such as its dimensions, color components, and bit depth, directly from a .NET Stream without loading the entire pixel data. This is efficient for quickly inspecting images or making decisions based on their properties. Returns null if the image format is not recognized. ```csharp using System; using System.IO; using StbImageSharp; // Get image info without loading full pixel data using (var stream = File.OpenRead("large_image.png")) { ImageInfo? info = ImageInfo.FromStream(stream); if (info.HasValue) { ImageInfo imageInfo = info.Value; Console.WriteLine($"Dimensions: {imageInfo.Width}x{imageInfo.Height}"); Console.WriteLine($"Color Components: {imageInfo.ColorComponents}"); Console.WriteLine($"Bits Per Channel: {imageInfo.BitsPerChannel}"); // 8 or 16 // Decide whether to load based on size if (imageInfo.Width > 4096 || imageInfo.Height > 4096) { Console.WriteLine("Warning: Large image detected"); } } else { Console.WriteLine("Unsupported image format"); } } // Check multiple files quickly string[] files = Directory.GetFiles("textures", "*.png"); foreach (string file in files) { using (var stream = File.OpenRead(file)) { ImageInfo? info = ImageInfo.FromStream(stream); if (info.HasValue) { Console.WriteLine($"{Path.GetFileName(file)}: {info.Value.Width}x{info.Value.Height}"); } } } ``` -------------------------------- ### Load Animated GIF Frames using StbImageSharp Source: https://github.com/stbsharp/stbimagesharp/blob/master/README.md Loads all frames of an animated GIF along with their delays from a stream. It returns an enumerable collection of AnimatedFrameResult objects, allowing processing of each frame individually. ```csharp foreach(AnimatedFrameResult frame in ImageResult.AnimatedGifFramesFromStream(stream)) { // Do something with a frame } ``` -------------------------------- ### Load Image from Stream with StbImageSharp Source: https://context7.com/stbsharp/stbimagesharp/llms.txt Loads an image from any .NET Stream using StbImageSharp. It supports automatic format detection for common image types and allows specifying the desired color components for the output pixel data. Returns decoded pixel data as a byte array. ```csharp using System; using System.IO; using StbImageSharp; // Load image from file with RGBA color components using (var stream = File.OpenRead("texture.png")) { ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); Console.WriteLine($"Width: {image.Width}"); // e.g., 512 Console.WriteLine($"Height: {image.Height}"); // e.g., 512 Console.WriteLine($"Source Format: {image.SourceComp}"); // Original format (e.g., RedGreenBlue) Console.WriteLine($"Output Format: {image.Comp}"); // Requested format (RedGreenBlueAlpha) Console.WriteLine($"Data Length: {image.Data.Length}"); // Width * Height * 4 bytes // Access raw pixel data (RGBA format, 1 byte per channel) byte r = image.Data[0]; // Red channel of first pixel byte g = image.Data[1]; // Green channel byte b = image.Data[2]; // Blue channel byte a = image.Data[3]; // Alpha channel } // Load with original color components (no conversion) using (var stream = File.OpenRead("photo.jpg")) { ImageResult image = ImageResult.FromStream(stream, ColorComponents.Default); // image.Comp will match source format (likely RedGreenBlue for JPG) } ``` -------------------------------- ### Load Image from Stream using StbImageSharp Source: https://github.com/stbsharp/stbimagesharp/blob/master/README.md Loads an image from a stream using StbImageSharp. It supports various image formats and requires the ColorComponents enum to specify the desired color format. Throws an exception on failure. ```csharp using(var stream = File.OpenRead(path)) { ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); } ``` -------------------------------- ### Load HDR Images as Float Arrays with ImageResultFloat.FromStream Source: https://context7.com/stbsharp/stbimagesharp/llms.txt Loads HDR images from a stream, returning pixel data as float arrays. This is useful for physically-based rendering and advanced image processing. It requires System.IO and StbImageSharp. The input is a stream and desired color components, outputting an ImageResultFloat object with width, height, and float data. ```csharp using System; using System.IO; using StbImageSharp; // Load HDR image for use as environment map using (var stream = File.OpenRead("skybox.hdr")) { ImageResultFloat hdrImage = ImageResultFloat.FromStream(stream, ColorComponents.RedGreenBlue); Console.WriteLine($"HDR Image: {hdrImage.Width}x{hdrImage.Height}"); Console.WriteLine($"Float data length: {hdrImage.Data.Length}"); // Width * Height * 3 floats // Access HDR pixel data (values can exceed 1.0 for bright areas) float r = hdrImage.Data[0]; // Red channel (float) float g = hdrImage.Data[1]; // Green channel (float) float b = hdrImage.Data[2]; // Blue channel (float) Console.WriteLine($"First pixel RGB: ({r:F3}, {g:F3}, {b:F3})"); // Find maximum brightness in the image float maxBrightness = 0f; for (int i = 0; i < hdrImage.Data.Length; i += 3) { float luminance = 0.2126f * hdrImage.Data[i] + 0.7152f * hdrImage.Data[i + 1] + 0.0722f * hdrImage.Data[i + 2]; maxBrightness = Math.Max(maxBrightness, luminance); } Console.WriteLine($"Max luminance: {maxBrightness:F2}"); } // Load HDR from memory byte[] hdrData = File.ReadAllBytes("environment.hdr"); ImageResultFloat envMap = ImageResultFloat.FromMemory(hdrData, ColorComponents.RedGreenBlueAlpha); ``` -------------------------------- ### Load Image from Memory using StbImageSharp Source: https://github.com/stbsharp/stbimagesharp/blob/master/README.md Loads an image from a byte array using StbImageSharp. This method is suitable when the image data is already in memory. It supports various image formats and requires the ColorComponents enum. Throws an exception on failure. ```csharp byte[] buffer = File.ReadAllBytes(path); ImageResult image = ImageResult.FromMemory(buffer, ColorComponents.RedGreenBlueAlpha); ``` -------------------------------- ### Load Image from Memory with StbImageSharp Source: https://context7.com/stbsharp/stbimagesharp/llms.txt Loads an image directly from a byte array in memory using StbImageSharp. This is useful for image data obtained from network responses, embedded resources, or other sources that don't involve direct file access. It also allows specifying the desired output color components. ```csharp using System; using System.IO; using System.Net.Http; using StbImageSharp; // Load image from byte array byte[] imageBytes = File.ReadAllBytes("sprite.png"); ImageResult image = ImageResult.FromMemory(imageBytes, ColorComponents.RedGreenBlueAlpha); Console.WriteLine($"Loaded {image.Width}x{image.Height} image"); Console.WriteLine($"Pixel data: {image.Data.Length} bytes"); // Load from HTTP response using (var httpClient = new HttpClient()) { byte[] downloadedData = httpClient.GetByteArrayAsync("https://example.com/image.png").Result; ImageResult downloadedImage = ImageResult.FromMemory(downloadedData, ColorComponents.RedGreenBlue); // Process the downloaded image Console.WriteLine($"Downloaded: {downloadedImage.Width}x{downloadedImage.Height}"); } ``` -------------------------------- ### Process Animated GIF Frames with AnimatedGifFramesFromStream Source: https://context7.com/stbsharp/stbimagesharp/llms.txt Iterates through all frames of an animated GIF, providing access to individual frame data and delay timings. It uses lazy enumeration to minimize memory usage. This function requires System.IO, System.Collections.Generic, and StbImageSharp. The input is a stream, and it outputs an IEnumerable of AnimatedFrameResult objects. ```csharp using System; using System.IO; using System.Collections.Generic; using StbImageSharp; // Load and process animated GIF frames using (var stream = File.OpenRead("animation.gif")) { int frameIndex = 0; int totalDuration = 0; foreach (AnimatedFrameResult frame in ImageResult.AnimatedGifFramesFromStream(stream)) { Console.WriteLine($"Frame {frameIndex}:"); Console.WriteLine($" Size: {frame.Width}x{frame.Height}"); Console.WriteLine($" Delay: {frame.DelayInMs}ms"); Console.WriteLine($" Data: {frame.Data.Length} bytes"); totalDuration += frame.DelayInMs; frameIndex++; // Process frame data (e.g., save individual frames) // frame.Data contains RGBA pixel data } Console.WriteLine($"Total frames: {frameIndex}"); Console.WriteLine($"Animation duration: {totalDuration}ms"); } // Extract frames with specific color format using (var stream = File.OpenRead("sprite_sheet.gif")) { var frames = new List(); foreach (var frame in ImageResult.AnimatedGifFramesFromStream(stream, ColorComponents.RedGreenBlueAlpha)) { // Clone frame data since the enumerator reuses the buffer byte[] frameData = new byte[frame.Data.Length]; Array.Copy(frame.Data, frameData, frame.Data.Length); frames.Add(frameData); } Console.WriteLine($"Extracted {frames.Count} frames to memory"); } ``` -------------------------------- ### Convert StbImageSharp Data to WinForms Bitmap Source: https://github.com/stbsharp/stbimagesharp/blob/master/README.md Converts the image data loaded by StbImageSharp into a Windows Forms Bitmap object. This involves reordering color components (RGBA to BGRA) and using Marshal.Copy for efficient data transfer. It requires System.Drawing namespaces. ```csharp byte[] data = image.Data; // Convert rgba to bgra for (int i = 0; i < x*y; ++i) { byte r = data[i*4]; byte g = data[i*4 + 1]; byte b = data[i*4 + 2]; byte a = data[i*4 + 3]; data[i*4] = b; data[i*4 + 1] = g; data[i*4 + 2] = r; data[i*4 + 3] = a; } // Create Bitmap Bitmap bmp = new Bitmap(_loadedImage.Width, _loadedImage.Height, PixelFormat.Format32bppArgb); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, _loadedImage.Width, _loadedImage.Height), ImageLockMode.WriteOnly, bmp.PixelFormat); Marshal.Copy(data, 0, bmpData.Scan0, bmpData.Stride*bmp.Height); bmp.UnlockBits(bmpData); ``` -------------------------------- ### Specify Image Color Format with ColorComponents Enum Source: https://context7.com/stbsharp/stbimagesharp/llms.txt Specifies the desired output color format when loading images, controlling the number of channels in the output data. This enum is used with image loading functions. Available options include Default, Grey, GreyAlpha, RedGreenBlue, and RedGreenBlueAlpha. ```csharp using System; using System.IO; using StbImageSharp; // Available color component options // ColorComponents.Default - Keep original format // ColorComponents.Grey - 1 channel (grayscale) // ColorComponents.GreyAlpha - 2 channels (grayscale + alpha) // ColorComponents.RedGreenBlue - 3 channels (RGB) // ColorComponents.RedGreenBlueAlpha - 4 channels (RGBA) using (var stream = File.OpenRead("photo.jpg")) { // Load as grayscale (1 byte per pixel) ImageResult grey = ImageResult.FromStream(stream, ColorComponents.Grey); Console.WriteLine($"Grayscale data: {grey.Data.Length} bytes"); // Width * Height stream.Seek(0, SeekOrigin.Begin); // Load as RGB (3 bytes per pixel) ImageResult rgb = ImageResult.FromStream(stream, ColorComponents.RedGreenBlue); Console.WriteLine($"RGB data: {rgb.Data.Length} bytes"); // Width * Height * 3 stream.Seek(0, SeekOrigin.Begin); // Load as RGBA (4 bytes per pixel) - best for most rendering APIs ImageResult rgba = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); Console.WriteLine($"RGBA data: {rgba.Data.Length} bytes"); // Width * Height * 4 } ``` -------------------------------- ### Convert StbImageSharp Data to MonoGame Texture2D Source: https://github.com/stbsharp/stbimagesharp/blob/master/README.md Converts the image data loaded by StbImageSharp into a MonoGame Texture2D object. This is useful for rendering images within a MonoGame application. It requires a GraphicsDevice instance. ```csharp Texture2D texture = new Texture2D(GraphicsDevice, image.Width, image.Height, false, SurfaceFormat.Color); texture.SetData(image.Data); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.