将字符串写入图片

将字符串写入到图片中.

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( 50 200 );
                g.DrawString(s2, drawFont, drawBrush, drawPoint);

                drawPoint 
=   new  PointF( 50 230 );
                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

你可能感兴趣的:(将字符串写入图片)