生成图片,保存到指定目录
代码
/// <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, 1, 2);
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();
}
}
/// 将指定的联系号码转换成图片
/// </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, 1, 2);
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();
}
}