Nopcommerce 使用Task时dbcontext关闭问题

时间:2021-01-23 19:15:41

1、开启一个线程

Task.Run(() => { CreatPrintImage(preViewModel.DiyProductGuid); });

2、线程代码

        /// <summary>
/// 生成打印图片 (这里一定要异步,开另外一个线程调用,不用接受返回)
/// </summary>
/// <param name="diyGuid"></param>
/// <returns></returns>
public void CreatPrintImage(string diyGuid)
{
lock (obj)
{
using (var lifeTime = EngineContext.Current.ContainerManager.Container.BeginLifetimeScope())
{
var productService = lifeTime.Resolve<IProductService>();
var pictureService = lifeTime.Resolve<IPictureService>();
var downloadService = lifeTime.Resolve<IDownloadService>();
var product = productService.GetProdcutByDiyGuid(diyGuid);
//var product = _productService.GetProductById(productId);
var diyItem = product.DiyItem;
var diyItemTemplate = diyItem.DiyItemTemplates.FirstOrDefault();
foreach (var pp in product.ProductPictures)
{
#region 合成打印图片
double bitmapWidth = 0;
double bitmapHeight = 0;
if (diyItemTemplate != null)
{
//如果是盒子
if (diyItem.DiyType.IdentNumber == "10005")
{
switch (pp.DiyItemTemplateName)
{
case "上":
bitmapWidth = diyItemTemplate.EditWidth;
bitmapHeight = diyItemTemplate.EditHeight;
break;
case "下":
bitmapWidth = diyItemTemplate.EditWidth;
bitmapHeight = diyItemTemplate.EditHeight;
break;
case "左":
bitmapWidth = diyItemTemplate.EditHeight;
bitmapHeight = diyItemTemplate.EditLong ?? 0;
break;
case "右":
bitmapWidth = diyItemTemplate.EditHeight;
bitmapHeight = diyItemTemplate.EditLong ?? 0;
break;
case "中":
bitmapWidth = diyItemTemplate.EditWidth;
bitmapHeight = diyItemTemplate.EditLong ?? 0;
break;
}
}
else
{
bitmapWidth = diyItemTemplate.EditWidth;
bitmapHeight = diyItemTemplate.EditHeight;
}
var bitmap = new Bitmap((int)bitmapWidth, (int)bitmapHeight);
var huabug = Graphics.FromImage(bitmap);
foreach (var sideItem in pp.DiyInfoSideItems.OrderBy(s => s.DisplayOrder))
{
var printDiyInfo = new PrintDiyInfo();
printDiyInfo.RotationAngle = sideItem.DiyInfoRotate;
printDiyInfo.CenterMarginLeft = sideItem.CenterMarginLeft ?? 0;
printDiyInfo.CenterMarginTop = sideItem.CenterMarginTop ?? 0;
printDiyInfo.TemplateZoom = pp.ShowScaling ?? 0;
printDiyInfo.EditWidth = 0;
printDiyInfo.EditHeight = 0;
//如果是盒子
if (diyItem.DiyType.IdentNumber == "10005")
{
switch (pp.DiyItemTemplateName)
{
case "上":
printDiyInfo.RotationAngle = 180;
break;
case "下":
printDiyInfo.RotationAngle = 0;
break;
case "左":
printDiyInfo.RotationAngle = 90;
break;
case "右":
printDiyInfo.RotationAngle = 270;
break;
}
}
//如果是图片或者是贴纸
if (sideItem.DiyInfoType == DiyInfoTypeEnum.Picture ||
sideItem.DiyInfoType == DiyInfoTypeEnum.Paster)
{
string originalPictureUrl = sideItem.DiyInfoType == DiyInfoTypeEnum.Picture ? pictureService.GetPictureUrl(sideItem.PictureId ?? 0) : pictureService.GetPictureUrl(sideItem.PasterId ?? 0);
var mapPath = _webHelper.MapPath(originalPictureUrl.Substring(originalPictureUrl.IndexOf("/Content", StringComparison.Ordinal)));
var bit = new Bitmap(mapPath);
//缩放
var bm1 = new Bitmap(bit, Convert.ToInt32(bit.Width * printDiyInfo.TemplateZoom * sideItem.DiyInfoZoom), Convert.ToInt32(bit.Height * printDiyInfo.TemplateZoom * sideItem.DiyInfoZoom));
var ms = new MemoryStream();
bm1.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.GetBuffer();
//旋转
var imageFactory = new ImageFactory();
imageFactory.Load(bytes);
imageFactory.Rotate((float)printDiyInfo.RotationAngle);
var rotationImage = imageFactory.Image;
if (rotationImage != null)
{
var pointx = Convert.ToInt32(printDiyInfo.CenterMarginLeft * printDiyInfo.TemplateZoom - rotationImage.Width / 2);
var pointy = Convert.ToInt32(printDiyInfo.CenterMarginTop * printDiyInfo.TemplateZoom - rotationImage.Height / 2);
var ulCorner = new Point(pointx, pointy);
huabug.DrawImage(rotationImage, ulCorner);
}
}
else if (sideItem.DiyInfoType == DiyInfoTypeEnum.Font)
{
var sidefont = downloadService.GetDownloadById(sideItem.FontId ?? 0);
if (sidefont != null)
{
var fontUrl = _webHelper.MapPath(sidefont.DownloadUrl);
var frm = new PrivateFontCollection();
frm.AddFontFile(fontUrl);
var fontFamily = frm.Families[0];
var fontSize = 51 * (float)sideItem.DiyInfoZoom;
var fontColor = ColorTranslator.FromHtml(sideItem.FontColor);
var fontStyle = new FontStyle();
if ((sideItem.FontIsBold ?? false) && (sideItem.FontIsItalic ?? false))
{
fontStyle = FontStyle.Bold | FontStyle.Italic;
}
else if ((sideItem.FontIsBold ?? false))
{
fontStyle = FontStyle.Bold;
}
else if (sideItem.FontIsItalic ?? false)
{
fontStyle = FontStyle.Italic;
}
var font = new Font(fontFamily, fontSize, fontStyle);
//定义颜色
var sbrush = new SolidBrush(fontColor);
// 绘制围绕点旋转的文本
var pointx = Convert.ToInt32(printDiyInfo.CenterMarginLeft * printDiyInfo.TemplateZoom);
var pointy = Convert.ToInt32(printDiyInfo.CenterMarginTop * printDiyInfo.TemplateZoom);
var point = new PointF(pointx, pointy);
//var point = new PointF(200, 200);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
Matrix mtxRotate = huabug.Transform;
mtxRotate.RotateAt((float)printDiyInfo.RotationAngle, point); // 旋转角度和平移
huabug.Transform = mtxRotate;
huabug.DrawString(sideItem.FontString, font, sbrush, point, format);
}
}
}
string printMapPath = _webHelper.MapPath(_filePath);
if (!System.IO.Directory.Exists(printMapPath))
{
System.IO.Directory.CreateDirectory(printMapPath);
}
var printFileName = pp.DiyItemTemplateName + "diy_" + Guid.NewGuid().ToString().ToLower().Replace("-", "") + ".png";
var printPicUrl = "/Content/Uploads/Images/PrintImage/" + DateTime.Now.ToString("yyyyMM") + "/" + printFileName;
bitmap.Save(_webHelper.MapPath(printPicUrl));
pp.PrintImageUrl = printPicUrl;
#endregion
//productPictureService.UpdateProductPicture(pp);
}
}
productService.UpdateProduct(product);
}
}
}