PDF 生成、解析二维码
界面样式:
第一步:下载ThoughtWorks.QRCode.dll 类并引用
地址 http://download.csdn.net/detail/liwb1987/8500113
第二步:设置控件到界面上
第三部:生成按钮下代码
private void btnCreateQRCodeClick(object sender, RoutedEventArgs e)
{
try
{
ShowQRCode();
}
catch (Exception ex)
{
MessageBox.Show("生成二维码出错!", "系统提示");
return;
}
}
ShowQRCode(); 在这个方法里面有调用了三个方法都有返回值
/// <summary>
/// 显示生成的二维码
/// </summary>
public void ShowQRCode()
{
if (txtQRCodeContent.Text.Trim().Length <= )
{
MessageBox.Show("二维码内容不能为空,请输入内容!", "系统提示");
txtQRCodeContent.Focus();
return;
}
bimg = CreateQRCode(txtQRCodeContent.Text);
imgQRcode.Source = BitmapToBitmapImage(bimg);
ResetImageStrethch(imgQRcode, bimg);
}
(1)CreateQRCode 将文本转换成为 Bitmap 格式
public Bitmap CreateQRCode(string content)
{
QRCodeEncoder qrEncoder = new QRCodeEncoder();
qrEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrEncoder.QRCodeScale = Convert.ToInt32(txtSize.Text);
qrEncoder.QRCodeVersion = Convert.ToInt32(cboVersion.SelectedValue);
qrEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
try
{
Bitmap qrcode = qrEncoder.Encode(content, Encoding.UTF8);
if (!logoImagepath.Equals(string.Empty))
{
Graphics g = Graphics.FromImage(qrcode);
Bitmap bitmapLogo = new Bitmap(logoImagepath);
int logoSize = Convert.ToInt32(txtLogoSize.Text);
bitmapLogo = new Bitmap(bitmapLogo, new System.Drawing.Size(logoSize, logoSize));
PointF point = new PointF(qrcode.Width / - logoSize / , qrcode.Height / - logoSize / );
g.DrawImage(bitmapLogo, point);
}
return qrcode;
}
catch (IndexOutOfRangeException ex)
{
MessageBox.Show("超出当前二维码版本的容量上限,请选择更高的二维码版本!", "系统提示");
return new Bitmap(, );
}
catch (Exception ex)
{
MessageBox.Show("生成二维码出错!", "系统提示");
return new Bitmap(, );
}
}
BitmapToBitmapImage 再次转换成image 格式 显示在界面上
public BitmapImage BitmapToBitmapImage(Bitmap bitmap)
{
BitmapImage bImage = new BitmapImage();
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
bImage.BeginInit();
bImage.StreamSource = new MemoryStream(ms.ToArray());
bImage.EndInit();
return bImage;
}
ResetImageStrethch(填充附加图片)
private void ResetImageStrethch(System.Windows.Controls.Image img, Bitmap bImg)
{
if (bImg.Width <= img.Width)
{
img.Stretch = Stretch.None;
}
else
{
img.Stretch = Stretch.Fill;
}
}
添加Logo 添加按钮
/// <summary>
/// 添加Logo按钮处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAddLogoClick(object sender, RoutedEventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Filter = "图片文件|*.jpg;*.png;*.gif|All files(*.*)|*.*";
if (openDialog.ShowDialog() == true)
{
logoImagepath = openDialog.FileName;
Bitmap bImg = new Bitmap(logoImagepath);
imgLogo.Source = new BitmapImage(new Uri(openDialog.FileName));
ResetImageStrethch(imgLogo, bImg);
}
}
第三步 解析二维码
解析按钮下代码
private void DecodeQRCodeClick(object sender, RoutedEventArgs e)
{
try
{
tbDecodeResult.Text = "";
DecodeQRCode();
}
catch (Exception ex)
{
MessageBox.Show("解析二维码出错!", "系统提示");
return;
}
}
DecodeQRCode()方法
/// <summary>
/// 解析二维码
/// </summary>
public void DecodeQRCode()
{
if (bimg == null)
{
MessageBox.Show("请先打开一张二维码图片!", "系统提示");
return;
}
QRCodeDecoder qrDecoder = new QRCodeDecoder();
QRCodeImage qrImage = new QRCodeBitmapImage(bimg);
tbDecodeResult.Text = qrDecoder.decode(qrImage, Encoding.UTF8);
}
将解析的内容显示在界面上
第四步
打开二维码按钮
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Filter = "图片文件|*.jpg;*.png;*.gif|All files(*.*)|*.*";
if (openDialog.ShowDialog() == true)
{
bimg = new Bitmap(openDialog.FileName);
imgQRcode.Source = new BitmapImage(new Uri(openDialog.FileName));
ResetImageStrethch(imgQRcode, bimg);
}
保存按钮
private void btnSaveClick(object sender, RoutedEventArgs e)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "Png文件(*.Png)|*.png|All files(*.*)|*.*";
if (saveDialog.ShowDialog() == true)
{
try
{
SaveQRCode(saveDialog.FileName);
MessageBox.Show("保存成功!", "系统提示");
}
catch (Exception ex)
{
MessageBox.Show("保存二维码出错!", "系统提示");
return;
}
}
}
C# winform生成二维码
界面样式