asp.net 图片 上传 打水印 高质量缩略图

数据库:proimgs/abc1.jpg||||

string[] arra = dr["picture"].ToString().Split('|');//取出图片 

string strName = arr[0].Substring(8);   "proimgs/"从这八个字符起  输出:abc1.jpg

string newname = strName.Substring(0, strName.LastIndexOf("."));   输出:abc1

string Pimg = arra[0].Substring(0, arra[0].LastIndexOf(".")) + "P" + arra[0].Substring(arra[0].LastIndexOf("."));   输出:proimgs/abc1P.jpg

使用FileUpload控件在获取文件名的时候,尽量使用Path.GetFileName(UpLoadFile.PostedFile.FileName)这样的方法可以使任何浏览器下面都能得到正确的文件名,而不会包含路径.

string picture = "";
string strUpath = Server.MapPath("~/proimgs/");
if (this.FileUpload1.FileName.ToString() != "")
{
string strName1 = this.FileUpload1.FileName.ToString();  //上传的文件名
int strSize1 = Convert.ToInt32(this.FileUpload1.PostedFile.ContentLength.ToString()); //上传文件大小 string strType1 = this.FileUpload1.PostedFile.ContentType.ToString();//上传文件类型 if (strType1 == "image/pjpeg" || strType1 == "image/bmp" || strType1 == "image/gif") //判断是否为图片类型 { if (strSize1 < 500000) { this.FileUpload1.SaveAs(strUpath + proFname + strName1.Substring(strName1.LastIndexOf(".")));//保存文件到服务器里 pp.imgCopy(strUpath, proFname, strName1.Substring(strName1.LastIndexOf(".")), 1);//图片打水印 pp.imgSmoClass(strUpath, proFname, strName1.Substring(strName1.LastIndexOf(".")));//分类缩略图 pp.imgSmoPro(strUpath, proFname, strName1.Substring(strName1.LastIndexOf(".")), 1);//产品缩略图 } else { Response.Write("<javascript langueage='javascript'>alert('上传文件过大!');history.go(-1);</script>"); } } else { Response.Write("<javascript langueage='javascript'>alert('上传文件类型不为图片!');history.go(-1);</script>"); } picture += "proimgs/" + proFname + "1" + strName1.Substring(strName1.LastIndexOf(".")) + "|"; } else { picture += "|"; } if (this.FileUpload2.FileName.ToString() != "") { string strName2 = this.FileUpload2.FileName.ToString(); //上传的文件名 int strSize2 = Convert.ToInt32(this.FileUpload2.PostedFile.ContentLength.ToString()); //上传文件大小 string strType2 = this.FileUpload2.PostedFile.ContentType.ToString();//上传文件类型 if (strType2 == "image/pjpeg" || strType2 == "image/bmp" || strType2 == "image/gif") //判断是否为图片类型 { if (strSize2 < 500000) { this.FileUpload2.SaveAs(strUpath + proFname + strName2.Substring(strName2.LastIndexOf(".")));//保存文件到服务器里 pp.imgCopy(strUpath, proFname, strName2.Substring(strName2.LastIndexOf(".")), 2); pp.imgSmoPro(strUpath, proFname, strName2.Substring(strName2.LastIndexOf(".")), 2);//缩略图 } else { Response.Write("<javascript langueage='javascript'>alert('上传文件过大!');history.go(-1);</script>"); } } else { Response.Write("<javascript langueage='javascript'>alert('上传文件类型不为图片!');history.go(-1);</script>"); } picture += "proimgs/" + proFname + "2" + strName2.Substring(strName2.LastIndexOf(".")) + "|"; } else { picture += "|"; }

 

//图片加水印,strPath:路径;strName:文件名;strType文件类型,i第几图片
public static void imgCopy(string strPath,string strName,string strType,int i)
{
try
{
//加图片水印
string path = strPath + strName + strType;
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/images/slogo.png"));// + "/slogo.png");
Graphics g = Graphics.FromImage(image);
g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g.Dispose();
//保存加水印过后的图片,删除原始图片
string newPath = strPath + strName + i + strType; //+ extension;//
image.Save(newPath);
image.Dispose();
//string strDelpath = strPath + strName;
if (File.Exists(path))//删除图片
{
File.Delete(path);
}
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}


//缩略图
http://www.cnblogs.com/zengxiangzhan/archive/2009/09/17/1568535.html

posted @ 2009-09-17 15:51  曾祥展  阅读(1081)  评论(0编辑  收藏  举报