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