C#.Net 上传图片,限制图片大小,检查类型完整版 在网上看到好多资料有几个问题看不明白, 请高手解答。

2025-03-02 04:21:22
推荐回答(3个)
回答1:

看看以下资料:
//检查上传文件的格式是否有效
if(this.FileUpload1.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
{
Response.Write("上传图片格式无效!");
return;
}
//生成原图
Byte[] oFileByte = new byte[this.FileUpload1.PostedFile.ContentLength];
System.IO.Stream oStream = this.FileUpload1.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度
int tWidth = 100; //设置缩略图初始宽度
int tHeight = 100; //设置缩略图初始高度
//按比例计算出缩略图的宽度和高度
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)));
}

回答2:

string virtualFilePath = "~//" + CAppConfiguration.Current.BbsUploadFilePath;
是获取上传的图片路径的
this.hfLastUploadImage.Value = filename;
是给控件赋值 路径
string root = CGlobals.GetHttpRoot(this);
获得类型

建议你用断点进去看看

回答3:

这都是人家自己写的方法,人家只是给你提供一种思路不是源码照搬的!