I'm using below simple code to write on an image with certain font.
PrivateFontCollection FontCollection = new PrivateFontCollection();
FontCollection.AddFontFile(ToplistConfig.PrivateFontsPath + "/font.ttf");
FontFamily FontCollectionFamily = new FontFamily(FontCollection.Families[0].Name, FontCollection);
Font myFont = new Font(FontCollectionFamily, 10, FontStyle.Regular);
It usually works fine but every now and then it generates below problem and web site stops responding until you recycle app pool
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at System.Drawing.SafeNativeMethods.Gdip.GdipDrawString(HandleRef graphics, String textString, Int32 length, HandleRef font, GPRECTF& layoutRect, HandleRef stringFormat, HandleRef brush) at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format) at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, PointF point) at banner.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Anyone knows a workaround for this?
EDIT: I have put as many as possible try/catches and usings to dispose stuff but result was always the same.