The World Of Nick

Doodle Jump

How To Read RGB BMP's Bits And Display It In C# | Tuesday, September 28th, 2025

I Show You How To Turn Bytes Into Shrek (Static)!

Preface

Before we get started, I want to preface about this guide! First thing I want to preface is that I am NOT a teacher/professor, a CS major, or have decades of experience with programming! So in turn my code isn't going to be perfect by any means, however I understand my code (for the most part); I also don't use any sort of LLMs (as you can probably tell lol with my grammar) for programming or writing! Yeah, I want to be a primary care physician for cripe’s sake, I enjoy programming/writing as a hobby, but I really don't think I could do this for a living (plus I have way more enjoyment as a doctor)! Maybe a side-gig, but not a career!

The second thing I want to preface is you do you, woman/man/enby! Yes my code DOES use MonoGame, but you can use anything to display pixels! You can use for example SILK.NET or OpenTK to display pixels! MonoGame is rather large, and we don't really need full sprites or the Content Manager for this project, I just used MonoGame because I know it (heck I probably will make a SILK.NET version of this)!

What This Guide Is

This guide is a guide on how to read a .BMP file and display it, do note that this guide is for just a standerd old RGB bitmap file, not other colour formats (I may midly talk about it later in the guide, but idk)! This is to learn a little bit about how images work, about bits, and really for the hell of it. This is by no means great code or super useful (I mean MonoGame in my case has built in image support)! Do also note that I am using MonoGame, however if you wanna use Silk.NET or whatever just make your own Screen.cs, the parsing and everything will be the same!

Why?

Well, to learn and for fun. This is by no means a particularly great BMP viewer. Windows Forms, MonoGame, etc; all have much better handling of images built in! I already said this in the preface, but I am only using MonoGame to display pixels in a Screen class, you can make your own Screen class that uses for example SILK.NET instead! And most of the guide will be the same! This is a guide to help you understand BMPs, not how to make the most practical and useful BMP viewer or for your UI in high-end triple-a games. Oh, it is also for me to prepare for my MPEG-1 project I am going to work on, but that's a whole different story!

What Is An BMP?

A BMP, sort for Bitmap, is just a map of bits used for storing images. It is a very simple format, it doesn't have compression (iirc) and is SO easy to parse. If you want to read up a little more check out this Wikipedia article! It isn't complicated whatsoever!

Shrek

Getting Started

It is very easy to get started all we really need is to have MonoGame setup and create the project, so just follow here! After you went through the getting started section, we will want to run `dotnet new mgdesktopgl -n YourProjectName` template, and we'll have created our project! To run our template just run `dotnet run`, and you're golden. Open the project now with whatever IDE/Text Editor or whatever you want to use (I in my case am using VSCodiuim).

Let's Get Pixelacle!

