




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
转】C#.Net 上传图片,限制图片大小,检查类型完整版1.缩小图片:public class CImageLibrary public enum ValidateImageResult OK, InvalidFileSize, InvalidImageSize /检查图片大小 public static ValidateImageResult ValidateImage(string file, int MAX_FILE_SIZE, int MAX_WIDTH, int MAX_HEIGHT) byte bs = File.ReadAllBytes(file); double size = (bs.Length / 1024); /大于50KB if (size MAX_FILE_SIZE) return ValidateImageResult.InvalidFileSize; Image img = Image.FromFile(file); if (img.Width MAX_WIDTH | img.Height MAX_HEIGHT) return ValidateImageResult.InvalidImageSize; return ValidateImageResult.OK; /按宽度比例缩小图片 public static Image GetOutputSizeImage(Image imgSource, int MAX_WIDTH) Image imgOutput = imgSource; Size size = new Size(imgSource.Width, imgSource.Height); if (imgSource.Width = 3 | imgSource.Height MAX_WIDTH | imgSource.Height MAX_WIDTH) double rate = MAX_WIDTH / (double)imgSource.Width; if (imgSource.Height * rate MAX_WIDTH) rate = MAX_WIDTH / (double)imgSource.Height; size.Width = Convert.ToInt32(imgSource.Width * rate); size.Height = Convert.ToInt32(imgSource.Height * rate); imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero); return imgOutput; /按比例缩小图片 public static Image GetOutputSizeImage(Image imgSource, Size outSize) Image imgOutput = imgSource.GetThumbnailImage(outSize.Width, outSize.Height, null, IntPtr.Zero); return imgOutput; public static byte GetImageBytes(string imageFileName) Image img = Image.FromFile(imageFileName); return GetImageBytes(img); public static byte GetImageBytes(Image img) if (img = null) return null; try System.IO.MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Jpeg); byte bs = ms.ToArray(); ms.Close(); return bs; catch return null; public static Image FromBytes(byte bs) if (bs = null) return null; try MemoryStream ms = new MemoryStream(bs); Image returnImage = Image.FromStream(ms); ms.Close(); return returnImage; catch return null; 2.自定义类:public class CFileUpload private FileUpload _fileUpload; private string _savePath; private string _LastUploadedFile = string.Empty; private bool _AutoGenFileName = false; public string LastUploadedFile get return _LastUploadedFile; private string PICTURE_FILE = .gif.png.jpeg.jpg.bmp; private string ZIP_FILE = .zip.rar; private string MUILT_MEDIA_FILE = .mpeg.mpg.fla.exe.wma; private int IMG_MAX_WIDTH = 0;/未指定宽度 private int IMG_MAX_HEIGHT = 0;/未指定高度 / / 构造器 / / A FileUpload对象 / 保存目录,不包含文件名 / 自动生成文件名 public CFileUpload(FileUpload fileUpload, string savePath, bool autoGenFileName) _savePath = savePath; _fileUpload = fileUpload; _AutoGenFileName = autoGenFileName; / / 构造器 / / A FileUpload对象 / 保存目录,不包含文件名 public CFileUpload(FileUpload fileUpload, string savePath) _savePath = savePath; _fileUpload = fileUpload; / / 上传RAR文件 / public bool UploadRARFile() return DoUpload(ZIP_FILE); / / 上传视频文件 / public bool UploadVideo() return DoUpload(MUILT_MEDIA_FILE); / / 上传图片文件 / public bool UploadImage() return DoUpload(PICTURE_FILE); public bool UploadImage(int maxWidth, int maxHeight) this.IMG_MAX_WIDTH = maxWidth; this.IMG_MAX_HEIGHT = maxHeight; return DoUpload(PICTURE_FILE); / / 上传任何支持的文件 / public bool UploadAnySupported() return DoUpload(PICTURE_FILE + ZIP_FILE + MUILT_MEDIA_FILE); / / 生成新的文件名 / private string GetNewFileName(string folder, string fileName) /_AutoGenFileName=true 或者文件名长度50,自动生成32位GUID文件名 if (_AutoGenFileName | StrUtils.GetStringLength(fileName) = 50) string ext = System.IO.Path.GetExtension(fileName); string newfile = Guid.NewGuid().ToString().Replace(-, ) + ext; return folder + newfile; else if (System.IO.File.Exists(folder + fileName) string ext = System.IO.Path.GetExtension(fileName); string filebody = fileName.Replace(ext, ); int x = 1; while (true) /如果文件存在,生成尾部带(x)的文件 string newfile = folder + filebody + ( + x.ToString() + ) + ext; if (!System.IO.File.Exists(newfile) return folder + filebody + ( + x.ToString() + ) + ext; else x+; else return folder + fileName; / / 最大支持小于1MB的文件。 / private bool AllowMaxSize(int fileLength) int MAX_SIZE_UPLOAD = 1024;/最大支持上传小于1MB的文件。 double kb = fileLength / 1024; return (int)kb 0; /检查上传文件大小 fileOK = fileOK & AllowMaxSize(_fileUpload.FileBytes.Length); if (!fileOK) return false; /如检查不通过,退出 try / 文件另存在服务器指定目录下 string savefile = GetNewFileName(_savePath, _fileUpload.FileName); if (IsUploadImage(fileExtension)/保存图片 System.Drawing.Image output = CImageLibrary.FromBytes(_fileUpload.FileBytes); / 检查图片宽度/高度/大小 if (this.IMG_MAX_WIDTH != 0 & output.Width this.IMG_MAX_WIDTH) output = CImageLibrary.GetOutputSizeImage(output, this.IMG_MAX_WIDTH); Bitmap bmp = new Bitmap(output); bmp.Save(savefile, output.RawFormat); else/其它任何文件 _fileUpload.PostedFile.SaveAs(savefile); _LastUploadedFile = savefile; return true; catch return false; private bool IsUploadImage(string fileExtension) bool isImage = PICTURE_FILE.IndexOf(fileExtension) 0; return isImage; 3.事件:protected void btnUploadImg_Click(object sender, EventArgs e) try if (!FileUploadImg.HasFile) return; string virtualFilePath = / + CAppConfiguration.Current.BbsUploadFilePath; string savePath = this.Server.MapPath(virtualFilePath); /保存文件物理路径 /调用CFileUpload类 CFileUpload upload = new CFileUpload(FileUploadImg, savePath, false); /上传图片文件 bool ret = upload.UploadImage(); if (ret) string filename = System.IO.Path.GetFileName(upload.LastUploadedFile); string imgHTML = ; this.hfLastUploadImage.Value = filename; string root = CGlobals.GetHttpRoot(this); imgHTML = string.Format(imgHTML, root + / + CAppConfiguration.Current.BbsUploadFilePath + filename); FreeTextBox
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 安阳教资考试题库及答案
- 押题宝典高校教师资格证之《高等教育法规》通关考试题库附参考答案详解(典型题)
- 高血压考试试题及答案
- 组织结构调整过程中的员工培训与发展试题及答案
- 2025年电商平台数据分析与社交电商模式创新报告
- 2025年直播电商行业主播品牌合作模式优化研究分析报告
- 2025年工业互联网平台光通信技术升级对光纤光缆制造设备行业的影响报告
- 2025年海上风电场海上风电场运维成本分析与控制策略报告
- 2025至2030年中国直接接触食品材料市场深度调研分析及投资前景研究预测报告
- 解析卷-人教版7年级数学上册期中试题及参考答案详解(综合题)
- 环境保护与水土保持监理实施细则
- 顾问项目进驻与退出管理办法
- 2025年部编版小学二年级语文上册全册教案
- 国有企业采购管理办法
- DBJ50-T-330-2025-建筑楼地面隔声保温工程应用技术标准
- 人教版2024-2025学年九年级英语下册教学计划(及进度表)
- 《磁感应强度-磁通量》课件
- 高中主题班会 《铭记历史强国有我》课件-高一上学期爱国主义教育主题班会
- 2025至2030年土壤检测仪器项目投资价值分析报告
- 标准预防及安全注射
- 儿童体适能教学 课件 (20期)
评论
0/150
提交评论