private void RefreshQrCode(QRCodeControl qRCodeControl) { if (IsRefresh) { if (index <= 5) { index++; qRCodeControl.QrCodeContent += " "; } else { index = 0; qRCodeControl.QrCodeContent = qRCodeControl.QrCodeContent.Trim(); } qRCodeControl.QRCodeImage = GetQRCodeImage(qRCodeControl); } }
/// <summary> /// 得到二维码 /// </summary> /// <param name="qRCodeControl"></param> /// <param name="content">二维码内容</param> /// <param name="icon">二维码图标</param> /// <returns></returns> private static ImageSource GetQRCodeImage(QRCodeControl qrCodeControl) { System.Drawing.ColorConverter colorConverter = new System.Drawing.ColorConverter(); using (QRCodeGenerator qrGenerator = new QRCodeGenerator()) { using (QRCodeData qRCodeData = qrGenerator.CreateQrCode(qrCodeControl.QrCodeContent, QRCodeGenerator.ECCLevel.L)) { using (QRCode qrCode = new QRCode(qRCodeData)) { Bitmap codeImage = qrCode.GetGraphic( qrCodeControl.QrCodePixelsPerModule, (System.Drawing.Color)colorConverter.ConvertFromString(qrCodeControl.Foreground.ToString()), System.Drawing.Color.FromName(((SolidColorBrush)qrCodeControl.Background).Color.ToString()), ImageBitmapConverter.ToBitmap(qrCodeControl.QrCodeIcon), qrCodeControl.QrCodeIconSizePercent, qrCodeControl.QrCodeIconBorderWidth); qrCodeControl.QRCodeImage = ImageBitmapConverter.ToImageSource(codeImage); //qrCodeControl.QRCodeImage = Imaging.CreateBitmapSourceFromHBitmap(codeImage.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); return(qrCodeControl.QRCodeImage); } } } }