首页 > 编程 > .NET > 正文

ASP.net生成文字图片

2024-07-10 12:56:37
字体:
来源:转载
供稿:网友
string _fontname = request["fontname"].tostring();
   int _fontsize = convert.toint16(request["fontsize"]);
   string _showname = request["str"].tostring();
   
   
   bitmap objbitmap = null;
   graphics g = null ;
   
   font stringfont = new font(_fontname, _fontsize, fontstyle.bold );
   stringformat stringformat = new stringformat();
   stringformat.formatflags = stringformatflags.nowrap;


   try
   {
    objbitmap = new bitmap(1,1);
    g = graphics.fromimage(objbitmap);
    sizef stringsize = g.measurestring(_showname, stringfont);
    int nwidth = (int)stringsize.width;
    int nheight = (int)stringsize.height;
    g.dispose();
    objbitmap.dispose();

    objbitmap = new bitmap(nwidth,nheight);
    g = graphics.fromimage(objbitmap);
    g.fillrectangle(new solidbrush(color.yellow), new rectangle(0,0,nwidth,nheight));
    g.textrenderinghint = textrenderinghint.antialias;
    g.drawstring(_showname, stringfont, new solidbrush(color.black), new pointf(0, 0), stringformat);  
    objbitmap.save(response.outputstream, imageformat.gif);
   }
   catch (exception ee)
   {
    response.write(ee.tostring());
   }
   finally
   {
    if (null != g) g.dispose();
    if (null != objbitmap) objbitmap.dispose();
    response.end();
   }
菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表