-1

我正在尝试将我从位图创建的图像保存在项目的给定路径中,但出现此错误:Additional information: A generic error occurred in GDI+.

这是代码:

        public void SaveCaptchaImage(string name)
        {
            Bitmap image = new Bitmap(128, 64);
            string path = "/content/images";

            if (System.IO.File.Exists(path + name))
                System.IO.File.Delete(path + name);

            using (Pen myPen = new Pen(Color.Red, 5))
            using (Brush myBrush = new SolidBrush(Color.Silver))
            using (Font myFont = new Font("Arial", 16))
            using (Graphics graphObject = Graphics.FromImage(image))
            {
                graphObject.FillRectangle(myBrush, new Rectangle(0, 0, 128, 64));
                graphObject.DrawString(GetRandomCaptcha(), myFont, myBrush, new Point(20, 20));
                image.Save(path + name + ".png", ImageFormat.Png);
            }
            image.Dispose();
        }

异常发生在这一行:

image.Save(path + name + ".png", ImageFormat.Png);

我能做些什么来解决这个问题?

编辑:我不明白为什么我被否决了,但是好的。

4

1 回答 1

0

用Server.MapPath包裹你的路径,如:

image.Save(HttpContext.Current.Server.MapPath(Path.Combine(path, name + ".png")), ImageFormat.Png);

获取绝对路径而不是相对路径。

并使用评论中建议的@stuartd 之类的Path.Combine方法。

于 2016-08-22T15:02:56.340 回答