日期:2014-05-18 浏览次数:21123 次
protected void Button1_Click(object sender, EventArgs e)
{
// 根据选中状态初始化值
float pos_t_x = 390.0f;
float pos_t_y = 132.0f;
float pos_b_x = 980.0f;
float pos_b_y = 363.0f;
if (RadioButton2.Checked == true)
{
pos_t_x = 430;
pos_t_y = 125;
pos_b_x = 970;
pos_b_y = 380;
}
if (RadioButton3.Checked == true)
{
pos_t_x = 75;
pos_t_y = 143;
pos_b_x = 680;
pos_b_y = 380;
}
string Filename =string.Empty;
string file = Server.MapPath("") + "\\" + Label1.Text.Replace("/", "\\");
file = file.Replace("image", "card_bg");
file = file.Replace("_all", "");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(DateTime.Now.Year.ToString());
sb.Append(DateTime.Now.Month.ToString());
sb.Append(DateTime.Now.Day.ToString());
sb.Append(DateTime.Now.Hour.ToString());
sb.Append(DateTime.Now.Minute.ToString());
sb.Append(DateTime.Now.Second.ToString());
sb.Append(".jpg");
Filename = sb.ToString();
//生成缩略图
//原始图片名称
//生成高质量图片名称
string strFile = Server.MapPath("~") + "/card/card_" + Filename;
//从文件获取图片对象
//System.Drawing.Image image = System.Drawing.Image.FromStream(hpf.InputStream, true);
System.Drawing.Image image = System.Drawing.Image.FromFile(@file);
System.Drawing.Image bitmap = new System.Drawing.Bitmap(1024, 634); //新建bmp图片
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //新建画板
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //制定高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量、低速度呈现平滑程度
g.Clear(System.Drawing.Color.White); //清空画布
//在制定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.GraphicsUnit.Pixel);
int fontsize = 20; // 文字大小
// 抬头
//文字水印
System.Drawing.Graphics testGrahpics = System.Drawing.Graphics.FromImage(bitmap);
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
string fontpath = Server.MapPath("font/font.ttf");//相对于虚拟目录的字体文件路径
pfc.AddFontFile(fontpath);
System.Drawing.Font font = new System.Drawing.Font(pfc.Families[0], fontsize);
System.Drawing.Brush brush = new System.Drawing.SolidBrush(ColorTranslator.FromHtml("#E0DEDD"));
//分行
string sInput ="尊敬的" + TextBox3.Text.Trim().ToString() + DropDownList1.SelectedItem.Text.ToString() + ":"; //获取输入的水印文字
testGrahpics.DrawString(sInput, font, brush, pos_t_x, pos_t_y);
fontsize = 14;
// 写落尾---------------------------
testGrahpics = System.Drawing.Graphics.FromImage(bitmap);
font = new System.Drawing.Font(pfc.Families[0], fontsize);
sInput = TextBox4.Text.Trim().ToString() + "致"; //获取输入的水印文字
// 为了保证落尾右侧对其,需要重新计算其X初始坐标
pos_b_x = pos_b_x - (sInput.Length) *(5.0f + (float) fontsize);
testGrahpics.DrawString(sInput, font, brush, pos_b_x, pos_b_y);
testGrahpics.Dispose();
//保存缩略图c
try
{
bitmap.Save(strFile, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
Response.Write("保存缩略图失败" + ex.Message