/****将jpeg文件转换成.h文件****/
/*****************************************************************************************
平台:VS2008 C#窗体应用程序
编者:张永辉 2013年6月6日
*****************************************************************************************/
/*****************************************************************************************
文件: Form1.cs
*****************************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;
using System.Net.Sockets;
namespace BinFileToh
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> logFullName = new List<string>(); //选择的文件夹下面符合规则的文件全名列表
private void but_chfile_Click(object sender, EventArgs e)
{
//文件路径
FolderBrowserDialog fbd = new FolderBrowserDialog();
label_filepath.Text = "no file path";
if (DialogResult.OK == fbd.ShowDialog())
{
label_filepath.Text = fbd.SelectedPath;
}
else
{
return;
}
//获取文件列表
logFullName.Clear();
int len = 0;
string dirp = label_filepath.Text;
DirectoryInfo mydir = new DirectoryInfo(dirp);
//遍历文件列表
foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
{
if (fsi is FileInfo)
{
FileInfo fi = (FileInfo)fsi;
//后缀名过滤
if (fi.FullName.EndsWith(".jpg"))
{
logFullName.Add(fi.FullName); //
len++;
}
}
}
//将文件列表显示到界面
checkedListBox_file.Items.Clear();
for (int i = 0; i < logFullName.Count(); i++)
{
string fn = logFullName.ElementAt(i);
fn = Path.GetFileName(fn);
checkedListBox_file.Items.Add(fn); //显示文件名,不显示路径,否则太长了
}
}
//【按钮】全选按钮
private void checkBox_AnChkAll_CheckedChanged(object sender, EventArgs e)
{
//全选按钮
for (int i = 0; i < checkedListBox_file.Items.Count; i++)
{
if (checkBox_AnChkAll.Checked)
{
checkedListBox_file.SetItemChecked(i, true);
}
else
{
checkedListBox_file.SetItemChecked(i, false);
}
}
}
//【按钮】开始转换
private void but_to_h_Click(object sender, EventArgs e)
{
//检测是否有文件列表
if (checkedListBox_file.Items.Count <= 0)
{
MessageBox.Show("请选择文件,亲!");
return;
}
//写文件头
string filehead = "/************************************************************\r\n";
filehead += " jpeg file to .h file \r\n";
filehead += "name: zhangyonghui 2013 06 06 \r\n";
filehead += "*************************************************/\r\n";
File.WriteAllText(label_filepath.Text + "\\jpgfile.h", filehead, Encoding.Default);
//开始转换文件
for (int i = 0; i < checkedListBox_file.Items.Count; i++)
{
//查找选中项
if (checkedListBox_file.GetItemChecked(i) == true)
{
//获取选中元素的值 = 文件名
string fname;
fname = (string)checkedListBox_file.Items[i];
//转换文件,并追加到文件尾
FileToh(label_filepath.Text, fname);
}
}
}
//转换文件,输入文件路径,文件名
byte[] fdata;
private void FileToh(string fpath,string fname)
{
string fn = fpath + "\\" + fname;
FileStream fs = File.OpenRead(fn);
fdata = new byte[fs.Length];
fs.Read(fdata, 0, fdata.Length); //将数据读取到fdata
int len = fname.Length;
//删除后缀名 .jpg
fname = fname.Remove((int)(len - 4));
//数组示例
//const unsigned char name[] = {\
//0x11,0x11
//0x12,0x12};
//数组头
string onefile = "\r\nconst unsigned char " + fname + @"[] = {";
string onebyte;
len = (int)fs.Length;
for(int i = 0; i < len;i++)
{
//每32个回车换行
if (i % 32 == 0)
{
onefile += "\r\n";
}
//转换
//onebyte = "0x" + Convert.ToString(fdata[i],16) + ",";
onebyte = ByteToHex(fdata[i]) + ",";
//加个空格对齐
if (onebyte.Length == 4)
{
onebyte += " ";
}
//加入到文本
onefile += onebyte;
}
//删除数组尾的 ","
onefile = onefile.Remove((int)(onefile.Length - 1));
onefile += "};\r\n";
//追加文件
File.AppendAllText(fpath+"\\jpgfile.h",onefile,Encoding.Default);
}
//将byte转换成16进制的数组
private string ByteToHex(byte b)
{
string s="";
byte qb = (byte)(b >> 4);
byte hb = (byte)(b & 0x0f);
s = "0x";
s += ByteToHexb(qb);
s += ByteToHexb(hb);
return s;
}
string ByteToHexb(byte b)
{
int bb = (int)b;
switch (bb)
{
case 0: return "0";
case 1: return "1";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "A";
case 11: return "B";
case 12: return "C";
case 13: return "D";
case 14: return "E";
case 15: return "F";
default: return "";
}
}
//测试
private void label_filepath_Click(object sender, EventArgs e)
{
string a = ByteToHex(55);
}
}
}
/*****************************************************************************************
文件:Form1.Designer.cs
*****************************************************************************************/
namespace BinFileToh
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.but_chfile = new System.Windows.Forms.Button();
this.checkedListBox_file = new System.Windows.Forms.CheckedListBox();
this.label_filepath = new System.Windows.Forms.Label();
this.but_to_h = new System.Windows.Forms.Button();
this.checkBox_AnChkAll = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// but_chfile
//
this.but_chfile.Location = new System.Drawing.Point(35, 32);
this.but_chfile.Name = "but_chfile";
this.but_chfile.Size = new System.Drawing.Size(125, 23);
this.but_chfile.TabIndex = 0;
this.but_chfile.Text = "选择文件夹";
this.but_chfile.UseVisualStyleBackColor = true;
this.but_chfile.Click += new System.EventHandler(this.but_chfile_Click);
//
// checkedListBox_file
//
this.checkedListBox_file.FormattingEnabled = true;
this.checkedListBox_file.Location = new System.Drawing.Point(35, 102);
this.checkedListBox_file.Name = "checkedListBox_file";
this.checkedListBox_file.Size = new System.Drawing.Size(377, 164);
this.checkedListBox_file.TabIndex = 1;
//
// label_filepath
//
this.label_filepath.AutoSize = true;
this.label_filepath.Location = new System.Drawing.Point(91, 74);
this.label_filepath.Name = "label_filepath";
this.label_filepath.Size = new System.Drawing.Size(29, 12);
this.label_filepath.TabIndex = 2;
this.label_filepath.Text = "path";
this.label_filepath.Click += new System.EventHandler(this.label_filepath_Click);
//
// but_to_h
//
this.but_to_h.Location = new System.Drawing.Point(324, 32);
this.but_to_h.Name = "but_to_h";
this.but_to_h.Size = new System.Drawing.Size(88, 23);
this.but_to_h.TabIndex = 3;
this.but_to_h.Text = "生成 .h文件";
this.but_to_h.UseVisualStyleBackColor = true;
this.but_to_h.Click += new System.EventHandler(this.but_to_h_Click);
//
// checkBox_AnChkAll
//
this.checkBox_AnChkAll.AutoSize = true;
this.checkBox_AnChkAll.Location = new System.Drawing.Point(37, 74);
this.checkBox_AnChkAll.Name = "checkBox_AnChkAll";
this.checkBox_AnChkAll.Size = new System.Drawing.Size(48, 16);
this.checkBox_AnChkAll.TabIndex = 4;
this.checkBox_AnChkAll.Text = "全选";
this.checkBox_AnChkAll.UseVisualStyleBackColor = true;
this.checkBox_AnChkAll.CheckedChanged += new System.EventHandler(this.checkBox_AnChkAll_CheckedChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(281, 273);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(131, 12);
this.label1.TabIndex = 5;
this.label1.Text = "zhangyonghui ver1.0.0";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(37, 273);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(185, 12);
this.label2.TabIndex = 6;
this.label2.Text = "生成文件 jpegfile.h 在源目录下";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(37, 299);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(287, 12);
this.label3.TabIndex = 7;
this.label3.Text = "数组名:const unsigned char 图片名[]={0x00,...};";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(456, 336);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.checkBox_AnChkAll);
this.Controls.Add(this.but_to_h);
this.Controls.Add(this.label_filepath);
this.Controls.Add(this.checkedListBox_file);
this.Controls.Add(this.but_chfile);
this.Name = "Form1";
this.Text = "jpeg转h文件--BinTo_Hfile";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button but_chfile;
private System.Windows.Forms.CheckedListBox checkedListBox_file;
private System.Windows.Forms.Label label_filepath;
private System.Windows.Forms.Button but_to_h;
private System.Windows.Forms.CheckBox checkBox_AnChkAll;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
}
}