未发布的资源:非托管对象

时间:2014-01-28 07:03:17

标签: c# .net

CaptchaHandler.ashx中的函数ProcessRequest()无法正确处理 Font()和SolidBrush()

分配的非托管系统资源
    oGraphics.DrawString
            (
                //Text
            sCaptchaText.Substring(i, 1),
                //Random Font Name and Style
            new Font(aFontNames[oRandom.Next(aFontNames.Length - 1)],
               aFontEmSizes[oRandom.Next(aFontEmSizes.Length - 1)],
               aFontStyles[oRandom.Next(aFontStyles.Length - 1)]),
                //Random Color (Darker colors RGB 0 to 100)
            new SolidBrush(Color.FromArgb(oRandom.Next(0, 100),
               oRandom.Next(0, 100), oRandom.Next(0, 100))),
            x,
            oRandom.Next(10, 40)
            );
            oGraphics.ResetTransform();

帮助我如何通过Font()和SolidBrush()

来释放内存

1 个答案:

答案 0 :(得分:0)

图形功能的正确使用必须如下所示:

byte[] bytes = null;
using (System.Drawing.Image image = new Bitmap(w, h))
{
    using (Graphics g = Graphics.FromImage(image))
    {
        g.DrawImage(originalImage, 0, 0, w, h);
        using (MemoryStream ms = new MemoryStream())
        {
            image.Save(ms, ImageFormat.Png);
            bytes = ms.ToArray();
        }
    }
}
return bytes;

确保使用using关键字包装对象。