Convert BMP to Hex C# code
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace _128x64oled_icon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string filePath, SavedFile;
string SavedFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Bitmap myBitmap;
private void bt_convert_Click(object sender, EventArgs e)
{
if (!File.Exists(filePath))
{
MessageBox.Show("No existing file selected");
return;
}
int userWidth = int.Parse(textWidth.Text);
int userHeight = int.Parse(textHeight.Text);
SavedFolder = Path.GetDirectoryName(filePath); // save folder for later
SavedFile = Path.GetFileName(filePath); // save file
for (int y = 0; y < userHeight; y++) // y < 64
{
for (int x = 0; x < userWidth; x++) // x <128
{
Color pixelColor = myBitmap.GetPixel(x, y);
if (pixelColor.R < 170) // || (pixelColor.B < 93) || (pixelColor.G < 93)) // was 170
{
myBitmap.SetPixel(x, y, Color.Black);
}
else
{
myBitmap.SetPixel(x, y, Color.White);
}
}
}
// MessageBox.Show(pixelColor.R.ToString() + " " + pixelColor.G.ToString() + " " + pixelColor.B.ToString());
myBitmap.Save(filePath + "_black.bmp");
// new code
int byteSize = userWidth * (userHeight / 8);
byte[] normal_hex = new byte[byteSize];
int k = -1;
Array.Clear(normal_hex, 0, normal_hex.Length);
// start code for Arduino Adafruit_GFX bytes from left to right
if (Ada_rb.Checked == true)
{
int mask = 0;
Array.Clear(normal_hex, 0, normal_hex.Length);
for (int y = 0; y < userHeight; y++)
{
for (int x = 0; x < userWidth; x++)
{
if ((x % 8) == 0)
{
k++;
mask = 8;
}
mask--;
Color pixelColor = myBitmap.GetPixel(x, y); // x= 0 -> 64 = 8 bytes y = 0-> 64 = 8 bytes
if (pixelColor.R == 0)
{
normal_hex[k] += (byte)(1 << mask);
}
}
}
} // end code Adafruit
else
{
// code for ATtiny85 SSD1306_minimal bytes from top to bottom
for (int y = 0; y < userHeight; y += 8)
{
for (int x = 0; x < userWidth; x++)
{
k++;
for (int i = 0; i < 8; i++)
{
Color pixelColor = myBitmap.GetPixel(x, i + y); // x= 0 -> 64 = 8 bytes y = 0-> 64 = 8 bytes
if (pixelColor.R == 0)
{
normal_hex[k] += (byte)(1 << i);
}
}
}
}
} // end code mini
FileStream fl = File.Create(SavedFolder + "\\" + SavedFile + ".hex");
string hex = BitConverter.ToString(normal_hex).Replace("-", ",0x");
string Plain = Path.GetFileNameWithoutExtension(SavedFile);
StreamWriter s = new StreamWriter(fl);
s.Write("// ");
s.Write(userWidth);
s.Write(" x ");
s.WriteLine(userHeight);
s.Write("const unsigned char ");
s.Write(Plain);
s.WriteLine(" [] PROGMEM = { ");
s.Write("0x");
s.Write(hex);
s.WriteLine("};");
s.Close();
tb_message.Text = SavedFolder + "\\" + SavedFile + ".hex created!";
// MessageBox.Show(filePath +".bin created!");
// Application.Exit();
}
private void mini_rb_CheckedChanged(object sender, EventArgs e)
{
tb_message.Text = "mini = " + mini_rb.Checked + " Ada = " + Ada_rb.Checked;
}
private void bt_browse_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
openFileDialog1.InitialDirectory = SavedFolder;
openFileDialog1.Filter = "bmp (*.bmp)|*.bmp|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false;
openFileDialog1.Title = "Please, Select Source File";
openFileDialog1.FileName= "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog1.FileName;
myBitmap = new Bitmap(filePath); // Get the color of a pixel within myBitmap.
textWidth.Text = myBitmap.Width.ToString();
textHeight.Text = myBitmap.Height.ToString();
}
}
} // private
}
}