MakeReferenceQrCodeService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using MySystem.PxcModels;
  6. using Library;
  7. using LitJson;
  8. using System.Drawing.Imaging;
  9. using System.Drawing;
  10. namespace MySystem
  11. {
  12. public class MakeReferenceQrCodeService
  13. {
  14. public readonly static MakeReferenceQrCodeService Instance = new MakeReferenceQrCodeService();
  15. private MakeReferenceQrCodeService()
  16. { }
  17. public void StartListen()
  18. {
  19. Thread th = new Thread(Loop);
  20. th.IsBackground = true;
  21. th.Start();
  22. }
  23. private void Loop()
  24. {
  25. bool op = true;
  26. while (op)
  27. {
  28. string content = RedisDbconn.Instance.RPop<string>("MakeReferenceQrCode");
  29. if (!string.IsNullOrEmpty(content))
  30. {
  31. Start(content);
  32. }
  33. else
  34. {
  35. Thread.Sleep(5000);
  36. }
  37. }
  38. }
  39. public void Start(string uid)
  40. {
  41. try
  42. {
  43. int UserId = int.Parse(function.CheckInt(uid));
  44. if (UserId > 0)
  45. {
  46. WebCMSEntities db = new WebCMSEntities();
  47. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  48. if (user != null)
  49. {
  50. string resultpath = MergeQrCode(user);
  51. user.ReferenceQrCode = resultpath;
  52. db.SaveChanges();
  53. PxcModels.WebCMSEntities pxcdb = new PxcModels.WebCMSEntities();
  54. PxcModels.Users tmpuser = pxcdb.Users.FirstOrDefault(m => m.Id == UserId);
  55. if (tmpuser != null)
  56. {
  57. tmpuser.ReferenceQrCode = user.ReferenceQrCode;
  58. pxcdb.SaveChanges();
  59. }
  60. pxcdb.Dispose();
  61. }
  62. db.Dispose();
  63. }
  64. }
  65. catch (Exception ex)
  66. {
  67. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  68. }
  69. }
  70. public string MergeQrCode(Users user, int pid = 0)
  71. {
  72. string BgPic = "QrCodeBg.png";
  73. if (pid > 0)
  74. {
  75. BgPic = "QrCodeBg" + pid + ".png";
  76. }
  77. string path = function.CreateQRCode2(ConfigurationManager.AppSettings["SourceHost"].ToString() + "p/user-inviteregist-1?Id=" + user.Id, function.MD5_16(user.Id.ToString() + "9527"), "/bsserver_com/static/ReferenceQrCode/");
  78. path = path.Replace("//", "/");
  79. string resultpath = "/bsserver_com/static/ReferenceQrCode/" + function.MD5_16(user.Id.ToString() + "9527") + "Pic.png";
  80. MakeQRCode(function.getPath("/static/" + BgPic), function.getPath(path), function.getPath(resultpath), user, pid);
  81. return resultpath.Replace("bsserver_com/", "");
  82. }
  83. public void MakeQRCode(string sourcepath, string qrcode, string resultpath, Users us, int pid = 0)
  84. {
  85. System.Drawing.Image image = System.Drawing.Image.FromFile(sourcepath);
  86. System.Drawing.Image qrcodepic = System.Drawing.Image.FromFile(qrcode);
  87. //获取图片宽高
  88. int Width = image.Width;
  89. int Height = image.Height;
  90. //创建一个位图文件
  91. Bitmap BitmapResult = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
  92. //在位图文件上填充一个矩形框
  93. Graphics Grp = Graphics.FromImage(BitmapResult);
  94. System.Drawing.Rectangle Rec = new System.Drawing.Rectangle(0, 0, Width, Height);
  95. //定义一个白色的画刷
  96. // SolidBrush mySolidBrush = new SolidBrush(System.Drawing.Color.White);
  97. //Grp.Clear(Color.White);
  98. //将矩形框填充为白色
  99. // Grp.FillRectangle(mySolidBrush, Rec);
  100. //向矩形框内填充Img
  101. Grp.DrawImage(image, 0, 0, Rec, GraphicsUnit.Pixel);
  102. Grp.DrawImage(qrcodepic, 340, 605, 326, 326);
  103. //写字
  104. Font f = new Font("PingFang SC", 34);
  105. Font f2 = new Font("PingFang SC", 30);
  106. Color color = Color.FromArgb(255, 255, 255, 255);
  107. if (pid > 0 && pid != 2)
  108. {
  109. color = Color.FromArgb(255, 0, 0, 0);
  110. }
  111. Brush b = new SolidBrush(color);
  112. Brush b2 = new SolidBrush(color);
  113. StringFormat sf = new StringFormat();
  114. sf.LineAlignment = StringAlignment.Center;
  115. sf.Alignment = StringAlignment.Center;
  116. Grp.DrawString(us.RealName, f, b, new RectangleF()
  117. {
  118. X = 0,
  119. Y = 980,
  120. Width = Width,
  121. Height = 50,
  122. }, sf);
  123. Grp.DrawString("推荐码:" + us.MakerCode, f2, b2, new RectangleF()
  124. {
  125. X = 0,
  126. Y = 1030,
  127. Width = Width,
  128. Height = 50,
  129. }, sf);
  130. //返回位图文件
  131. ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Png);
  132. System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
  133. EncoderParameters myEncoderParameters = new EncoderParameters(1);
  134. EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);//这里的50L用来设置保存时的图片质量
  135. myEncoderParameters.Param[0] = myEncoderParameter;
  136. BitmapResult.Save(resultpath, jpgEncoder, myEncoderParameters);
  137. image.Dispose();
  138. qrcodepic.Dispose();
  139. Grp.Dispose();
  140. BitmapResult.Dispose();
  141. }
  142. private ImageCodecInfo GetEncoder(ImageFormat format)
  143. {
  144. ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
  145. foreach (ImageCodecInfo codec in codecs)
  146. {
  147. if (codec.FormatID == format.Guid)
  148. {
  149. return codec;
  150. }
  151. }
  152. return null;
  153. }
  154. }
  155. }