谁有图片上传代码/加注释给份

zhangjingcheng 2007-08-30 10:58:35
用来研究
源码谢谢先
...全文
588 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
symbol441 2007-08-30
  • 打赏
  • 举报
回复
mark
jiangmin168168 2007-08-30
  • 打赏
  • 举报
回复
///<summary>
///图片处理(另存一个小图片,并设定图片的最大宽度,最大高度,进行等比缩放)
/// by fycooer
/// 2007-01-20
///</summary>
///<return></return>
private bool DealImage(string Imagepath, string Imagesmallpath, string Extension,int iWidth,int iHeight)
{
int iWidthTemp=iWidth;
int iHeightTemp=iHeight;
//by minjiang 07-08-24
double dHeight=(double )iHeight ;
double dWidth=(double )iWidth ;
try
{
System.Drawing.Image myimage = System.Drawing.Image.FromFile(Imagepath);

//int iHeight;
//int iWidth = 180;
//如果上传的图片的宽大于设置的宽或者图片的高大于设置的高
if (myimage.Width > iWidth || myimage.Height > iHeight)
{
if (((double)myimage.Width) / (double)iWidth > ((double)myimage.Height) / (double)iHeight)
{
// iHeight = Convert.ToInt16(((double)(iWidth / myimage.Width) * myimage.Height));
//by minjiang 07-08-08 修改

dHeight = (double)iWidth / (double)myimage.Width;
dHeight = dHeight * myimage.Height;
iHeight = Convert.ToInt32(dHeight);
}
else
{
//iWidth = Convert.ToInt16(((double)iHeight / myimage.Height * myimage.Width));
//iWidth = myimage.Width;
//iHeight = myimage.Height;
//by minjiang 07-08-08 修改

dWidth = (double)iHeight / (double)myimage.Height;
dWidth = dWidth * myimage.Width;
iWidth = Convert.ToInt32(dWidth);
}
if (iWidth > iWidthTemp)
{
iWidth = iWidthTemp;
}
if (iHeight > iHeightTemp)
{
iHeight = iHeightTemp;

}


}
else
{
//如果图片的宽或者是高都没有大于设定的值,则不进行任何缩放
//by minjiang 07-08-24 解决图片的缩略图片大小问题
iWidth = myimage .Width ;
iHeight = myimage .Height ;

}


System.Drawing.Bitmap smallimage = new Bitmap(myimage, iWidth, iHeight);
switch (Extension)
{
case ".gif":
smallimage.Save(Imagesmallpath, System.Drawing.Imaging.ImageFormat.Gif);
break;
default:
smallimage.Save(Imagesmallpath, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
smallimage.Dispose();
myimage.Dispose();
return true;
}
catch
{
return false;
}
}
jiangmin168168 2007-08-30
  • 打赏
  • 举报
回复
/// <summary>
/// 检查图片的保存路径是否存在
/// </summary>
/// <returns></returns>
public string CheckProductPictureML(string sSavePath)
{
string strml=this.Server.MapPath(sSavePath);


if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}

#region maybe Use

/*****************************************************************

//年
strml+="\\"+DateTime.Now.Year.ToString();
if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}
//月
strml+="\\"+DateTime.Now.Month.ToString();
if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}
//日
strml+="\\"+DateTime.Now.Day.ToString();
if(Directory.Exists(strml)==false)
{
Directory.CreateDirectory(strml);
}
*****************************************************************/
#endregion

return strml;
}
jiangmin168168 2007-08-30
  • 打赏
  • 举报
回复
/// <summary>
/// 上传多个图片
/// by minjiang 07-08-01
/// </summary>
/// <param name="sSavePath">原图路径</param>
/// <param name="sSaveSmallPath">小图路径</param>
/// <param name="preFileName"></param>
/// <param name="iMaxSize">大小</param>
/// <param name="iWidth">宽</param>
/// <param name="iHeight">高</param>
/// <param name="intSucess">统计有多少个图片已经成功上传</param>
/// <param name="arrFileNames">保存原图名称数组</param>
/// <param name="arrSmallFileNames">保存小图名称数组</param>
/// <returns></returns>

public string[] upPicture(string sSavePath, string sSaveSmallPath, string preFileName, int iMaxSize, int iWidth, int iHeight,int intSucess, out ArrayList arrFileNames, out ArrayList arrSmallFileNames)
{
arrFileNames = new ArrayList();
arrSmallFileNames = new ArrayList();
HttpFileCollection files = HttpContext.Current.Request.Files;
string strfileoldpath; //上传原图片的保存路径
string strfileoldname;//图片保存后名称
string strfilesmallpath; //小图片的保存路径
string strfilesmallname;//小图片保存后名称

string fileName, fileExtension;
string[] arrayImageUrl = new string[4] { "", "", "", "" };
int iSucess = 0;//统计有多少个图片已经成功上传
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
//没有文件上传
if (postedFile.FileName == "")
{
arrayImageUrl[2] = "errorNoFile";
continue;

}


fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName == "")
{
arrayImageUrl[2] = "errorNoFile";
continue;

}

fileExtension = System.IO.Path.GetExtension(fileName);
//判断上传文件类型
string filetype = postedFile.ContentType;
//判断是否图片
if ((filetype.IndexOf("image") == -1) || (fileExtension.Trim().ToLower() != ".jpeg") && (fileExtension.Trim().ToLower() != ".jpg") && (fileExtension.Trim().ToLower() != ".gif"))
{
arrayImageUrl[2] = "errorT";
continue;
}

//判断是否图片大小超过上限
int MaxFile = 0;
MaxFile = postedFile.ContentLength;
if (MaxFile > (iMaxSize * 1024))
{
arrayImageUrl[2] = "errorb";
continue;
}

//检查保存文件的目录是否存在,否则创建
strfileoldpath = CheckProductPictureML(sSavePath);
strfilesmallpath = CheckProductPictureML(sSaveSmallPath);

//为文件命名,然后保存
Random ra = new Random();

strfileoldname = preFileName + "_" + ra.Next(100000, 999999).ToString() + fileExtension;
strfilesmallname = "s_" + strfileoldname;

strfileoldpath += "\\" + strfileoldname;
strfilesmallpath += "\\" + strfilesmallname;
//保存原始图像
postedFile.SaveAs(strfileoldpath);

//把上传的图片另存一个小图
if (this.DealImage(strfileoldpath, strfilesmallpath, fileExtension, iWidth, iHeight))
{
arrayImageUrl[0] = sSavePath + "/" + strfileoldname;
arrayImageUrl[1] = sSaveSmallPath + "/" + strfilesmallname;
arrFileNames.Add(arrayImageUrl[0].ToString());
arrSmallFileNames.Add(arrayImageUrl[1].ToString());
iSucess += 1;

}
else
{
arrayImageUrl[2] = "error";
continue;
}




}
//统计已经成功上传的图片数,如果大于等于1则说明上传成功
if (iSucess >= intSucess)
{ arrayImageUrl[2] = ""; }
else
{ arrayImageUrl[2] = "error"; }


}
catch
{
arrayImageUrl[2] = "error";
return arrayImageUrl;
}
return arrayImageUrl;
}
tantaiyizu 2007-08-30
  • 打赏
  • 举报
