将字符串写入图片

将字符串写入到图片中.

1 换行采用指定写入位置来实现

2 务必释放资源,否则会出现Win32Error 

 

 protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                
//前一页面传递过来的参数
                string email = Request.QueryString.Get("email");
                
string realName = new OLUserService.OLUserService().GetRealNameByEmail(email);
                
string workid = new OLUserService.OLUserService().GetWorkIdByEmail(email);

                
//image1图片控件-呈现写入字符串后的图片
                string fileName = this.image1.ImageUrl;
                
int index = fileName.LastIndexOf(".");

                
string newFileName = fileName.Substring(0, index) + "_" + realName + fileName.Substring(index);
                FileInfo fileInfo 
= new FileInfo(Server.MapPath(fileName));
                fileInfo.CopyTo(Server.MapPath(newFileName), 
true);

                System.Drawing.Image image 
= new Bitmap(Server.MapPath(newFileName));
                Graphics g 
= Graphics.FromImage(image);
                
                
string s2 = string.Format("通过了关于《集团商业行为准则》的{0}", DateTime.Now.ToString("yyyy"));
                
string s3 = "年度认证,特此证明。";

                Font drawFont 
= new Font("Arial"12);
                SolidBrush drawBrush 
= new SolidBrush(Color.Black);
                
                
//通过指定写入位置实现换行功能
                drawPoint = new PointF(50200);
                g.DrawString(s2, drawFont, drawBrush, drawPoint);

                drawPoint 
= new PointF(50230);
                g.DrawString(s3, drawFont, drawBrush, drawPoint);
               

                System.Drawing.Image img 
= new Bitmap(image);
                
//此处尤为重要,使用资源后的释放.否则会出现Win32错误

                
//[ExternalException (0x80004005): A generic error occurred in GDI+.]

                g.Dispose();
                image.Dispose();

                img.Save(Server.MapPath(newFileName), System.Drawing.Imaging.ImageFormat.Jpeg);
                
this.image1.ImageUrl = newFileName;
            }
        }

 

 

 

 

 

转载于:https://www.cnblogs.com/zwwon/archive/2009/11/11/1600789.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值