将文字写入图片

/** <summary>
  /// 功能:将文字写入图片
  /// 日期:2006-11-20
  /// 作者:杨义贤
  /// </summary>
  /// <param name="strfilename">文件名字</param>
  /// <param name="strtext">需要写入的文字</param>
  private void texttoimage(string strfilename,string strtext)
  {
      if(!file.exists(mappath(strfilename)))
      {
    throw new filenotfoundexception("文件不存在!");

和 "asp.net 中将文字写入图片中" 有关的编程小帖士:

strong>DataFormats.CF_OEMTEXT

指明标准的Windows OEM文本格式。

语法

public static final String CF_OEMTEXT;

说明

在WFC中,你可以在字符串对象中使用它。WFC替你自动执行向Win32 OEM文本剪贴板格式的转换。

      }
    
      if( strtext == string.empty )
      {
    return;
      }
      //还需要判断文件类型是否为图像类型
      system.drawing.image image = system.drawing.image.fromfile(mappath(strfilename));
      bitmap bitmap = new bitmap(image,image.width,image.height);
      graphics grap = graphics.fromimage(bitmap);
      float fontsize = 24.0f; //字体大小
      float textwidth = strtext.length*fontsize;  //文本的长度
      //下面定义一个矩形区域,以后在这个矩形里画上白底黑字
      float rectx = 0;
      float recty = 0;
      float rectwidth = strtext.length*(fontsize+12);
      float rectheight = fontsize+8;
      string strfont;//定义字体类型
      //声明矩形域
      rectanglef textarea = new rectanglef(rectx,recty,rectwidth,rectheight);
      strfont = ddlfont.selecteditem.text.tostring().trim();
      font font = new font(strfont,fontsize);   //定义字体
      brush whitebrush = new solidbrush(color.white);   //白笔刷,画文字用
      brush blackbrush = new solidbrush(color.transparent);   //黑笔刷,画背景用

      grap.fillrectangle(blackbrush,rectx,recty,rectwidth,rectheight);    

      grap.drawstring(strtext,font,whitebrush,textarea);
      memorystream ms = new memorystream();
      //保存为jpg类型
      bitmap.save(ms,imageformat.jpeg);

      //输出处理后的图像.//在这里可以将生成后的图片放大oracle数据库power中
     
      response.clear();
      response.contenttype = "image/jpeg";
      response.binarywrite(ms.toarray());
      grap.dispose();
      bitmap.dispose();
      image.dispose();
  }
事件:

private void btnwrite_click(object sender, system.eventargs e)
  {
      httppostedfile httppostfile;    //客户端上载文件对象
      httppostfile = docupload.postedfile;
      string strtemp;
      string strfilename;//完成文件名
      strtemp = httppostfile.filename.tostring().trim();
      strfilename = getrealfilename(strtemp);
      string strimg = "images/";
      texttoimage(strimg +strfilename,txtdocname.text.tostring());

  }/** <summary>
  ///  功能:获取文件名
  ///  日期:2006-11-20
  ///  作者:杨义贤
  /// </summary>
  /// <param name="strpathname">文件路径</param>
  /// <returns></returns>
  public string getrealfilename(string strpathname)
  {
      string [] strfilename = strpathname.split('/');
      int ipos = strfilename.getupperbound(0);
      return strfilename[ipos];
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值