public string GetRandom()
{
Random r=new Random();
return r.Next(10000).ToString();
}
private void upImge()
{
string fileName=Path.GetFileName(File1.PostedFile.FileName);
string txt=fileName.Substring(fileName.LastIndexOf(".")+1);
if(fileName!="")
{
string time=DateTime.Now.Month.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString();
string newFileName=time+this.GetRandom()+"."+txt;
string filepath = Server.MapPath("upPic/") + newFileName;
if (File.Exists(filepath))
{
js.showAlert("该文件名已存在!");
}
else
{
if(File1.PostedFile != null)
{
File1.PostedFile.SaveAs(filepath);
System.Drawing.Image image = System.Drawing.Image.FromFile(filepath);
double h=Convert.ToDouble(image.Height.ToString());
double w=Convert.ToDouble(image.Width.ToString());
double f=h/w;
if(h>w)
{
if(h>150)
{
h=150;
w=150*(1/f);
}
else
{
h=150;
}
}
else if(w>h)
{
if(w>150)
{
w=150;
h=150*f;
}
else
{
w=150;
}
}
else
{
h=150;
w=150;
}
image=image.GetThumbnailImage(Convert.ToInt32(w),Convert.ToInt32(h),null,new System.IntPtr());
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f11 = new Font("Verdana", 5);
Brush b = new SolidBrush(Color.White);
g.DrawString("传媒中国", f11, b, 10, 10);
g.Dispose();
string newPath = Server.MapPath("upPic/") + "_new" +newFileName;
image.Save(newPath);
image.Dispose();
js.showAlert("上传成功");
}
else
{
js.showAlert("图片为空!");
}
}
}
else
{
js.showAlert("请选择一个文件!");
}
}