コード例 #1
0
        public void Img(string id, int?width, int?height)
        {
            var t = this.Request.Headers;

            if (string.IsNullOrEmpty(id))
            {
                return;
            }
            FileModel fileModel = FileModel.FirstOrDefault("where id = @id", new { id });

            if (fileModel != null)
            {
                fileModel.PathName = MTConfig.SaveBasePath + fileModel.PathName;
            }
            string imgName = fileModel.PathName;

            if (null != width && null != height)
            {
                string dir  = Path.GetDirectoryName(fileModel.PathName);
                string name = Path.GetFileName(fileModel.PathName);
                string ext  = Path.GetExtension(fileModel.PathName);
                imgName = dir + "\\" + name.Replace(ext, "") + "_" + width + "_" + height + ext;
            }
            //物理路径
            string saveFilePath = imgName;

            //不存在该图片
            if (!System.IO.File.Exists(saveFilePath))
            {
                //判断源文件是否存在
                string sourceSaveFilePath = fileModel.PathName;

                if (!System.IO.File.Exists(sourceSaveFilePath))
                {
                    return;
                }
                //生成缩略图
                ImageUtil.GetPicThumbnail(sourceSaveFilePath, saveFilePath, height.Value, width.Value);
            }
            Response.ContentType = saveFilePath.ToContentType();
            using (FileStream stream = new FileStream(saveFilePath, FileMode.Open, FileAccess.Read))
            {
                int    len    = (int)stream.Length;
                byte[] buffer = new byte[len];
                stream.Read(buffer, 0, len);
                Response.OutputStream.Write(buffer, 0, len);
            };
        }