How C # eliminates the sawtooth effect of a captcha picture _c# tutorial

Source: Internet
Author: User

Introduction

Based on the generation of images to achieve a mobile phone number of the need to turn pictures. Content is also very simple, directly using mobile phone number to generate a PNG image. is for the background to be transparent so that other places are invoked. There is no sawtooth mainly relies on a code: G. Textrenderinghint= Textrenderinghint.antialias;

Generate pictures

1, serrated


2, no Sawtooth

Generation method

String color = "#ff6633";
    System.Drawing.Bitmap image = new System.Drawing.Bitmap (170, 35);
    Graphics g = graphics.fromimage (image);  try {g.textrenderinghint= Textrenderinghint.antialias;//anti-aliasing//Generate random generator Random Random = new
     Random ();
      Clear the picture background color//g.clear (color.transparent); Picture background noise line/*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", 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); Picture the foreground noise point//for (int i = 0; i < i++)//{//int x = random. Next (image.
      Width); int y = random. Next (image.
      Height); Image. SetPixel (x, Y, Color.FromArgb) (random.

      Next ())); ///Draw the picture's border line//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 ();
 }

Resources

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

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.