辞职两个月,网站终于上线,最后一点问题请教
public static string UploadImage(HttpPostedFileBase UploadFile, int userid, int tWidth = 400, int tHeight = 400, string path = "/content/userhead/")
{
string result = "";
System.IO.Stream oStream = null;
System.Drawing.Image oImage = null;
using (oStream = UploadFile.InputStream)
{
using (System.Drawing.Image img11 = System.Drawing.Image.FromStream(oStream))
{
oImage = new System.Drawing.Bitmap(img11);
img11.Dispose();
oStream.Dispose();
int oWidth = oImage.Width;
int oHeight = oImage.Height;
if (oWidth < tWidth && oHeight < tHeight)
{
return result;
}
if (oWidth >= oHeight)
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
else
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
Bitmap tImage = new Bitmap(tWidth, tHeight);//70x70
using (Graphics g = Graphics.FromImage(tImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
g.Clear(Color.Transparent); //清空画布并以透明背景色填充
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
string file = userid + ".jpg";
string oFullName = System.Web.HttpContext.Current.Server.MapPath(path) + file;//保存小图的物理路径
result = file;
try
{
oImage.Dispose();
oStream.Dispose();
g.Dispose();
tImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
oImage.Dispose();
oStream.Dispose();
g.Dispose();
tImage.Dispose();
}
return path + result;
}
}
}
网站有个功能上传图片并压缩的,用这段代码又会有问题,多上传几次就会提示图片被锁定无法操作,有人知道怎么解决吗?
另外希望大家对我的网站发展给点的建议,www.84qy.net
[最优解释]
并发?图片正在使用中?
[其他解释]
int userid此参数不知传进来的是何值,假如为一固定值则:string file = userid + ".jpg";此句则产生文件名相同。多次上传会出现错误情况。
[其他解释]
该回复于2012-11-27 16:18:12被管理员删除
[其他解释]
你这个网站买多少银?
[其他解释]
打开文件的方法 改成类似以下代码:
FileStream fs = new FileStream("C:\\WINNT\\Web\\Wallpaper\\Fly Away.jpg", FileMode.Open, FileAccess.Read);
pictureBox1.Image = System.Drawing.Image.FromStream(fs);
fs.Close();
[其他解释]
tImage.Save的时候做一下文件是否存在的判断
[其他解释]