当前位置:编程学习 > C#/ASP.NET >>

GDI+ 发生一般性错误 的分析,


在网上找了个上传图片代码,源代码是这个样子的
public bool UpLoadIMG(FileUpload UploadFile)
    {
        if (UploadFile.HasFile)//检查是否已经选择文件
        {
            string filename = UploadFile.FileName.ToLower();
            int i = filename.LastIndexOf(".");
            filename = filename.Substring(i, filename.Length - i);
            if (!(filename == ".jpg" || filename == ".jpeg" || filename == ".gif" || filename == ".png" || filename == ".bmp"))
            {
                MSG = "不受支持的类型,请重新选择!";
                return false;
            }//检查上传文件的格式是否有效
            if (UploadFile.PostedFile.ContentLength == 0 || UploadFile.PostedFile.ContentLength >= Size)
            {
                MSG = "指定的文件大小不符合要求!";
                return false;
            }//检查图片文件的大小
            //生成原图
            Stream oStream = UploadFile.PostedFile.InputStream;
            System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
            int owidth = oImage.Width; //原图宽度
            int oheight = oImage.Height; //原图高度
            if (owidth > LimitWidth || oheight > LimitHeight)
            {
                MSG = "超过允许的图片尺寸范围!";
                return false;
            }//检查是否超出规定尺寸
            if (IsRate)
            {
                //按比例计算出缩略图的宽度和高度
                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);
            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);
            Random oRandom = new Random();
            string oStringRandom = oRandom.Next(9999).ToString();//生成4位随机数字
            //格式化日期作为文件名
            DateTime oDateTime = new DateTime();
            oDateTime = DateTime.Now;
            string oStringTime = oDateTime.ToString();
            oStringTime = oStringTime.Replace("-", "");
            oStringTime = oStringTime.Replace("/", "\\");
            oStringTime = oStringTime.Replace(" ", "");
            oStringTime = oStringTime.Replace(":", "");
            OFileName = "o" + oStringTime + oStringRandom + filename;
            TFileName = "t" + oStringTime + oStringRandom + filename;
            string oSavePath = HttpContext.Current.Server.MapPath("~") + "\\" + Path + "\\";
            if (!Directory.Exists(oSavePath))
            {
                Directory.CreateDirectory(oSavePath);//在根目录下建立文件夹
            }
            //保存路径+完整文件名
            OFullName = oSavePath + OFileName;
            TFullName = oSavePath + TFileName;
            //开始保存图片至服务器
            try
            {
                switch (filename)
                {
                    case ".jpeg":
                    case ".jpg":
                        {
                            oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            break;
                        }
                    case ".gif":
                        {
                            oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Gif);
                            tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Gif);
                            break;
                        }
                    case ".png":
                        {
                            oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Png);
                            tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Png);
                            break;
                        }
                    case ".bmp":
                        {
                            oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Bmp);
                            tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Bmp);
                            break;
                        }
                }
                MSG = "图片上传成功!";
                return true;
            }
            catch (Exception ex)
            {
                MSG = ex.Message;
                return false;
            }
            finally
            {
                //释放资源
                oImage.Dispose();
                g.Dispose();
                tImage.Dispose();
            }
        }
        else
        {
            MSG = "请先选择需要上传的图片!";
            return false;
        }
    }
出现错误的地方是这里, --------------------编程问答-------------------- 一般这个问题都是权限不够 --------------------编程问答-------------------- 测试了下,是不会按时间自动创建文件夹,请问应该怎么改呢 --------------------编程问答-------------------- 还有就是想问下,你说的权限不过是说我计算机登录的用户权限不够还是什么意思呢 --------------------编程问答-------------------- 99%是文件目录不存在。或者没有访问权限。 --------------------编程问答--------------------
引用 4 楼 yuwenge 的回复:
99%是文件目录不存在。或者没有访问权限。
,是的,本来是按时间来创建文件夹的,但是它没有自动创建,请问应该怎么来改呢 --------------------编程问答--------------------
引用 5 楼 llllll_123456 的回复:
Quote: 引用 4 楼 yuwenge 的回复:

99%是文件目录不存在。或者没有访问权限。
,是的,本来是按时间来创建文件夹的,但是它没有自动创建,请问应该怎么来改呢
先判定目录是否存在,System.IO下有相应的类。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,