I have a function that accepts a work-id string and generates a QRcode using the ZXing library and saves the image to to disk via Bitmap.save() method.The problem is non existent in my local machine and everything works fine but the GDI exception is thrown on production environment which works fine for a while but after some hours or sometimes after a day and an empty file is generated and the exception resolved only by recycling application pool. I have tried everything i can find from checking permissions to file path to memory analysis disposing the the bitmap but nothing seems to work.
public QRCodeModel GenerateQRCode(QRCodeModel obj, UserProfile profile)
{
try
{
string base64Image = "";
var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options.Height = 70;
writer.Options.Width = 70;
writer.Options.Margin = 0;
var result = writer.Write(obj.WORK_ID.ToString());
if (!Directory.Exists(obj.AbsolutePath))
{
Directory.CreateDirectory(obj.AbsolutePath);
}
using (var bitmap = new Bitmap(result))
{
//bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
//base64Image = Convert.ToBase64String(ms.GetBuffer()); //Get Base64
string curtime=obj.AbsolutePath+obj.WORK_ID+ DateTime.Now.Ticks.ToString() + ".png";
bitmap.Save(curtime, System.Drawing.Imaging.ImageFormat.Png);
base64Image = Convert.ToBase64String(File.ReadAllBytes(curtime)); //Get Base64
}
//}
string src = "data:image/png;base64," + base64Image;
string encodedString = HttpUtility.UrlEncode(src);
obj.ENCODED_IMAGE_BYTES = encodedString;
return obj;
}
catch (Exception ex)
{
throw ex;
}
}