bool fileOK = false;
string path = Server.MapPath("~/Upload")+"\\"; //上传路径
if (fupImgUrl.HasFile)
![]()
{
string fileExtension = System.IO.Path.GetExtension(fupImgUrl.FileName).ToLower(); //获取要上传文件的扩展名
![]()
![]()
string[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
![]()
{
if (fileExtension == allowedExtensions[i])
![]()
{
fileOK = true;
}
}
}
![]()
string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +DateTime.Now.Hour.ToString()+ DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + System.IO.Path.GetExtension(fupImgUrl.FileName).ToLower();
//生成随机的文件名
if (fileOK)
![]()
{
try
![]()
{
fupImgUrl.PostedFile.SaveAs(path + fileName); //上传原图
System.Drawing.Image image, sImage;
image = System.Drawing.Image.FromStream(fupImgUrl.PostedFile.InputStream);
int width = image.Width;
int height = image.Height;
int NewWidth, NewHeight;
if (width > height)
![]()
{
NewWidth = 150;
NewHeight = NewWidth*height / width;
}
else
![]()
{
NewHeight = 150;
NewWidth = NewHeight*width / height;
}
sImage = image.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
int x, y;
x = sImage.Width / 2 - 30;
y = sImage.Height - 20;
System.Drawing.Bitmap output = new System.Drawing.Bitmap(sImage);
System.Drawing.Graphics g= System.Drawing.Graphics.FromImage(output);
System.Drawing.Font Fonts = new System.Drawing.Font("Courier New", 9);
g.DrawString("水印内容。。。。", Fonts, new System.Drawing.SolidBrush(System.Drawing.Color.Red), x, y);
output.Save(path + "s\\" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
![]()
Response.Write("<script>alert('上传文件成功')</script>");
lbFileName.Text ="~/upload/"+fileName;
lbImages.Text = "~/upload/s/" + fileName;
}
catch (Exception ex)
![]()
{
Response.Write("<script>alert('"+ex.Message+"')</script>");
}
}
else
![]()
{
Response.Write("<script>alert('无效的文件类型')</script>");
}