📄 mainform.cs
字号:
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace bmpFormat
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
// user params
//private String m_sPath;
//sys params
private System.Windows.Forms.Button EncodeBmp;
private System.Windows.Forms.Button DecodeBmp;
private System.Windows.Forms.TextBox CodeText;
private System.Windows.Forms.TextBox tbBmp;
private System.Windows.Forms.Button OpenBmp;
private System.Windows.Forms.OpenFileDialog oFDialogBmp;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public MainForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.EncodeBmp = new System.Windows.Forms.Button();
this.DecodeBmp = new System.Windows.Forms.Button();
this.CodeText = new System.Windows.Forms.TextBox();
this.tbBmp = new System.Windows.Forms.TextBox();
this.OpenBmp = new System.Windows.Forms.Button();
this.oFDialogBmp = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// EncodeBmp
//
this.EncodeBmp.Enabled = false;
this.EncodeBmp.Location = new System.Drawing.Point(344, 8);
this.EncodeBmp.Name = "EncodeBmp";
this.EncodeBmp.Size = new System.Drawing.Size(80, 24);
this.EncodeBmp.TabIndex = 0;
this.EncodeBmp.Text = "加密";
this.EncodeBmp.Click += new System.EventHandler(this.EncodeBmp_Click);
//
// DecodeBmp
//
this.DecodeBmp.Enabled = false;
this.DecodeBmp.Location = new System.Drawing.Point(432, 8);
this.DecodeBmp.Name = "DecodeBmp";
this.DecodeBmp.Size = new System.Drawing.Size(80, 24);
this.DecodeBmp.TabIndex = 1;
this.DecodeBmp.Text = "解密";
this.DecodeBmp.Click += new System.EventHandler(this.DecodeBmp_Click);
//
// CodeText
//
this.CodeText.Location = new System.Drawing.Point(16, 40);
this.CodeText.MaxLength = 65535;
this.CodeText.Multiline = true;
this.CodeText.Name = "CodeText";
this.CodeText.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.CodeText.Size = new System.Drawing.Size(496, 312);
this.CodeText.TabIndex = 2;
this.CodeText.Text = "";
//
// tbBmp
//
this.tbBmp.Location = new System.Drawing.Point(16, 8);
this.tbBmp.Name = "tbBmp";
this.tbBmp.ReadOnly = true;
this.tbBmp.Size = new System.Drawing.Size(240, 21);
this.tbBmp.TabIndex = 3;
this.tbBmp.Text = "";
//
// OpenBmp
//
this.OpenBmp.Location = new System.Drawing.Point(264, 8);
this.OpenBmp.Name = "OpenBmp";
this.OpenBmp.Size = new System.Drawing.Size(72, 24);
this.OpenBmp.TabIndex = 4;
this.OpenBmp.Text = "打开";
this.OpenBmp.Click += new System.EventHandler(this.OpenBmp_Click);
//
// oFDialogBmp
//
this.oFDialogBmp.Filter = "Bmp文件|*.bmp";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 365);
this.Controls.Add(this.OpenBmp);
this.Controls.Add(this.tbBmp);
this.Controls.Add(this.CodeText);
this.Controls.Add(this.DecodeBmp);
this.Controls.Add(this.EncodeBmp);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "MainForm";
this.Text = "BMP格式应用";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
/// <summary>
/// 错误日志
/// </summary>
/// <param name="szMsg">记录信息</param>
public void logs(string szMsg)
{
DateTime dt=DateTime.Now;
String szFile=string.Format("./logs/log{0}.log", DateTime.Now.ToLongDateString());
lock(typeof(MainForm))
{
using (StreamWriter sw = new StreamWriter(szFile, true))
{
sw.WriteLine(string.Format("[{0}]:{1}",DateTime.Now, szMsg));
}
}
}
private void ExtendBmp(String FILE_NAME, String FILE_NAME2)
{
try
{
if (!File.Exists(FILE_NAME))
{
MessageBox.Show(String.Format("{0} file not exists!", FILE_NAME));
return;
}
FileStream fsi = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fsi);
// Read data from Test.bmp.
int i;
byte [] bTmp = new byte[14];
if ((i=r.Read(bTmp, 0, 14)) != 14)
{
MessageBox.Show("读取文件头错误");
return;
}
bmpHeader bh = new bmpHeader(bTmp);
bTmp = new byte[40];
if ((i=r.Read(bTmp, 0, 40)) != 40)
{
MessageBox.Show("读取文件头错误");
return;
}
bmpInfoHeader bih = new bmpInfoHeader(bTmp);
//r.BaseStream.Length
if (bih.BitCount == 24)
{
byte[] bEncode = Encoding.UTF8.GetBytes(CodeText.Text);
if (((bh.FileSize - 54) / 4) < bEncode.Length)
{
MessageBox.Show("要加密文件过长,请重新选择文件载体(bmp文件)!");
r.Close();
fsi.Close();
return;
}
FileStream fso = new FileStream(FILE_NAME2, FileMode.Create, FileAccess.Write);
BinaryWriter w = new BinaryWriter(fso);
//bh.reserved = (uint)bEncode.Length;
bih.BitCount = 32;
bih.ColorsImportant = 32;
w.Write(bh.ToArray());
w.Write(bih.ToArray());
bTmp = new byte[4];
int nIndex = 0;
while ((i = r.Read(bTmp, 0, 3))> 0)
{
//if ( i == 4 && bEncode.Length > nIndex )
if ( i == 3)
bTmp[3] = 0;// bEncode[nIndex];
w.Write(bTmp);
nIndex ++;
}
w.Close();
fso.Close();
}
r.Close();
fsi.Close();
MessageBox.Show("扩展完成");
}
catch (Exception ep)
{
// Let the user know what went wrong.
logs("The file could not be read:");
logs(ep.Message);
}
}
private void DecodeBmp_Click(object sender, System.EventArgs e)
{
try
{
string FILE_NAME2 = tbBmp.Text;
if (!File.Exists(FILE_NAME2))
{
MessageBox.Show(String.Format("{0} file not exists!", FILE_NAME2));
return;
}
FileStream fsi = new FileStream(FILE_NAME2, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fsi);
// Read data from Test.bmp.
int i;
byte [] bTmp = new byte[14];
if ((i=r.Read(bTmp, 0, 14)) != 14)
{
MessageBox.Show("读取文件头错误");
r.Close();
fsi.Close();
return;
}
bmpHeader bh = new bmpHeader(bTmp);
bTmp = new byte[40];
if ((i=r.Read(bTmp, 0, 40)) != 40)
{
MessageBox.Show("读取文件头错误");
r.Close();
fsi.Close();
return;
}
bmpInfoHeader bih = new bmpInfoHeader(bTmp);
if (bh.reserved == 0 || bih.BitCount != 32)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -