Our code works for an indeterminate period of time, but after that time, the call always throws an exception until the server is rebooted. It is not resolved by restarting the IIS website, its app pool, or restarting IIS.
The exception is:
System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
The code is as follows:
Document template;
using (var file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
template = new Document(file);
}
var document = new Document();
var builder = new DocumentBuilder(document);
builder.Document.RemoveAllChildren();
builder.Document.AppendDocument(template, ImportFormatMode.KeepSourceFormatting);
builder.MoveToSection(nextSectionIndex);
var nodes = builder.CurrentSection.GetChildNodes(NodeType.Shape, true).Cast<Shape>().ToList();
...
Image img = new Bitmap(300, 300);
using (var fontFamily = new FontFamily("Arial"))
using (var font = new System.Drawing.Font(fontFamily, 13))
using (var textBrush = new SolidBrush(Color.LightGray))
using (var drawing = Graphics.FromImage(img))
{
drawing.Clear(Color.White);
drawing.TranslateTransform(-190.0F, 100.0F);
drawing.RotateTransform(-45.0F);
drawing.DrawString(text, font, textBrush, new RectangleF(0, 0, 500, 500));
drawing.Save();
}
...
var node = nodeList.Single(n => n.Name.Contains("Watermark");
((Shape)node.FirstParagraph.FirstChild).ImageData.SetImage(nodeImage);
Is the call to SetImage() creating a temporary file which is deleted upon reboot?