C# 使用图形后的文件锁定
在本例中,我使用True Type字体ttf文件。当我使用图形对象的DrawString方法时,它会锁定文件,并显示:操作无法完成,因为文件是在iisexpress中打开的。 绘图后如何解锁文件 代码:C# 使用图形后的文件锁定,c#,graphics,gdi,C#,Graphics,Gdi,在本例中,我使用True Type字体ttf文件。当我使用图形对象的DrawString方法时,它会锁定文件,并显示:操作无法完成,因为文件是在iisexpress中打开的。 绘图后如何解锁文件 代码: 请参阅-显然,您需要P/Invoke来释放fontI,我正在使用gdi32.dll和extern,如第一个链接的示例中所示。 using (Font a = new Font(pfc.Families[0], points)) {
请参阅-显然,您需要P/Invoke来释放fontI,我正在使用gdi32.dll和extern,如第一个链接的示例中所示。
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);
}
}
}