社区
C#
帖子详情
谁有图片上传代码/加注释给份
zhangjingcheng
2007-08-30 10:58:35
用来研究
源码谢谢先
...全文
619
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
打赏
举报
回复
有没有简单点的太复杂了
iOS
图片上传
代码
加
注释
//分界线的标识符 NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x"; //根据url初始化request NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url
vue + vant实现
图片上传
加
水印
(注:以下的
代码
都来自于csdn,只是我把两部份
代码
合在一起来实现我的需求而已,在最后面,我也会附上原作者的接,大家都不容易)base64转file:【https://blog.csdn.net/qq_43886365/article/details/126729188】图片
加
水印 :【https://blog.csdn.net/King0217/article/details/121562949】
代码
就是这些,然而,在此中,我是把图片
加
水印和图片base64转file两个不同的博文混在一起写的。
C# MVC(File)控件多张
图片上传
加
预览
刚来公司实习,老板叫我写一个积分商城网站。用的是公司的框架结构搭建的后台,所以后台的
图片上传
不需要自己写。但是前台的评价图片就需要自己手写了,在网上找了很多
代码
发现都用不了。问了很多人也都没有实现! 最后公司同事给我了一个上传的
代码
,拿来借用。发现效果很好,困扰了我很多天的问题,也得于解决。现在我把他分享出来! 先看一下上传效果: 界面样式:(显示效果) ...
PHP
图片上传
代码
怎么写和
代码
的用发
很多做网站的小伙伴都会遇到有客户求助的现象,一般都是找我们删除网站的一些功能或者添
加
一些功能,前几天就遇到一个客户需要我给他的网站添
加
一个可以上传图片并且添
加
的图片可以在前台展示,那么功能是怎样实现的呢,下面小淘来教大家这样的功能是怎样写的。echo ‘当前文件保存路径为:’.$_FILES[‘file’][‘tmp_name’],$path.$file_name;//将上传的临时tmp文件移动到指定位置,并且已指定的file_name来命名文件,成功就echo输出移动后的文件位置。
若依-vue
图片上传
本地改OSS前台以及后台-附带oss
图片上传
工具类
阿丹:在二次开发若依的过程中发现若依的
图片上传
的默认的是在本地,在spring-vue版本中,如果要将平台上线那么就需要考虑这个问题,要使用fastdfs或者oss来完成代替本地的
图片上传
。本篇文章我使用了oss来完成
图片上传
以及前台的api添
加
拼接路径完成。
C#
111,129
社区成员
642,534
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章