0

In this example I use True Type Font (ttf) file. When i use DrawString method of the Graphics object it locks the file and it says: The action can't be completed because the file is opened in iisexpress. How to unlock the file after drawing ?

Code:

                using (Font a = new Font(pfc.Families[0], points))
                {
                    using (Bitmap codeBitmap = new Bitmap(1, 1, PixelFormat.Format24bppRgb))
                    {
                        using (Graphics g = Graphics.FromImage(codeBitmap))
                        {
                            size = g.MeasureString(code, a);
                        }
                    }

                    using (Bitmap codeBitmap = new Bitmap((int)size.Width, (int)size.Height, PixelFormat.Format24bppRgb))
                    {
                        using (Graphics g = Graphics.FromImage(codeBitmap))
                        {
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                            g.Clear(Color.White);
                            g.DrawString(code, a, Brushes.Black, 0, 0); //It locks the file here
                            codeBitmap.Save(response.OutputStream, ImageFormat.Gif);
                        }
                    }
                }
theChampion
  • 4,207
  • 7
  • 28
  • 35
  • See http://stackoverflow.com/questions/26671026/how-to-delete-the-file-of-a-privatefontcollection-addfontfile - apparently you need to P/Invoke to release the font – C.Evenhuis Dec 11 '14 at 07:51
  • I am using gdi32.dll with extern like in the example of the first link. – theChampion Dec 11 '14 at 07:52

0 Answers0