首页 > 编程 > .NET > 正文

Asp.net 中将文字写入图片中

2024-07-10 13:05:45
字体:
来源:转载
供稿:网友


/**//// <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("文件不存在!");
            }
          
            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];
        }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表