Exemplo n.º 1
0
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalImagePath">源图路径(相对路径)</param>
        /// <param name="thumbnailPath">缩略图路径(相对路径)</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式</param>
        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(Utils.GetMapPath(originalImagePath));
            int towidth  = width;
            int toheight = height;

            int x  = 0;
            int y  = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (mode)
            {
            case "HW":    //指定高宽缩放(可能变形)
                break;

            case "W":    //指定宽,高按比例
                toheight = originalImage.Height * width / originalImage.Width;
                break;

            case "H":    //指定高,宽按比例
                towidth = originalImage.Width * height / originalImage.Height;
                break;

            case "Cut":    //指定高宽裁减(不变形)
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y  = 0;
                    x  = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * height / towidth;
                    x  = 0;
                    y  = (originalImage.Height - oh) / 2;
                }
                break;

            default:
                break;
            }

            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
            //新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);
            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                        new System.Drawing.Rectangle(x, y, ow, oh),
                        System.Drawing.GraphicsUnit.Pixel);

            try
            {
                //保存缩略图
                bitmap.Save(Utils.GetMapPath(thumbnailPath));
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 文件上传方法
        /// </summary>
        /// <param name="postedFile">文件流</param>
        /// <param name="isThumbnail">是否生成缩略图</param>
        /// <param name="isWater">是否打水印</param>
        /// <returns>上传后文件信息</returns>
        public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater)
        {
            try
            {
                string fileExt              = Utils.GetFileExt(postedFile.FileName);                                    //文件扩展名,不含“.”
                int    fileSize             = postedFile.ContentLength;                                                 //获得文件大小,以字节为单位
                string fileName             = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得原文件名
                string newFileName          = Utils.GetRamCode() + "." + fileExt;                                       //随机生成新的文件名
                string newThumbnailFileName = newFileName;                                                              //随机生成缩略图文件名
                string upLoadPath           = GetUpLoadPath();                                                          //上传目录相对路径
                string fullUpLoadPath       = Utils.GetMapPath(upLoadPath);                                             //上传目录的物理路径
                string newFilePath          = upLoadPath + newFileName;                                                 //上传后的路径
                string newThumbnailPath     = upLoadPath + newThumbnailFileName;                                        //上传后的缩略图路径

                //检查文件扩展名是否合法
                //if (!CheckFileExt(fileExt))
                //{
                //    return "{\"status\": 0, \"msg\": \"不允许上传" + fileExt + "类型的文件!\"}";
                //}
                //检查文件大小是否合法
                if (!CheckFileSize(fileExt, fileSize))
                {
                    return("{\"status\": 0, \"msg\": \"文件超过限制的大小啦!\"}");
                }
                //检查上传的物理路径是否存在,不存在则创建
                if (!Directory.Exists(fullUpLoadPath))
                {
                    Directory.CreateDirectory(fullUpLoadPath);
                }

                //保存文件
                postedFile.SaveAs(fullUpLoadPath + newThumbnailFileName);
                //如果是图片,检查图片是否超出最大尺寸,是则裁剪
                //if (IsImage(fileExt) && (this.siteConfig.imgmaxheight > 0 || this.siteConfig.imgmaxwidth > 0))
                //{
                //    Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName,
                //        this.siteConfig.imgmaxwidth, this.siteConfig.imgmaxheight);
                //}
                ////如果是图片,检查是否需要生成缩略图,是则生成
                //if (IsImage(fileExt) && isThumbnail && this.siteConfig.thumbnailwidth > 0 && this.siteConfig.thumbnailheight > 0)
                //{
                //    Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName,
                //        this.siteConfig.thumbnailwidth, this.siteConfig.thumbnailheight, "Cut");
                //}
                ////如果是图片,检查是否需要打水印
                //if (IsWaterMark(fileExt) && isWater)
                //{
                //    switch (this.siteConfig.watermarktype)
                //    {
                //        case 1:
                //            WaterMark.AddImageSignText(newFilePath, newFilePath,
                //                this.siteConfig.watermarktext, this.siteConfig.watermarkposition,
                //                this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize);
                //            break;
                //        case 2:
                //            WaterMark.AddImageSignPic(newFilePath, newFilePath,
                //                this.siteConfig.watermarkpic, this.siteConfig.watermarkposition,
                //                this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency);
                //            break;
                //    }
                //}
                //处理完毕,返回JOSN格式的文件信息
                return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \""
                       + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \""
                       + newThumbnailPath + "\", \"size\": " + fileSize + ", \"ext\": \"" + fileExt + "\"}");
            }
            catch
            {
                return("{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}");
            }
        }