Exemplo n.º 1
0
        /// <summary>
        /// 附件上传
        /// </summary>
        private void Upload()
        {
            byte   attribute        = (byte)Utils.GetFormInt("hidden_attr");  //附件属性
            string hasWaterMark     = Utils.GetFormString("chHasWaterMark");  //原图是否水印
            string hasAbbrImage1    = Utils.GetFormString("chHasAbbrImage1"); //是否生成缩略图
            string hasWaterMark1    = Utils.GetFormString("chHasWaterMark1"); //缩略图是否水印
            int    abbrImageWidth1  = Utils.GetFormInt("abbrImageWidth1");    //缩略图宽
            int    abbrImageHeight1 = Utils.GetFormInt("abbrImageHeight1");   //缩略图高
            string filepathshort    = sysConfig.Attachments.Path.Replace("\\\\", "\\") + "\\"
                                      + DateTime.Now.ToString(sysConfig.Attachments.Directory == "" ? "yyyyMM" : sysConfig.Attachments.Directory) + "\\";
            string filepath = Utils.GetRootPath() + filepathshort; //附件存放路径

            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }

            string errorMsg      = string.Empty; //错误信息
            int    returnVal     = 1;            //返回值。1:成功,202:无效上传文件,203:你没有权限,204:未知错误
            string returnImgName = string.Empty; //返回图片名称
            string abbName       = string.Empty; //缩略图名称

            HttpFileCollection uploadedFiles = Request.Files;

            for (int i = 0; i < uploadedFiles.Count; i++)
            {
                string fileDisplayName = DTCMS.Common.Utils.GetFormString("File" + (i + 1) + "Name"); //附件名称
                string fileInfo        = DTCMS.Common.Utils.GetFormString("File" + (i + 1) + "Info"); //附件描述

                HttpPostedFile userPostedFile = uploadedFiles[i];
                string         fileName       = System.IO.Path.GetFileName(userPostedFile.FileName);
                if (fileName != "")
                {
                    #region 验证附件格式是否正确
                    if (!AttachmentFormat(fileName, attribute))
                    {
                        if (errorMsg != string.Empty)
                        {
                            errorMsg += ",";
                        }
                        errorMsg += fileName;
                        continue;
                    }
                    #endregion 验证附件格式是否正确

                    try
                    {
                        int fileContentLen = userPostedFile.ContentLength;
                        if (fileContentLen > 0)
                        {
                            #region 附件上传

                            returnImgName = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                            abbName       = returnImgName.Substring(0, returnImgName.LastIndexOf('.')) + "_abbr" + fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                            userPostedFile.SaveAs(filepath + returnImgName);  //附件上传
                            #endregion 附件上传

                            #region 是否图片
                            if (attribute == (int)EAttachmentAttribute.Photo)
                            {
                                if (hasAbbrImage1.Trim().ToLower() == "true")
                                {
                                    #region 生成缩略图
                                    EWaterImageType mode = EWaterImageType.W;
                                    if (abbrImageHeight1 > 0 && abbrImageWidth1 <= 0)
                                    {
                                        mode = EWaterImageType.H;
                                    }
                                    else if (abbrImageWidth1 > 0 && abbrImageHeight1 <= 0)
                                    {
                                        mode = EWaterImageType.W;
                                    }
                                    else if ((abbrImageHeight1 > 0) && (abbrImageWidth1 > 0))
                                    {
                                        mode = EWaterImageType.CUT;
                                    }
                                    else
                                    {
                                        mode = EWaterImageType.NO;
                                    }
                                    if (mode == EWaterImageType.NO)
                                    {
                                        abbrImageWidth1  = 500;
                                        abbrImageHeight1 = 0;
                                        mode             = EWaterImageType.W;
                                    }
                                    string abbPath = filepath + abbName;
                                    Common.WaterImage.MakeThumbnail(filepath + returnImgName, abbPath
                                                                    , abbrImageWidth1, abbrImageHeight1, mode);
                                    #endregion 生成缩略图

                                    #region 缩略图水印
                                    if (hasWaterMark1.Trim().ToLower() == "true")
                                    {
                                        WaterImage(abbPath, abbPath);
                                    }
                                    #endregion 缩略图水印
                                }

                                #region 原图水印

                                if (attribute == (int)EAttachmentAttribute.Photo)
                                {
                                    if (hasWaterMark.Trim().ToLower() == "true")
                                    {
                                        WaterImage(filepath + returnImgName, filepath + returnImgName);
                                    }
                                }
                                #endregion 原图水印
                            }
                            #endregion 是否图片

                            #region 保存数据
                            AttachMent modAttachMent = new AttachMent();
                            modAttachMent.Attribute      = attribute;
                            modAttachMent.DisplayName    = fileDisplayName;
                            modAttachMent.AttachMentPath = "/" + filepathshort.Replace("\\", "/") + returnImgName;
                            modAttachMent.AttachMentSize = fileContentLen;
                            if (hasAbbrImage1.Trim().ToLower() == "true")
                            {
                                modAttachMent.AbbrPhotoPath = "/" + filepathshort.Replace("\\", "/") + abbName;
                            }
                            else
                            {
                                modAttachMent.AbbrPhotoPath = "";
                            }
                            modAttachMent.PubLisher        = "";
                            modAttachMent.AddDate          = DateTime.Now;
                            modAttachMent.PhotoDescription = fileInfo;
                            bllAttachment.Add(modAttachMent);
                            #endregion 保存数据
                        }
                        else
                        {
                            if (errorMsg != string.Empty)
                            {
                                errorMsg += ",";
                            }
                            errorMsg += fileName;
                        }
                    }
                    catch
                    {
                        returnVal = 204;    //未知错误
                        errorMsg  = "";
                    }
                }

                if (errorMsg != "")
                {
                    returnVal = 202;    //无效上传文件
                }
            }
            string returnImgPath = "";
            if (hasAbbrImage1.Trim().ToLower() == "true")
            {
                if (abbName != string.Empty && abbName != null)
                {
                    returnImgPath = "/" + filepathshort.Replace("\\", "/") + abbName;
                }
            }
            else
            {
                if (returnImgName != string.Empty && returnImgName != null)
                {
                    returnImgPath = "/" + filepathshort.Replace("\\", "/") + returnImgName;
                }
            }

            Response.Redirect("~/admin/attachment/emptyPage.html?returnVal=" + returnVal + "&errorMsg=" + Server.UrlEncode(errorMsg) + "&returnImgPath=" + Server.UrlEncode(returnImgPath));
        }
