原文 C#版二維碼生成器附皮膚下載html
前言c#
本文所使用的二維碼生成代碼是谷歌開源的條形碼圖像處理庫完成的,c#版的代碼可去https://code.google.com/p/zxing/downloads/list下載壓縮包。ruby
截止目前爲止最新版本爲2.2,提供如下編碼格式的支持:post
同時官網提供了 Android、cpp、C#、iPhone、j2me、j2se、jruby、objc、rim、symbian等多種應用的類庫,具體詳情能夠參考下載的源碼包中。this
本文已經下載好了,c#版的zxing代碼,並編譯生成了類庫,供程序直接調用。google
軟件截圖及說明編碼
功能說明:一、根據文字輸入及時生成二維碼,支持保存,打印url
二、支持解碼,打開二維碼文件解碼spa
三、採用IrisSkin4皮膚美化3d
代碼說明
public MainForm()
{
InitializeComponent();
this.skinEngine1.SkinFile = Application.StartupPath + "//skins//SportsGreen.ssk";
this.p1.Visible = true;
//this.p2.Visible = false;
//用於實現PictureBox的鼠標移動過去的提示
ToolTip toolTip = new ToolTip();
toolTip.AutoPopDelay = 5000;//一下4個都是屬性
toolTip.InitialDelay = 1000;
toolTip.ReshowDelay = 500;
toolTip.ShowAlways = true;
toolTip.SetToolTip(this.open, "打開");//參數1是button名,參數2是要顯示的內容
toolTip.SetToolTip(this.save, "保存");
toolTip.SetToolTip(this.print,"打印二維碼");
toolTip.SetToolTip(this.decod,"解碼");
toolTip.SetToolTip(this.exit,"返回生成二維碼");
}
主窗體顯示,皮膚,按鈕圖表鼠標移動顯示提示等代碼。
//隨文本框輸入內容生成二維碼圖片
private void txtContent_TextChanged(object sender, EventArgs e)
{
string content = txtContent.Text;
if (content == "")
{
MessageBox.Show("請輸入內容", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200);
Bitmap bitmap = toBitmap(byteMatrix);
orpic.Image = bitmap;
}
//生成圖片代碼
public static Bitmap toBitmap(ByteMatrix matrix)
{
int width = matrix.Width;
int height = matrix.Height;
Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
}
}
return bmap;
}
//時間顯示代碼
private void timer_Tick(object sender, EventArgs e)
{
this.timeshowlable.Text = DateTime.Now.ToString();
}
隨文本框輸入內容生成二維碼圖片及時間顯示代碼。
private void exit_MouseMove(object sender, MouseEventArgs e)
{
System.Drawing.Bitmap map = new Bitmap(Application.StartupPath+@"\image\exit-1.png");
this.exit.Image = map;
}
private void exit_MouseLeave(object sender, EventArgs e)
{
System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\exit.png");
this.exit.Image = map;
}
private void print_MouseMove(object sender, MouseEventArgs e)
{
System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\print-1.png");
this.print.Image = map;
}
private void print_MouseLeave(object sender, EventArgs e)
{
System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\print.png");
this.print.Image = map;
}
private void open_MouseLeave(object sender, EventArgs e)
{
System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\open.png");
this.open.Image = map;
}
private void open_MouseMove(object sender, MouseEventArgs e)
{
System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\open-1.png");
this.open.Image = map;
}
private void save_MouseLeave(object sender, EventArgs e)
{
System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\save.png");
this.save.Image = map;
}
private void save_MouseMove(object sender, MouseEventArgs e)
{
System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\save-1.png");
this.save.Image = map;
}
private void decod_MouseMove(object sender, MouseEventArgs e)
{
System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\encoding-1.png");
this.decod.Image = map;
}
private void decod_MouseLeave(object sender, EventArgs e)
{
System.Drawing.Bitmap map = new Bitmap(Application.StartupPath + @"\image\encoding.png");
this.decod.Image = map;
}
鼠標移動到按鈕圖片,圖片切換代碼。
private void open_Click(object sender, EventArgs e)
{
if (p1.Visible == true)
{
if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
string path = this.openFileDialog1.FileName;
openFileDialog1.DefaultExt = "*.txt";
openFileDialog1.Filter = "*.txt|";
if (path.EndsWith(".txt"))
{
string content = File.ReadAllText(path, Encoding.Default);
this.txtContent.Text = content;
}
else
{
MessageBox.Show("只能讀取txt文件","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
else
{
if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
string path = this.openFileDialog1.FileName;
if (!path.EndsWith(".png"))
{
MessageBox.Show("圖像格式不正確,只能讀取png文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
Image img = Image.FromFile(path);
Bitmap bmap;
try
{
bmap = new Bitmap(img);
}
catch (System.IO.IOException ioe)
{
MessageBox.Show(ioe.ToString());
return;
}
if (bmap == null)
{
MessageBox.Show("Could not decode image");
return;
}
else
{
this.picOpenQr.Image = bmap;
}
LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new com.google.zxing.common.HybridBinarizer(source));
Result result;
try
{
result = new MultiFormatReader().decode(bitmap);
}
catch (ReaderException re)
{
MessageBox.Show(re.ToString());
return;
}
this.txtDencoder.Text = (result.Text);
}
}
}
private void save_Click(object sender, EventArgs e)
{
if (p1.Visible == true)
{
saveFileDialog1.Filter = "*.png|";
//saveFileDialog1.DefaultExt = "*.png";
saveFileDialog1.RestoreDirectory = true;
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string localPath = saveFileDialog1.FileName.ToString();
Image qrcode = orpic.Image;
if (qrcode == null)
{
MessageBox.Show("無保存項", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (localPath.EndsWith(".png"))
{
qrcode.Save(localPath);
}
else
{
qrcode.Save(localPath + ".png");
}
}
}
}
else
{
saveFileDialog1.Filter = "*.txt|";
//saveFileDialog1.DefaultExt = "*.txt";
if(this.saveFileDialog1.ShowDialog()==DialogResult.OK)
{
string localPath = saveFileDialog1.FileName.ToString()+".txt";
if (File.Exists(localPath))
{
DialogResult RESULT = MessageBox.Show("是否確認覆蓋原有文件?", "信息提示", MessageBoxButtons.YesNo);
if (RESULT.ToString().Equals("Yes"))
{
File.Delete(localPath);
File.AppendAllText(localPath, this.txtDencoder.Text, Encoding.Default);
}
else
{
return;
}
}
else
{
File.AppendAllText(localPath, this.txtDencoder.Text, Encoding.Default);
}
}
}
}
private void exit_Click(object sender, EventArgs e)
{
this.p2.Visible = false;
this.p1.Visible=true;
}
private void decod_Click(object sender, EventArgs e)
{
this.p2.Visible=true;
this.p1.Visible = !(p1.Visible);
}
打開,保存,面板切換代碼。
private void print_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
if (printDialog.ShowDialog() == DialogResult.OK) { try
{
printDocument.Print();
}
catch
{ //中止打印
printDocument.PrintController.OnEndPrint(printDocument, new System.Drawing.Printing.PrintEventArgs());
}
}
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (this.p1.Visible == true)
{
e.Graphics.DrawImage(this.orpic.Image, 20, 20);
}
else
{
e.Graphics.DrawImage(this.picOpenQr.Image, 20, 20);
}
}
打印代碼。
關鍵代碼在txtContent_TextChanged()及打開方法中處理了。
最後說明:程序比較簡單,不作過多說明,時間倉促代碼存在不少不合理的地方,歡迎拍磚。
附件下載(帶皮膚):點我去下載