生成图片,保存到指定目录

代码
        /// <summary>
        
/// 将指定的联系号码转换成图片
         
/// </summary>
        
/// <param name="phone">联系号码</param>
        
/// <param name="fileSaveDir">生成图片的保存路径</param>
        
/// <returns>返回生成的图片的文件名</returns>
        public static string ConvertPhoneToImageAndSave(string phone, string fileSaveDir)
        {
            Bitmap image 
= new System.Drawing.Bitmap((int)Math.Ceiling(15 * 0.76 * phone.Length), 25);
            Graphics g 
= System.Drawing.Graphics.FromImage(image);
            
try
            {
                g.Clear(Color.White);
                Font f 
= new Font("Arial"15, FontStyle.Bold);
                SolidBrush brush 
= new SolidBrush(ColorTranslator.FromHtml("#0A3B7E"));
                g.DrawString(phone, f, brush, 
12);
                Guid guid 
= Guid.NewGuid();
                
string fileName = guid.ToString() + ".gif";
                DirectoryInfo dir 
= new DirectoryInfo(fileSaveDir);
                
if (!dir.Exists)
                {
                    dir.Create();
                }
                
string fileFullName = dir.FullName + fileName;
                image.Save(fileFullName);
                
return fileName;
            }
            
catch
            {
                
return "";
            }
            
finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

posted @ 2010-06-16 22:46  流浪者的飘  阅读(438)  评论(0编辑  收藏  举报