using Container.Common; using QRCoder; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Container.Win { /// /// RichAD.xaml 的交互逻辑 /// public partial class RichAD : UserControl { Class_Log log = new Class_Log(); App app = ((App)Application.Current); public RichAD(string logo, string name,string devNum, string floor, string floorOrder, string houseNum,string xaxis, string shopFormatName,string shopFormatIcon,string applets) { InitializeComponent(); try { this.TxtName.Text = name; this.TxtFloor.Text = floor; logo = app.HttpUrl+ logo; var httpPath = logo.Substring(0, logo.IndexOf("://") + 3); var fileBackPath = logo.Substring(logo.IndexOf("://") + 3).Replace("//", "/").Replace("\\", "/").Replace(@"\", @"/"); logo = httpPath + fileBackPath; Uri uri = new Uri(logo, UriKind.Absolute); this.ImgLogo.Source = BitmapFrame.Create(uri); if (string.IsNullOrEmpty(shopFormatIcon)) { this.imgType.Visibility = Visibility.Hidden; this.TxtType.Margin = new Thickness(320,-20,0,0); } else { this.imgType.Visibility = Visibility.Visible; shopFormatIcon = app.HttpUrl + shopFormatIcon; var httpIconPath = shopFormatIcon.Substring(0, shopFormatIcon.IndexOf("://") + 3); var fileBackIconPath = shopFormatIcon.Substring(shopFormatIcon.IndexOf("://") + 3).Replace("//", "/").Replace("\\", "/").Replace(@"\", @"/"); shopFormatIcon = httpIconPath + fileBackIconPath; Uri uriIcon = new Uri(shopFormatIcon, UriKind.Absolute); this.iconImage.Source = BitmapFrame.Create(uriIcon); this.TxtType.Margin = new Thickness(380, -20, 0, 0); } this.TxtType.Text = shopFormatName; CreateQR(applets+ "?sname="+ devNum + "&spoint="+xaxis+"&sfloororder="+floorOrder+"&ehousenum="+houseNum); } catch (Exception e) { log.WriteLogFile(e.ToString(), "RichAD"); } } private void CreateQR(string plainText) { Class_Log log = new Class_Log(); try { QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator(); //QRCodeGenerator.ECCLevel:纠错能力,Q级:约可纠错25%的数据码字 QRCodeData qrCodeData = qrGenerator.CreateQrCode(plainText, QRCodeGenerator.ECCLevel.Q); QRCode qrcode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrcode.GetGraphic(15); MemoryStream ms = new MemoryStream(); qrCodeImage.Save(ms, ImageFormat.Png); ImageSourceConverter imageSourceConverter = new ImageSourceConverter(); QRImage.Source = (ImageSource)imageSourceConverter.ConvertFrom(ms); } catch (Exception ex) { log.WriteLogFile(ex.ToString(), "qr"); } } public static byte[] GetImageFromResponse(string url, string cookie = null) { redo: try { System.Net.WebRequest request = System.Net.WebRequest.Create(url); if (!string.IsNullOrWhiteSpace(cookie)) { request.Headers[System.Net.HttpRequestHeader.Cookie] = cookie; } System.Net.WebResponse response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { using (MemoryStream ms = new MemoryStream()) { Byte[] buffer = new Byte[1024]; int current = 0; do { ms.Write(buffer, 0, current); } while ((current = stream.Read(buffer, 0, buffer.Length)) != 0); return ms.ToArray(); } } } catch (System.Net.WebException ex) { if (ex.Message == "基础连接已经关闭: 发送时发生错误。") { goto redo; } else { throw; } } } } }