SuperDesign/Source/开发辅助工具/Tools/FrmQrCode.cs
鑫Intel 03bcb8c5fd ### 2023-02-21更新
------
#### SuperDesign    V3.0.2302.2101
- *.[新增]全新文本编辑器,支持高亮、FTP查看和编辑、目录浏览,历史版本等众多功能。
2023-02-21 09:46:13 +08:00

166 lines
5.9 KiB
C#

using _SCREEN_CAPTURE;
using ryCommon._SCREEN_CAPTURE;
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 WeifenLuo.WinFormsUI.Docking;
using ZXing;
namespace .Tools
{
public partial class FrmQrCode : DockContent
{
public FrmQrCode()
{
InitializeComponent();
}
/// <summary>
/// 生成二维码图片
/// </summary>
/// <param name="strMessage">要生成二维码的字符串</param>
/// <param name="width">二维码图片宽度</param>
/// <param name="height">二维码图片高度</param>
/// <returns></returns>
private Bitmap CreateQrCode(String strMessage, Int32 width, Int32 height)
{
Bitmap result = null;
try
{
BarcodeWriter barCodeWriter = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE
};
barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
barCodeWriter.Options.Height = height;
barCodeWriter.Options.Width = width;
barCodeWriter.Options.Margin = 0;
ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
result = barCodeWriter.Write(bm);
Bitmap qr_bg = (Bitmap)ryCommon.RyImage.LoadPic(Application.StartupPath + "\\mod\\qr_bg.jpg");
Graphics g = Graphics.FromImage(qr_bg);
g.DrawImage(result, new Rectangle(25, 25, qr_bg.Width - 50, qr_bg.Height - 50));
result = qr_bg;
}
catch (Exception)
{
//异常输出
}
return result;
}
private void BtnCreate_Click(object sender, EventArgs e)
{
pictureBox1.Image= CreateQrCode(TxtFromStr.Text, 256, 256);
}
private void BtnBrowser_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
TxtQrCodePath.Text = openFileDialog1.FileName;
BtnAnaly.PerformClick();
}
}
private void BtnAnaly_Click(object sender, EventArgs e)
{
string path = TxtQrCodePath.Text;
if (!System.IO.File.Exists(path)) { Init(); return; }
var img = ryCommon.RyImage.LoadPic(path);
if (img == null) { Init(); return; }
Bitmap bmap;
try
{
bmap = new Bitmap(img);
}
catch (System.IO.IOException ioe)
{
MessageBox.Show(ioe.ToString(),"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (bmap == null)
{
MessageBox.Show("不能解码图片","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
var result = reader.Decode(bmap);
TxtToResult.Text = (result == null) ? "" : result.Text;
pictureBox2.Image = img;
}
catch (Exception)
{
Init();
MessageBox.Show("解析二维码失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
void Init()
{
pictureBox2.Image = null; TxtToResult.Text = "";
}
}
private void PictureBox1_Click(object sender, EventArgs e)
{
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
try
{
pictureBox1.Image.Save(saveFileDialog1.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void BtnCapture_Click(object sender, EventArgs e)
{
ScreenCapture capture = new ScreenCapture();
Bitmap m = capture.StartCapture(false);
if (m != null)
{
try
{
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
var result = reader.Decode(m);
TxtToResult.Text = (result == null) ? "" : result.Text;
pictureBox2.Image = m;
}
catch (Exception)
{
Init();
MessageBox.Show("解析二维码失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
void Init()
{
pictureBox2.Image = null; TxtToResult.Text = "";
}
}
}
private void TxtFromStr_DoubleClick(object sender, EventArgs e)
{
ryControls.TextBoxEx2 txt = (ryControls.TextBoxEx2)sender;
.Controls.FrmText frm = new Controls.FrmText
{
Icon = Icon
};
frm.richTextBox1.Text = txt.Text;
if (frm.ShowDialog() == DialogResult.OK)
{
txt.Text = frm.richTextBox1.Text;
}
}
}
}