在Microsoft Small Basic中如何将CSV像素数据转为BMP等栅格图像
Absolutely! You can definitely convert that hex pixel array into a BMP (or other raster format) using Microsoft Small Basic—here are two straightforward approaches tailored to the language's capabilities:
This is the simplest method, perfect for prototyping or when you don't need to dig into low-level file structures. Small Basic's GraphicsWindow lets you draw each pixel directly, then save the entire canvas as an image file.
Step-by-Step:
- Define your image dimensions: You'll need to know the width and height of your image (calculate this from your array length—
width * height = total pixels). - Set up the drawing window: Match the window size to your image dimensions.
- Loop through your pixel array: Convert each hex value (like
FFFFFF) to a Small Basic-compatible color (prefix with#), then draw a 1x1 rectangle for each pixel. - Save the result: Use
GraphicsWindow.SaveImage()to export as BMP, PNG, or JPG.
Example Code:
' Replace these with your actual values width = 100 height = 100 ' Assume pixelArray is your existing array of hex color strings (e.g., "FFFFFF", "000000") ' Configure the drawing window GraphicsWindow.Width = width GraphicsWindow.Height = height GraphicsWindow.BackgroundColor = "White" ' Draw each pixel for y = 1 to height for x = 1 to width ' Calculate array index (Small Basic uses 1-based indexing) pixelIndex = (y - 1) * width + x hexColor = pixelArray[pixelIndex] ' Convert hex to Small Basic color format GraphicsWindow.PenColor = "#" + hexColor ' Draw a 1x1 rectangle for the pixel GraphicsWindow.DrawRectangle(x - 1, y - 1, 1, 1) endfor endfor ' Save the image to your desired path GraphicsWindow.SaveImage("C:\Your\Output\Path\result.bmp")
If you want to understand how image formats work under the hood, you can construct a BMP file directly by writing its binary structure. BMP is a straightforward format with a fixed header, so this is totally doable in Small Basic.
Key Notes for BMP:
- BMP stores pixels in BGR order (not RGB), so you'll need to swap the red and blue components of your hex values.
- Rows of pixels must be padded to a multiple of 4 bytes—add zero bytes if needed.
- The image is stored from bottom to top (so we'll loop from the last row to the first).
Example Code:
width = 100 height = 100 ' Initialize a test array (replace with your actual pixel data) pixelArray = Array.Create(width * height) for i = 1 to width * height pixelArray[i] = "FFFFFF" ' White pixels for testing endfor ' Calculate BMP structure values bytesPerPixel = 3 ' 24-bit color (R, G, B) rowSize = width * bytesPerPixel padding = (4 - (rowSize mod 4)) mod 4 ' Padding to align rows to 4 bytes totalPixelBytes = (rowSize + padding) * height fileSize = 14 + 40 + totalPixelBytes ' Total BMP file size (headers + pixels) ' Create and write to the BMP file file = File.OpenWrite("C:\Your\Output\Path\manual.bmp") ' Write BMP File Header (14 bytes) File.WriteByte(file, 66) ' 'B' File.WriteByte(file, 77) ' 'M' File.WriteInt32(file, fileSize) File.WriteInt16(file, 0) ' Reserved File.WriteInt16(file, 0) ' Reserved File.WriteInt32(file, 14 + 40) ' Offset to pixel data ' Write BMP Info Header (40 bytes) File.WriteInt32(file, 40) ' Header size File.WriteInt32(file, width) File.WriteInt32(file, height) File.WriteInt16(file, 1) ' Number of color planes File.WriteInt16(file, 24) ' Bits per pixel (24-bit) File.WriteInt32(file, 0) ' No compression File.WriteInt32(file, totalPixelBytes) File.WriteInt32(file, 0) ' Horizontal resolution (default) File.WriteInt32(file, 0) ' Vertical resolution (default) File.WriteInt32(file, 0) ' Number of colors in palette File.WriteInt32(file, 0) ' Number of important colors ' Write pixel data (bottom to top, BGR order) for y = height downto 1 for x = 1 to width pixelIndex = (y - 1) * width + x hexColor = pixelArray[pixelIndex] ' Convert hex to RGB bytes, then write as BGR red = Convert.ToInt32(Text.GetSubText(hexColor, 1, 2), 16) green = Convert.ToInt32(Text.GetSubText(hexColor, 3, 2), 16) blue = Convert.ToInt32(Text.GetSubText(hexColor, 5, 2), 16) File.WriteByte(file, blue) File.WriteByte(file, green) File.WriteByte(file, red) endfor ' Add padding bytes for row alignment for p = 1 to padding File.WriteByte(file, 0) endfor endfor ' Close the file to save changes File.Close(file)
Quick Tips:
- Double-check your array indexing—Small Basic uses 1-based arrays, so make sure your pixel positions align correctly.
- For the manual BMP method, if your image uses 8-bit (indexed) color instead of 24-bit, you'll need to add a color palette to the header.
- Test with a small array first (like 10x10) to debug any alignment or color issues.
内容的提问来源于stack exchange,提问作者Oliver Bryant

