/// <summary>
/// 获取商品二维码图片
/// </summary>
/// <param name="productid"></param>
/// <returns></returns>
public static DResult GetProductQr(long productid, long memberid)
{
DResult dResult = new DResult() { Success = false };
Graphics g = null;
Image img = null;
try
{
var modelProduct = new ProductInfoBLL().GetModel(productid);
if (modelProduct == null || modelProduct.Id <= 0)
{
dResult.Message = "参数错误";
return dResult;
}
string agentCode = System.Configuration.ConfigurationManager.AppSettings["DefaultAgentCode"] + "";
string urlstr = string.Empty;
var member = new MemberBLL().GetModel(memberid);
if (member == null || member.MemberId <= 0)
{
dResult.Message = "请登录后分享此图片";
dResult.Code = -1;
return dResult;
}
if (memberid > 0)
{
ModelAgent modelAgent = new AgentBLL().GetModelByMemberId(memberid);
if (modelAgent != null && modelAgent.AgentId > 0)
{
agentCode = modelAgent.Code;
}
}
if (modelProduct.ProductType == EnumProductType.Solitaire.GetHashCode() || modelProduct.ProductType == EnumProductType.TwoPersonChip.GetHashCode())
{
urlstr = ConfigurationBLL.GetConfigValueByCache("SysWebH5") + "WeiXin/scanning?id=" + agentCode + "&retUrls=" + System.Web.HttpUtility.UrlEncode(ConfigurationBLL.GetConfigValueByCache("SysWebH5") + "index.html#/FreeDetail?Good_id=" + productid);
}
else
{
urlstr = ConfigurationBLL.GetConfigValueByCache("SysWebH5") + "WeiXin/scanning?id=" + agentCode + "&retUrls=" + System.Web.HttpUtility.UrlEncode(ConfigurationBLL.GetConfigValueByCache("SysWebH5") + "index.html#/GeneralItemDescription?Good_id=" + productid);
}
decimal SalePrice = modelProduct.SalePrice.Value;//商品最低价
var list = new ProductSKUBLL().GetListArray("ProductId=" + productid + " and SalePrice>0");
if (list != null && list.Count > 0)
{
var skuSalePrice = list.Min(p => p.SalePrice).Value;
if (skuSalePrice > 0 && skuSalePrice < SalePrice)
{
SalePrice = skuSalePrice;
}
}
string logo = modelProduct.ProductImg;
if (!logo.Contains("http"))
{
logo = (System.Configuration.ConfigurationManager.AppSettings["IMAGE_URL"] + "").TrimEnd('/') + "/" + logo;
}
string dir = HttpContext.Current.Server.MapPath("~/");
Log.Info("dir=" + dir);
var attachment = new AttachmentUtility();
byte[] bt = QRCodeHelper.QRCode(urlstr, logo, 430, false);
string fileName = "ProductQr/" + productid + "/" + memberid + "/" + DateTime.Now.Ticks + ".png";
string productQr = attachment.GetAttachmentFullUrl("Upload/" + attachment.SaveAttachPath(bt, "ProductQr", (fileName + "qr.png").Replace("ProductQr/", "")));
// string bg = dir + "images\\Share\\productqrbg.jpg";
// img = new Bitmap(new Bitmap(bg), 705, 1233);
img = new Bitmap(705, 1233, PixelFormat.Format32bppRgb);
g = Graphics.FromImage(img);
g.Clear(ColorTranslator.FromHtml("#ffffff"));
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//加上抗锯齿效果
g.DrawLine(new Pen(ColorTranslator.FromHtml("#D7D7D7")), new Point(30, 900), new Point(670, 900));
Point[] destPoints1 = {
new Point(0, 0),//图片左上点
new Point(705, 0),//图片右上点
new Point(0, 705),//图片左下点
};
Bitmap btm = null;
try
{
Uri myUri = new Uri(logo);
WebRequest webRequest = WebRequest.Create(myUri);
webRequest.Timeout = 30000;
WebResponse webResponse = webRequest.GetResponse();
btm = new Bitmap(webResponse.GetResponseStream());
g.DrawImage(btm, destPoints1);//按指定的顶点画图
}
catch (Exception ex)
{
SysLog.SaveException(ex, "生成二维码logo异常:" + logo);
}
finally
{
if (null != btm)
btm.Dispose();
}
string UserIcon = member.UserIcon;
if (string.IsNullOrEmpty(UserIcon))
{
UserIcon = dir + "images\\Share\\logo.png";
}
Bitmap btm1 = null;//头像
try
{
Uri myUri = new Uri(UserIcon);
WebRequest webRequest = WebRequest.Create(myUri);
webRequest.Timeout = 30000;
WebResponse webResponse = webRequest.GetResponse();
btm1 = new Bitmap(webResponse.GetResponseStream());
g.DrawImage(CirclePhoto(btm1, 90), new Point(25, 960));//按指定的顶点画图
string[] hp = ConfigurationBLL.GetConfigValueByCache("GetProductQrHaoPing").Split('|');
int index = new Random().Next(0, hp.Length); //生成随机下标
g.DrawString(member.NickName, new Font("微软雅黑", 20f, FontStyle.Regular, GraphicsUnit.Point), new SolidBrush(ColorTranslator.FromHtml("#424242")), new RectangleF(120, 960, 400, 50));
g.DrawString("五星好评", new Font("微软雅黑", 18f, FontStyle.Regular, GraphicsUnit.Point), new SolidBrush(ColorTranslator.FromHtml("#999999")), new RectangleF(120, 1010, 400, 50));
g.DrawString(hp[index], new Font("微软雅黑", 20f, FontStyle.Regular, GraphicsUnit.Point), new SolidBrush(ColorTranslator.FromHtml("#424242")), new RectangleF(120, 1055, 400, 50));
}
catch (Exception ex)
{
SysLog.SaveException(ex, "生成二维码logo异常:" + logo);
}
finally
{
if (null != btm1)
btm1.Dispose();
}
try
{
string xingxiang = "iVBORw0KGgoAAAANSUhEUgAAAJIAAAAbCAYAAACXxNaeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAnrSURBVGhDvZsLsFVVHcZXZZCJQZJgJErxshQVHbVpFJWh0R6T0VT2oHGKNCuzp4ZmZTSTZgzN2EMtiWIgkoF77hFzsEnQmrKMVFJjQIXRsDIqM5UMjN3vW/t/zj1nn3XOXvvcve83880+dz3+316PvR7/ta7rB8ka9yL7OaKQbpK4F9ifI4LkBvfixI2sppBc6Q6AL7Q/RwTSG7G2TepuUjLorkoG3DEWNCJIVriDkpr7KtrzRqozJevc4Wh+Hc13WtCIgDo+GN1L0b14JD8c9D6N9oV0plEWVB0o4I3webjcgkYEFPLzcB+6d1DQAy24MqgB0fscfA7N7XSqIy2qcqB1Jpr/QPtZnidbcKVIbnanorUHzb/wnGLB1UCVicj/EEvgHhr0EIuqFPaF/tnrSn+dO8GiKgNlG4vWz62satALLKpyUN5vmm7CyL+06lHJT2k1N9jUrLtLLKoaIPKVpljKyy2qUqDzPvhci+5Ki6oMdKTj0fm36e2Hq5L17qUWXRnQHYPWY6Yr/oawIyy6EtBZj6MjPdHUrLltaFYzvdl64e6WAoo7k1Xu5ZakEthodBNaasyGrkaIqZak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013lGfBW/i7tcBK+wEzFQ3yfdjnbdiRL6rmVqEzt6m53k1n5HoL8Rua6VKqgy8yU4WAvZPIO+TTScuyweukF9x0Yj+Vd5jD80bisj6nlXlO1yywq5sZWVfHnXA+Orr5kWoOujdArRvbHZi8h5nKB2LjySAnnV5clbqpl3PRHF26yy0XfFrYAXehRUfD//+Y7kWnL6xKvRc7p1p0B7wzccD7nVpHqOssOhrkaVyp1eWx+7F5dq8GoqHnk+4R0jc6QV83NbHRGPVl5wn+XtjLDqPJCaS7x+pGdXSHrspYdBTIo39xUudPR9u6O9+igiCNOtQDMB2NqR+Lyoc/Idd2uOZ+RuaP8Aye4WRBA5xO+tVQ10fea8HR8B2JHR/5f0X+y2iwwy2qJ6j8WeSRx1bTwFUWHA3e+xLy3Yn2FXCSBXeFX2esdUfzjt8inxpjSV8dSR1y0P0aO0t4h9dacE/w0eoIS9v3h3gWd0im/0RxF1wKZ8V4xdGQn02auqm60YLjQGc6lMJNwEihHQnpx5JvRvZCWSx44XFqzKJXWf2oqCG5n8X2MnZtckoW8dgCO82fpnqyoELQ4a4va0GvOG0z2i8vdEku8nikAV9P6owFr8/6j1yH66vdZAvKwLn/AzIqwPfZFi0TAAAAAElFTkSuQmCC";
byte[] imageBytes = Convert.FromBase64String(xingxiang);
//读入MemoryStream对象
MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
memoryStream.Write(imageBytes, 0, imageBytes.Length);
//转成图片
Image image = Image.FromStream(memoryStream);
g.DrawImage(image, new Point(225, 1012));//按指定的顶点画图
}
catch (Exception ex)
{
g.DrawString("★★★★★", new Font("微软雅黑", 24f, FontStyle.Bold, GraphicsUnit.Point), new SolidBrush(ColorTranslator.FromHtml("#FFB000")), new RectangleF(225, 1005, 400, 50));
}
MemoryStream ms = new MemoryStream(bt);
Image qr = new Bitmap(System.Drawing.Image.FromStream(ms), 240, 240);
g.DrawImage(qr, new Point(440, 910));//按指定的顶点画图
g.DrawString(modelProduct.ProductTitle, new Font("微软雅黑", 22f, FontStyle.Regular, GraphicsUnit.Point), new SolidBrush(ColorTranslator.FromHtml("#424242")), new RectangleF(30, 720, 670, 90));
g.DrawString("市场价", new Font("微软雅黑", 22f, FontStyle.Regular, GraphicsUnit.Point), new SolidBrush(ColorTranslator.FromHtml("#999999")), new RectangleF(235, 820, 400, 50));
g.DrawString("¥" + modelProduct.MarketPrice.Value.ToString("N2") + " ", new Font("微软雅黑", 22f, FontStyle.Strikeout, GraphicsUnit.Point), new SolidBrush(ColorTranslator.FromHtml("#999999")), new RectangleF(330, 820, 400, 50));
g.DrawString("¥" + SalePrice.ToString("N2"), new Font("微软雅黑", 22f, FontStyle.Bold, GraphicsUnit.Point), new SolidBrush(ColorTranslator.FromHtml("#E47E3A")), new RectangleF(500, 820, 400, 50));
g.DrawString("长按扫码查看", new Font("微软雅黑", 20f, FontStyle.Regular, GraphicsUnit.Point), new SolidBrush(ColorTranslator.FromHtml("#666666")), new RectangleF(470, 1150, 400, 50));
MemoryStream MStreamBit = new MemoryStream();
img.Save(MStreamBit, ImageFormat.Jpeg);
byte[] _byte = MStreamBit.GetBuffer();
string productAd = attachment.GetAttachmentFullUrl("Upload/" + attachment.SaveAttachPath(_byte, "ProductQr", fileName.Replace("ProductQr/", "")));
return new DResult()
{
Success = true,
Message = "成功",
Data = new
{
productAd = productAd,
productQr = productQr
}
};
}
catch (Exception ex)
{
Log.Error(ex, "生成商品二维码失败", string.Format("生成二维码失败user={0},异常信息:{1}", memberid, ex.Message + ex.StackTrace));
}
finally
{
if (g != null)
{
g.Dispose();
}
if (img != null)
{
img.Dispose();
}
}
return new DResult() { Success = false, Message = "生成商品二维码失败" };
}