日期:2014-05-17 浏览次数:20869 次
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SAVEPNG.aspx.cs" Inherits="_SAVEPNG" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>生成宝宝年龄图</title> </head> <body> <form id="form1" runat="server"> <asp:Button ID="Button1" runat="server" Text="生成宝宝年龄图" onclick="Button1_Click" /> </form> </body> </html>
using System.Drawing; using System; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {} protected void Button1_Click(object sender, EventArgs e) { if (!Directory.Exists(Server.MapPath("img"))) Directory.CreateDirectory(Server.MapPath("img")); //img/test.png 是生成后的图片名称 string path1 = Server.MapPath("img/test.png"); //图片建立引用 images.png 是画布 System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("images.png")); // 获取水印图片地址 psb.png 是 水印图片(遮罩层) System.Drawing.Image syImage = System.Drawing.Image.FromFile(Server.MapPath("psb.png")); // 把水印绘制到 要添加水印的图片上 Graphics g = Graphics.FromImage(image); // 绘制水印的 位置坐标 g.DrawImage(syImage, new Rectangle(0, 0, syImage.Width, syImage.Height), 0, 0, syImage.Width, syImage.Height, GraphicsUnit.Pixel); //出生日期 string str = getAge("2010-3-17"); Bitmap images = new Bitmap(482, 250);//图像大小 //填充透明色 //g.Clear(Color.Transparent); //写文字 g.DrawString(str, new Font("宋体", 12), new SolidBrush(Color.FromArgb(255, 0, 138)), 195, 53); //Graphics 类还有很多绘图方法可以绘制 直线、曲线、圆等等 g.Dispose(); //保存 添加水印后的图片 "cs" 保存图片的文件夹 image.Save(path1, System.Drawing.Imaging.ImageFormat.Png); } #region 计算年龄 /// <summary> /// 计算年龄 /// </summary> /// <param name="birthDay">出生日期</param> /// <returns></returns> private string getAge(string bornDate) { DateTime b = DateTime.Parse(bornDate); int a2 = DateTime.Now.Year; int b2 = b.Year; int a3 = DateTime.Now.Month; int b3 = b.Month; int a4 = DateTime.Now.Day; int b4 = b.Day; int aa = (a2 - 1900) * 365 + a3 * 30 + a4; int bb = (b2 - 1900) * 365 + b3 * 30 + b4; int cc = aa - bb; //天数 int dd = (int)Math.Floor(Convert.ToDouble(cc / 365)); int days = cc - dd * 365; int ee = days / 30; int day = days - ee * 30; return "今天是" + DateTime.Now.ToString("D") + " 宝宝已经" + dd.ToString() + "岁" + ee.ToString() + "个月 零" + day + &