通用GDI +错误发生QR码

时间:2016-03-30 04:01:33

标签: c# .net multithreading api

当有大量请求进入时,我会收到几个通用的gdi +错误代码。大多数时候这部分都没有错误。

我相信这部分的错误

            string guidString = Guid.NewGuid().ToString();
            string dateNowString = DateTime.Now.ToString("ddMMyyyHHmmssfff");
            var localFilePath = @"c:\temp\" + dateNowString + ".png";

            //Create QrCode on local
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode = qrEncoder.Encode(pass.Barcode);
            Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);
            renderer.CreateImageFile(qrCode.Matrix, localFilePath, ImageFormat.Png);
            /*using (MemoryStream ms = new MemoryStream())
            {
                using (FileStream stream = new FileStream(localFilePath, FileMode.Create))
                {
                    renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.Png);
                }
            }*/

            //Upload locally saved QR image to AWS S3
            string bucketName = ConfigurationManager.AppSettings["bucketName"].ToString();
            var s3Client = new S3(bucketName, guidString);
            string key = guidString + dateNowString + ConfigurationManager.AppSettings["FileFormatExtension"].ToString();
            qrCodeUrl = s3Client.UploadFile(localFilePath, key);

            //Delete QrCode image on local
            if (File.Exists(localFilePath))
            {
                File.Delete(localFilePath);
            }

            //Save QrCode Url to BoardingPass Table
            pass.QrCodeUrl = qrCodeUrl;
            data.SaveChanges();

这是由几个线程调用的(1个调用= 1个线程,我正在应用一个任务来同时运行这些线程,一个线程可能多次调用这个部分)

这里是stacktrace

   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)     at Gma.QrCodeNet.Encoding.Windows.Controls.Renderer.CreateImageFile(BitMatrix matrix, String fileName, ImageFormat imageFormat)     at <redacted>API.Helpers.BoardingPassHelper.GenerateQRCode(BoardingPassRequest request) in <redacted>.API\Helpers\BoardingPassHelper.cs:line 972     at BCSBoardingPass.API.Controllers.API.BoardingPassController.GenerateMultiPaxBoardingPass(MBPRequest mbpRequest, Int32 requestIndex, Boolean isMbp) in <redacted>controllers\API\BoardingPassController.cs:line 615

1 个答案:

答案 0 :(得分:2)

在多线程环境中,我们使用库的0.3版本发生了这种随机错误。我们没有设法修复这种随机行为,但我们注意到,由于我们将图像格式从PNG切换为JPEG,我们再也没有得到错误。