Exemplo n.º 2
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, EWaterImageType mode)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

            int imgWidth = width <= 0?originalImage.Width:width;

            imgWidth = width > originalImage.Width ? originalImage.Width : width;
            int imgHeight = height < 0?originalImage.Height:height;

            imgHeight = height > originalImage.Height ? originalImage.Height : height;
            int x  = 0;
            int y  = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

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

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

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

            case EWaterImageType.CUT:    //指定高宽裁减(不变形)
                if ((double)originalImage.Width / (double)originalImage.Height > (double)imgWidth / (double)imgHeight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * imgWidth / imgHeight;
                    y  = 0;
                    x  = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * imgHeight / imgWidth;
                    x  = 0;
                    y  = (originalImage.Height - oh) / 2;
                }
                break;

            default:
                break;
            }


            Dictionary <string, string> dicImg = SetImgType();//缩略图图片格式

            System.Drawing.Bitmap   bitMap   = new System.Drawing.Bitmap(imgWidth, imgHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitMap);                //新建一个画板
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            graphics.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量查值法
            graphics.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;            //设置高质量,低速度呈现平滑程度
            graphics.Clear(System.Drawing.Color.Transparent);                                            //清空画布并以透明背景色填充

            //在指定位置并且按指定大小绘制原图片的指定部分
            graphics.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight), new System.Drawing.Rectangle(x, y, ow, oh),
                               System.Drawing.GraphicsUnit.Pixel);

            //设置 原图片 对象的 EncoderParameters 对象
            System.Drawing.Imaging.EncoderParameters parms = new System.Drawing.Imaging.EncoderParameters(1);
            parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long)100));
            string extension = Path.GetExtension(originalImagePath).ToLower();

            System.Drawing.Imaging.ImageCodecInfo imgCodeInfo = GetImageCodeInfo(dicImg[extension].ToString());

            string directoryPath = Path.GetDirectoryName(thumbnailPath);

            //看是否存在目录 不存在则创建目录
            if (!System.IO.File.Exists(directoryPath))
            {
                System.IO.Directory.CreateDirectory(directoryPath);
            }
            try
            {
                bitMap.Save(thumbnailPath, imgCodeInfo, parms);
            }
            catch
            {
            }
            finally
            {
                originalImage.Dispose();
                bitMap.Dispose();
                graphics.Dispose();
                parms.Dispose();
            }
        }