0
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;
        }

I want to create "Image Security Code" using above function. The code works perfectly on the local machine, but when I upload an image, it gives me a GDI Error , I mean it does not save the .jpg file. It seems that the code line
Pic.Save(Server.MapPath("~/Images/Captcha.jpg")); is not working on the web server, please help me to rectify this error.

Raktim Biswas
  • 3,833
  • 5
  • 22
  • 29
  • 1
    What is the Path resulting from `Server.MapPath()`? Do you have the privileges to write to that path? Also what is the exact error you get? "it give gdi error" is not a valid problem description. – derpirscher May 11 '16 at 09:04
  • make sure you have the *write access* to the directory in which you are saving. – Raktim Biswas May 11 '16 at 09:10

0 Answers0