C # Eliminate the Sawtooth when generating the verification code image,

Source: Internet
Author: User

C # Eliminate the Sawtooth when generating the verification code image,
Introduction

A mobile phone number can be converted to an image based on the generated image. The content is also very simple. You can use your mobile phone number to generate a png image. It is to make the background transparent so that it can be called elsewhere. The Code g. TextRenderingHint = TextRenderingHint. AntiAlias;

 

Generating Images

1. serticate

2. Non-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; // eliminate the Sawtooth // generate the Random generator random Random = new Random (); // clear the background color of the image // g. clear (Color. transparent); // specifies the background noise line of the image./* 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); // foreground noise of the image. // 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 image // 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

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.