public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
{
string sThumbFile = "";
string sFilename = "";
sSavePath = "UpLoad/";
sThumbExtension = "_thumb";
if (upImage.PostedFile != null)
{
HttpPostedFile myFile = upImage.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
{
return "没有选择上传图片";
} string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower(); if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp")
{
return "图片格式不正确";
} byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
sFilename = System.IO.Path.GetFileName(myFile.FileName);
int file_append = 0;
while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
{
file_append++;
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ file_append.ToString() + extendName;//".jpg";
}
System.IO.FileStream newFile
= new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),
System.IO.FileMode.Create, System.IO.FileAccess.Write);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
System.Drawing.Image.GetThumbnailImageAbort myCallBack =
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Bitmap myBitmap;
try
{
myBitmap = new System.Drawing.Bitmap(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
if(intThumbWidth==0)
intThumbWidth = myBitmap.Width;
if(intThumbHeight==0)
intThumbHeight = myBitmap.Height;
file_append = 0;
sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ sThumbExtension + extendName;//".jpg";
while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
{
file_append++;
sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
file_append.ToString() + sThumbExtension + extendName;// ".jpg";
}
System.Drawing.Image myThumbnail
= myBitmap.GetThumbnailImage(intThumbWidth,
intThumbHeight, myCallBack, IntPtr.Zero);
myThumbnail.Save(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile));
myThumbnail.Dispose();
myBitmap.Dispose();
}
catch
{
System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
return "图片格式不正确";
}
return sThumbFile;
}
return "没有选择图片";
}
public bool ThumbnailCallback()
{
return false;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货