We will start by looking atour `Game1.cs` class, this is assuming you're using MonoGame like me! This is the main area to our project! I just removed some comments that is pretty much it for now. If you didn't already try running it in Getting Started make sure the runs now with `dotnet run`!
                        using Microsoft.Xna.Framework;
                        using Microsoft.Xna.Framework.Graphics;
                        using Microsoft.Xna.Framework.Input;
                        
                        namespace MonoBMP;
                        
                        public class Game1 : Game
                        {
                            private GraphicsDeviceManager _graphics;
                            private SpriteBatch _spriteBatch;
                        
                            public Game1()
                            {
                                _graphics = new GraphicsDeviceManager(this);
                                Content.RootDirectory = "Content";
                                IsMouseVisible = true;
                            }
                        
                            protected override void Initialize()
                            {
                                _spriteBatch = new SpriteBatch(GraphicsDevice);
                        
                                base.Initialize();
                            }
                            
                            protected override void LoadContent()
                            {

                                base.LoadContent();
                            }
                        
                            protected override void Update(GameTime gameTime)
                            {
                        
                                base.Update(gameTime);
                            }
                        
                            protected override void Draw(GameTime gameTime)
                            {
                                GraphicsDevice.Clear(Color.CornflowerBlue);
                        
                                base.Draw(gameTime);
                            }
                        }
                    
                        using System;
                        using Microsoft.Xna.Framework;
                        using Microsoft.Xna.Framework.Content;
                        using Microsoft.Xna.Framework.Graphics;
                        
                        namespace MonoBMP;
                        
                        public class Screen
                        {
                            // Screen Properties
                            public int _imageWidth = 0;
                            public int _imageHeight = 0;
                            
                            // Misc
                            public Color[,] pixelStates;
                        
                            // Classes
                            public GraphicsDeviceManager _graphics;
                            public ContentManager _contentManager;
                        
                            // Pixel
                            public Texture2D pixel;
                            
                            public Screen(int imageWidth, int imageHeight, 
                              GraphicsDeviceManager graphics, ContentManager contentManager)
                            {
                        
                                // Set Classes
                                _graphics = graphics;
                                _contentManager = contentManager;
                        
                                // Set Display
                                _imageHeight = imageHeight;
                                _imageWidth = imageWidth;
                            
                                _graphics.PreferredBackBufferHeight = _imageHeight;
                                _graphics.PreferredBackBufferWidth = _imageWidth;
                                _graphics.ApplyChanges();
                        
                                // Set Pixels
                                pixelStates = new Color[_imageHeight, _imageWidth];
                                Clear(); // this will set all pixels to a value so we dont null!
                            }
                        
                            public void LoadContent()
                            {
                                pixel = new Texture2D(_graphics.GraphicsDevice, 1, 1);
                                pixel.SetData(new[] { Color.White });
                            }
                            
                            public void Clear()
                            {
                                for (int y = 0; y < _imageHeight; y++)
                                {
                                    for (int x = 0; x < _imageWidth; x++)
                                    {
                                        pixelStates[y, x] = new Color(255,255,255);
                                    }
                                }
                            }
                            
                            public void ReSizeWindowSize(int height, int width)
                            {
                                var previousWidth = _imageWidth;
                                var previousHeight = _imageHeight;
                        
                                _imageWidth = width;
                                _imageHeight = height;
                            
                                _graphics.PreferredBackBufferHeight = _imageHeight;
                                _graphics.PreferredBackBufferWidth = _imageWidth;
                                _graphics.ApplyChanges();
                        
                                var previousPixelStates = pixelStates;
                        
                                pixelStates = new Color[height, width];
                        
                                Clear();
                            }
                            
                            public void SetPixel(int y, int x, Color colour)
                            {
                                if (y < _imageHeight)
                                {
                                    if (x < _imageWidth)
                                    {
                                        pixelStates[y, x] = colour;
                                    }
                                }
                            }
                            
                            public void Draw(SpriteBatch spriteBatch)
                            {
                                for (int y = 0; y < _imageHeight; y++)
                                {
                                    for (int x = 0; x < _imageWidth; x++)
                                    {
                                        spriteBatch.Draw(pixel, new Rectangle(x, y, 1, 1), pixelStates[y,x]);
                                    }
                                }
                            }
                        }
                    
                      using Microsoft.Xna.Framework;
                      using Microsoft.Xna.Framework.Graphics;
                      using Microsoft.Xna.Framework.Input;
                      
                      namespace MonoBMP;
                      
                      public class Game1 : Game
                      {
                          // Classes
                          private GraphicsDeviceManager _graphics;
                          private SpriteBatch _spriteBatch;
                          private Screen _screen;
                      
                          // Window/Image/Screen Size & Such
                          private int screenWidth = 500;
                          private int screenHeight = 500;
                        
                          public Game1()
                          {
                              _graphics = new GraphicsDeviceManager(this);
                              Content.RootDirectory = "Content";
                              IsMouseVisible = true;
                          }
                      
                          protected override void Initialize()
                          {
                              _spriteBatch = new SpriteBatch(GraphicsDevice);
                              _screen = new Screen(screenWidth, screenHeight, _graphics, Content);
                      
                              _screen.SetPixel(10, 10, Color.Red);
                      
                              base.Initialize();
                          }
                      
                          protected override void LoadContent()
                          {
                              _screen.LoadContent();
                      
                              base.LoadContent();
                          }
                      
                          protected override void Update(GameTime gameTime)
                          {
                              base.Update(gameTime);
                          }
                      
                          protected override void Draw(GameTime gameTime)
                          {
                              _spriteBatch.Begin();
                      
                              _screen.Draw(_spriteBatch);
                      
                              _spriteBatch.End();
                      
                              base.Draw(gameTime);
                          }
                      }

                  
Red Sqaure On White Backgrounds

It Takes Some Bits To Shrek!

STUB
    
                    
STUB
   
                     

Displaying/Parsing The Bits/Bytes For Shrek!

STUB