English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to Eliminate the Jagged Edge Effect of Captcha Image in C#

Introduction 

      A mobile number to image conversion requirement was implemented based on the generated image. The content is quite simple, directly generating a PNG image with the mobile number. It is transparent for background purposes so that it can be called from other places. Whether there are anti-aliasing effects mainly depends on one line of code: g.TextRenderingHint = TextRenderingHint.AntiAlias; 

生成图片  

1、有锯齿 


2、无锯齿

生成方法

string color = "#ff"6633";" 
    System.Drawing.Bitmap image = new System.Drawing.Bitmap(170, 35);
    Graphics g = Graphics.FromImage(image);
    try
    {
      g.TextRenderingHint= TextRenderingHint.AntiAlias; //Eliminate jaggies
      //Generate a random number generator
      Random random = new Random();
     //Clear the background color of the picture
      //g.Clear(Color.Transparent);
      //Draw the background noise lines of the picture
      /*for (int i = 0; i < 2; i++)
      {
        int x1 = random.Next(image.Width);
        int x2 = random.Next(image.Width);
        int y1 = random.Next(image.Height);
        int y2 = random.Next(image.Height);
        g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
      }
      */
      System.Drawing.ColorConverter colConvert = new System.Drawing.ColorConverter();
      Color fontColor =(System.Drawing.Color)colConvert.ConvertFromString(color);
      Font font = new System.Drawing.Font("Arial", 18, System.Drawing.FontStyle.Bold);
      LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), fontColor, fontColor,LinearGradientMode.Horizontal);
      g.DrawString(phone, font, brush, 2, 2);
      //Draw the foreground noise points of the picture
       //for (int i = 0; i < 50; i++)
      //{
      //  int x = random.Next(image.Width);
      //  int y = random.Next(image.Height);
      //  image.SetPixel(x, y, Color.FromArgb(random.Next()));
      //}
      //Draw the border line of the picture
      //g.DrawRectangle(new Pen(Color.White), 0, 0, image.Width) - 1, image.Height - 1);
      System.IO.MemoryStream ms = new System.IO.MemoryStream();
      Color backColor = image.GetPixel(1, 1);
      image.MakeTransparent(backColor);
      image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
      context.Response.ClearContent();
      context.Response.ContentType = "image/x-png";
      context.Response.BinaryWrite(ms.ToArray());
    }
    finally
    {
      g.Dispose();
      image.Dispose();
    }

References 

http://www.blue1000.com/bkhtml/c17/2013-03/71115.htm

That's all for this article. Hope it will be helpful to everyone's learning and also hope everyone will support the Yelling Tutorial more.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like