好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#如何消除验证码图片的锯齿效果

引言

      基于生成图片实现了一个手机号转图片的需求。 内容也很简单,直接用手机号生成一个png图片。就是为了背景透明以便其他地方调用。 有无锯齿主要依靠一句代码:g.textrenderinghint= textrenderinghint.antialias; 

生成图片  

1、有锯齿 

2、无锯齿

生成方法

?

string color = "#ff6633" ;

 

     system.drawing.bitmap image = new system.drawing.bitmap(170, 35);

     graphics g = graphics.fromimage(image);

     try

     {

       g.textrenderinghint= textrenderinghint.antialias; //消除锯齿

      

 

       //生成随机生成器

       random random = new random();

      //清空图片背景色

       //g.clear(color.transparent);

       //画图片的背景噪音线

 

       /*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);

       //画图片的前景噪音点

        //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()));

 

       //}

 

 

 

       //画图片的边框线

 

       //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();

 

     }

参考资料 

http://HdhCmsTestblue1000测试数据/bkhtml/c17/2013-03/71115.htm

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://HdhCmsTestcnblogs测试数据/sword-successful/archive/2016/09/18/5880866.html

dy("nrwz");

查看更多关于C#如何消除验证码图片的锯齿效果的详细内容...

  阅读:47次