无法使用Server.MapPath保存.jpg文件

时间:2016-05-11 09:00:38

标签: c# asp.net

protected void Page_Load(object sender, EventArgs e)
        {
            string Captcha = "354zxC";
            TextToImage(Captcha);
            Image1.ImageUrl = "~/Images/Captcha.jpg";
        }

       public void TextToImage(string text)
        {
            Bitmap Pic = new Bitmap(200, 150);
            Pic = MergeBitmap(Pic, CreatTextBitmap(text));
            Pic.Save(Server.MapPath("~/Images/Captcha.jpg"));
        }

       public Bitmap CreatTextBitmap(string Text)
        {
            Bitmap objBmpImage = new Bitmap(1, 1);
            int intWidth = 0;
            int intHeight = 0;
            Font objFont = new Font("Arial", 36, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
            Graphics objGraphics = Graphics.FromImage(objBmpImage);
            intWidth = (int)objGraphics.MeasureString(Text, objFont).Width;
            intHeight = (int)objGraphics.MeasureString(Text, objFont).Height;
            objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
            objGraphics = Graphics.FromImage(objBmpImage);
            objGraphics.Clear(Color.White);
            objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            objGraphics.DrawString(Text, objFont, new SolidBrush(Color.FromArgb(150, 200, 102)), 0, 0);
            objGraphics.Flush();
            return (objBmpImage);
        }
      public Bitmap MergeBitmap(Bitmap main,Bitmap text)
        {
            int x = 0; int y = 0; int w = 0; int h = 0; 
            w=text.Width;
            h=text.Height;
            for (int i=0;i<w;i++)
            {
                for (int j=0;j<h;j++)
                {
                    main.SetPixel(i, j, text.GetPixel(x, y));
                    y++;
                }
                y = 0;
                x++;
            }            
            return main;
        }

我想使用上述功能创建“图像安全码”。代码在本地机器上完美运行,但是当我上传图像时,它会给我一个 GDI错误 ,我的意思是它不会保存 .jpg < / em>文件。似乎是代码行
Pic.Save(Server.MapPath("~/Images/Captcha.jpg")); 没有在Web服务器上工作,请帮我纠正这个错误。

0 个答案:

没有答案