回复
SaveAs 就可以了



还想要什么?
活靶子哥哥 2007-08-30
  • 打赏
  • 举报
回复
see:

http://www.aspxboy.com/private/218/default.aspx
paual779 2007-08-30
  • 打赏
  • 举报
回复
//定义一个变量,取全部文件名
string fileFullname=this.File1.PostedFile.FileName;
//取出当前日期
string dataName= DateTime.Now.ToString("yyyyMMddhhmmss");
//取文件扩展名
string fileName=fileFullname.Substring(fileFullname.LastIndexOf("\\")+1);
//截取上传文件的扩展名既"."后面的字母所以+1
string type=fileFullname.Substring(fileFullname.LastIndexOf(".")+1);
//类型控制,想上传什么类型,可以在这里类似添加
if(type=="jpg"||type=="gif"||type=="bmp")
{
this.File1.PostedFile.SaveAs(Server.MapPath("pic")+"\\"+dataName+fileName);
Response.Write("<script language='javascript'>alert('上传成功!');</script>");
//image1输出上传图片,可以在这里控制图片大小
this.Image1.ImageUrl="pic"+"\\"+dataName+fileName;
}
else
{
Response.Write("<script language='javascript'>alert('你选的文件类型不符!');</script>");
}
zhangjingcheng 2007-08-30
  • 打赏
  • 举报
回复
有没有简单点的太复杂了

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