using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Graphics
{
///
/// Summary description for Form1.
///
public class MainForm : System.Windows.Forms.Form
{
private String fileName;
int fileIndex = 0;
private System.Windows.Forms.OpenFileDialog openFileDialog;
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.TextBox textOutput;
private System.Windows.Forms.Button buttonOpen;
private System.Windows.Forms.Button buttonSave;
private System.Windows.Forms.Button buttonCopy;
private System.Windows.Forms.SaveFileDialog saveFileDialog;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.textOutput = new System.Windows.Forms.TextBox();
this.buttonOpen = new System.Windows.Forms.Button();
this.buttonSave = new System.Windows.Forms.Button();
this.buttonCopy = new System.Windows.Forms.Button();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// openFileDialog
//
this.openFileDialog.DefaultExt = "png";
this.openFileDialog.Filter = "Image files (*.bmp, *.png, *.jpg, *.gif)|*.BMP;*.PNG;*.JPG;*.GIF|All files (*.*)|" +
"*.*";
//
// pictureBox
//
this.pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox.Location = new System.Drawing.Point(16, 16);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(160, 160);
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
// textOutput
//
this.textOutput.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.textOutput.Location = new System.Drawing.Point(192, 16);
this.textOutput.Multiline = true;
this.textOutput.Name = "textOutput";
this.textOutput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textOutput.Size = new System.Drawing.Size(192, 160);
this.textOutput.TabIndex = 1;
this.textOutput.Text = "";
//
// buttonOpen
//
this.buttonOpen.Location = new System.Drawing.Point(96, 192);
this.buttonOpen.Name = "buttonOpen";
this.buttonOpen.Size = new System.Drawing.Size(80, 24);
this.buttonOpen.TabIndex = 2;
this.buttonOpen.Text = "&Open File";
this.buttonOpen.Click += new System.EventHandler(this.buttonOpen_Click);
//
// buttonSave
//
this.buttonSave.Location = new System.Drawing.Point(224, 192);
this.buttonSave.Name = "buttonSave";
this.buttonSave.Size = new System.Drawing.Size(80, 24);
this.buttonSave.TabIndex = 3;
this.buttonSave.Text = "&Save Text";
this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
//
// buttonCopy
//
this.buttonCopy.Location = new System.Drawing.Point(320, 192);
this.buttonCopy.Name = "buttonCopy";
this.buttonCopy.Size = new System.Drawing.Size(64, 24);
this.buttonCopy.TabIndex = 4;
this.buttonCopy.Text = "&Copy";
this.buttonCopy.Click += new System.EventHandler(this.buttonCopy_Click);
//
// saveFileDialog
//
this.saveFileDialog.DefaultExt = "txt";
this.saveFileDialog.FileName = "doc1";
this.saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(400, 232);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonCopy,
this.buttonSave,
this.buttonOpen,
this.textOutput,
this.pictureBox});
this.Name = "MainForm";
this.Text = "Graphics Converter";
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
private void openImageFile()
{
if(openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = openFileDialog.FileName;
pictureBox.Image = Image.FromFile(fileName);
if(pictureBox.Image != null)
{
convertImageFile();
textOutput.SelectAll();
textOutput.Copy();
}
}
else
fileName = "";
}
private void convertImageFile()
{
Bitmap pictureBitmap;
Rectangle rect;
Color pixel;
int pixels = 0;
pictureBitmap = new Bitmap(pictureBox.Image);
rect = new Rectangle(0, 0, pictureBitmap.Width, pictureBitmap.Height);
//pictureBitmapData = pictureBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
int pixelMax = rect.Width * rect.Height;
System.IO.StringWriter stringBuffer = new System.IO.StringWriter(new System.Text.StringBuilder(pixelMax << 2 + 40));
stringBuffer.Write("flash unsigned char GRAPHIC_" + fileIndex++ + "[" + (rect.Width * rect.Height / 2) + "] = {\r\n");
int blockWidth = rect.Width >> 3;
int blockHeight = rect.Height >> 3;
int blockRow, blockCol, binX, binY;
int offsetX, offsetY;
// convert to text
for(blockRow = 0; blockRow < blockHeight; blockRow++)
{
for(blockCol = 0; blockCol < blockWidth; blockCol++)
{
// write block comment
stringBuffer.Write(" // [" + blockCol + "," + blockRow + "] \r\n");
// block offset
offsetX = blockCol * 8;
offsetY = blockRow * 8;
for(binY = 0; binY < 8; binY++)
{
for(binX = 0; binX < 8; binX++)
{
// get pixel information
pixel = pictureBitmap.GetPixel(binX + offsetX, binY + offsetY);
pixels++;
// write indent
if(binX % 8 == 0)
stringBuffer.Write(" ");
// write high nibble
if(binX % 2 == 0)
{
stringBuffer.Write("0x" + decToHex(pixel.R >> 4, 1));
}
// write low nibble
else
{
// check for last pixel
if(pixels < pixelMax)
stringBuffer.Write(decToHex(pixel.R >> 4, 1) + ", ");
else
stringBuffer.Write(decToHex(pixel.R >> 4, 1) + "\r\n};\r\n");
}
// write new bin line
if(binX % 8 == 7)
{
stringBuffer.Write("\r\n");
}
}
}
}
}
// output string
textOutput.Text = stringBuffer.ToString();
saveFileDialog.FileName = (openFileDialog.FileName).Substring(0, openFileDialog.FileName.Length - 4) + ".txt";
if(saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.StreamWriter fileStream = new System.IO.StreamWriter(saveFileDialog.FileName);
fileStream.Write(textOutput.Text);
fileStream.Close();
}
}
private string decToHex(int number, int i)
{
int nibble;
char[] buffer;
buffer = new char[i];
// iterate over each nibble of number, write right-to-left
for(i--; i >= 0; i--)
{
// mask lowest-order nibble
nibble = number & 0xf;
// combine nibble and ASCII offset to get hex-character
buffer[i] = (char)(nibble + (int)(nibble >= 10 ? ((int)'A' - 10) : (char)'0'));
// drop lowest-order nibble from number
number = number >> 4;
}
// return hex string
return (new String(buffer));
}
private void buttonCopy_Click(object sender, System.EventArgs e)
{
textOutput.SelectAll();
textOutput.Copy();
}
private void buttonOpen_Click(object sender, System.EventArgs e)
{
openImageFile();
}
private void buttonSave_Click(object sender, System.EventArgs e)
{
}
}
}