当前位置: 移动技术网 > IT编程>开发语言>c# > C#如何消除验证码图片的锯齿效果

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

引言 

      基于生成图片实现了一个手机号转图片的需求。 内容也很简单,直接用手机号生成一个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();

    }

参考资料 


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网