string imgdate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Ticks.ToString();
//上傳圖片並生成縮略圖150*150並添加文字水印
if (FUImg.PostedFile.FileName != string.Empty)
{
string imgtype = FUImg.PostedFile.FileName.Substring(FUUserImg.PostedFile.FileName.LastIndexOf("."));
string imgname = imgdate + imgtype;
FUImg.PostedFile.SaveAs(Server.MapPath("../*****") + @"\" + imgname);
System.Drawing.Image image, aNewImage;
image = System.Drawing.Image.FromStream(FUImg.PostedFile.InputStream);
decimal width = image.Width;
decimal height = image.Height;
int newwidth, newheight;
if (width > height)
{
newwidth = 150;
newheight = (int)(height / width * 150);
}
else
{
newheight = 150;
newwidth = (int)(width / height * 150);
}
aNewImage = image.GetThumbnailImage(newwidth, newheight, null, IntPtr.Zero);
Bitmap output = new Bitmap(aNewImage);
Graphics g = Graphics.FromImage(output);
g.DrawString("水印文字", new Font("CourIEr New", 14), new SolidBrush(Color.Red), 60, 60);//寫版權信息及文本格式及位置
output.Save(Server.MapPath("../*****") + @"\s_" + imgname, System.Drawing.Imaging.ImageFormat.Jpeg);
}