Ejemplo n.º 1
0
    /// <summary>
    /// 裁剪图片并保存
    /// </summary>
    public bool cropSaveAs(string fileName, string newFileName, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y)
    {
        string fileExt = SimonUtils.GetFileExt(fileName); //文件扩展名,不含“.”

        if (!IsImage(fileExt))
        {
            return(false);
        }
        string newFileDir = SimonUtils.GetMapPath(newFileName.Substring(0, newFileName.LastIndexOf(@"/") + 1));

        //检查是否有该路径,没有则创建
        if (!Directory.Exists(newFileDir))
        {
            Directory.CreateDirectory(newFileDir);
        }
        try
        {
            string fileFullPath   = SimonUtils.GetMapPath(fileName);
            string toFileFullPath = SimonUtils.GetMapPath(newFileName);
            return(SimonThumbnail.MakeThumbnailImage(fileFullPath, toFileFullPath, 180, 180, cropWidth, cropHeight, X, Y));
        }
        catch
        {
            return(false);
        }
    }
Ejemplo n.º 2
0
 /// <summary>
 /// 创建
 /// </summary>
 /// <param name="FileName">原始图片路径</param>
 public bool SetImage(string FileName)
 {
     srcFileName = SimonUtils.GetMapPath(FileName);
     try
     {
         srcImage = Image.FromFile(srcFileName);
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 3
0
    /// <summary>
    /// IP地址转换为地理位置
    /// </summary>
    /// <param name="ipstr"></param>
    /// <returns></returns>
    public static string GetIPQQWryLocator(string ipstr)
    {
        string result = string.Empty;

        try
        {
            QQWry.NET.QQWryLocator qqWry = new QQWry.NET.QQWryLocator(SimonUtils.GetMapPath("/App_Data/qqwry.dat"));
            QQWry.NET.IPLocation   ipl   = qqWry.Query(ipstr);
            result = string.Format("{0} {1} {2}", ipl.IP, ipl.Country, ipl.Local);
        }
        catch
        {
            result = "error";
        }

        return(result);
    }
Ejemplo n.º 4
0
        /// <summary>
        /// 图片水印处理类
        /// </summary>
        /// <param name="imgPath">服务器图片相对路径</param>
        /// <param name="filename">保存文件名</param>
        /// <param name="watermarkFilename">水印文件相对路径</param>
        /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 5=居中 6=右中 7=左下 8=中下  9=右下</param>
        /// <param name="quality">附加水印图片质量,0-100</param>
        /// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param>
        public static void AddImageSignPic(string imgPath, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
        {
            if (!File.Exists(SimonUtils.GetMapPath(imgPath)))
            {
                return;
            }
            byte[] _ImageBytes = File.ReadAllBytes(SimonUtils.GetMapPath(imgPath));
            Image  img         = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));

            filename = SimonUtils.GetMapPath(filename);

            if (watermarkFilename.StartsWith("/") == false)
            {
                watermarkFilename = "/" + watermarkFilename;
            }
            watermarkFilename = SimonUtils.GetMapPath(watermarkFilename);
            if (!File.Exists(watermarkFilename))
            {
                return;
            }
            Graphics g = Graphics.FromImage(img);
            //设置高质量插值法
            //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Image watermark = new Bitmap(watermarkFilename);

            if (watermark.Height >= img.Height || watermark.Width >= img.Width)
            {
                return;
            }

            ImageAttributes imageAttributes = new ImageAttributes();
            ColorMap        colorMap        = new ColorMap();

            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] remapTable = { colorMap };

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            float transparency = 0.5F;

            if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
            {
                transparency = (watermarkTransparency / 10.0F);
            }


            float[][] colorMatrixElements =
            {
                new float[] { 1.0f, 0.0f, 0.0f,         0.0f, 0.0f },
                new float[] { 0.0f, 1.0f, 0.0f,         0.0f, 0.0f },
                new float[] { 0.0f, 0.0f, 1.0f,         0.0f, 0.0f },
                new float[] { 0.0f, 0.0f, 0.0f, transparency, 0.0f },
                new float[] { 0.0f, 0.0f, 0.0f,         0.0f, 1.0f }
            };

            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            int xpos = 0;
            int ypos = 0;

            switch (watermarkStatus)
            {
            case 1:
                xpos = (int)(img.Width * (float).01);
                ypos = (int)(img.Height * (float).01);
                break;

            case 2:
                xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                ypos = (int)(img.Height * (float).01);
                break;

            case 3:
                xpos = (int)((img.Width * (float).99) - (watermark.Width));
                ypos = (int)(img.Height * (float).01);
                break;

            case 4:
                xpos = (int)(img.Width * (float).01);
                ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                break;

            case 5:
                xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                break;

            case 6:
                xpos = (int)((img.Width * (float).99) - (watermark.Width));
                ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
                break;

            case 7:
                xpos = (int)(img.Width * (float).01);
                ypos = (int)((img.Height * (float).99) - watermark.Height);
                break;

            case 8:
                xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
                ypos = (int)((img.Height * (float).99) - watermark.Height);
                break;

            case 9:
                xpos = (int)((img.Width * (float).99) - (watermark.Width));
                ypos = (int)((img.Height * (float).99) - watermark.Height);
                break;
            }

            g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo   ici    = null;

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType.IndexOf("jpeg") > -1)
                {
                    ici = codec;
                }
            }
            EncoderParameters encoderParams = new EncoderParameters();

            long[] qualityParam = new long[1];
            if (quality < 0 || quality > 100)
            {
                quality = 80;
            }

            qualityParam[0] = quality;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);

            encoderParams.Param[0] = encoderParam;

            if (ici != null)
            {
                img.Save(filename, ici, encoderParams);
            }
            else
            {
                img.Save(filename);
            }

            g.Dispose();
            img.Dispose();
            watermark.Dispose();
            imageAttributes.Dispose();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 文字水印
        /// </summary>
        /// <param name="imgPath">服务器图片相对路径</param>
        /// <param name="filename">保存文件名</param>
        /// <param name="watermarkText">水印文字</param>
        /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中  9=右下</param>
        /// <param name="quality">附加水印图片质量,0-100</param>
        /// <param name="fontname">字体</param>
        /// <param name="fontsize">字体大小</param>
        public static void AddImageSignText(string imgPath, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
        {
            byte[] _ImageBytes = File.ReadAllBytes(SimonUtils.GetMapPath(imgPath));
            Image  img         = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));

            filename = SimonUtils.GetMapPath(filename);

            Graphics g        = Graphics.FromImage(img);
            Font     drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
            SizeF    crSize;

            crSize = g.MeasureString(watermarkText, drawFont);

            float xpos = 0;
            float ypos = 0;

            switch (watermarkStatus)
            {
            case 1:
                xpos = (float)img.Width * (float).01;
                ypos = (float)img.Height * (float).01;
                break;

            case 2:
                xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                ypos = (float)img.Height * (float).01;
                break;

            case 3:
                xpos = ((float)img.Width * (float).99) - crSize.Width;
                ypos = (float)img.Height * (float).01;
                break;

            case 4:
                xpos = (float)img.Width * (float).01;
                ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                break;

            case 5:
                xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                break;

            case 6:
                xpos = ((float)img.Width * (float).99) - crSize.Width;
                ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                break;

            case 7:
                xpos = (float)img.Width * (float).01;
                ypos = ((float)img.Height * (float).99) - crSize.Height;
                break;

            case 8:
                xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                ypos = ((float)img.Height * (float).99) - crSize.Height;
                break;

            case 9:
                xpos = ((float)img.Width * (float).99) - crSize.Width;
                ypos = ((float)img.Height * (float).99) - crSize.Height;
                break;
            }

            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo   ici    = null;

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType.IndexOf("jpeg") > -1)
                {
                    ici = codec;
                }
            }
            EncoderParameters encoderParams = new EncoderParameters();

            long[] qualityParam = new long[1];
            if (quality < 0 || quality > 100)
            {
                quality = 80;
            }

            qualityParam[0] = quality;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);

            encoderParams.Param[0] = encoderParam;

            if (ici != null)
            {
                img.Save(filename, ici, encoderParams);
            }
            else
            {
                img.Save(filename);
            }

            g.Dispose();
            img.Dispose();
        }
Ejemplo n.º 6
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              = SimonUtils.GetFileExt(postedFile.FileName);                               //文件扩展名,不含“.”
            int    fileSize             = postedFile.ContentLength;                                                 //获得文件大小,以字节为单位
            string fileName             = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得原文件名
            string newFileName          = SimonUtils.GetRamCode() + "." + fileExt;                                  //随机生成新的文件名
            string newThumbnailFileName = "thumb_" + newFileName;                                                   //随机生成缩略图文件名
            string upLoadPath           = GetUpLoadPath();                                                          //上传目录相对路径
            string fullUpLoadPath       = SimonUtils.GetMapPath(upLoadPath);                                        //上传目录的物理路径
            string newFilePath          = upLoadPath + newFileName;                                                 //上传后的路径
            string newThumbnailPath     = upLoadPath + newThumbnailFileName;                                        //上传后的缩略图路径

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

            //保存文件
            postedFile.SaveAs(fullUpLoadPath + newFileName);
            //如果是图片,检查图片是否超出最大尺寸,是则裁剪
            if (IsImage(fileExt) && (Upload_PicMaxWidth > 0 || Upload_PicMaxHeight > 0))
            {
                SimonThumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName,
                                                  Upload_PicMaxWidth, Upload_PicMaxHeight);
            }
            //如果是图片,检查是否需要生成缩略图,是则生成
            if (IsImage(fileExt) && isThumbnail && Upload_PicThumbnailWidth > 0 && Upload_PicThumbnailHeight > 0)
            {
                SimonThumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName,
                                                  Upload_PicThumbnailWidth, Upload_PicThumbnailHeight, "Cut");
            }
            //如果是图片,检查是否需要打水印
            if (IsWaterMark(fileExt) && isWater)
            {
                switch (Upload_PicWaterMarkType)
                {
                case 1:
                    SimonWaterMark.AddImageSignText(newFilePath, newFilePath, Upload_PicWaterMark_Txt, 9, 63, "黑体", 16);
                    break;

                case 2:
                    SimonWaterMark.AddImageSignPic(newFilePath, newFilePath, Upload_PicWaterMark_PicUrl, 9, 63, 6);
                    break;
                }
            }
            //处理完毕,返回JOSN格式的文件信息
            //return "{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \""
            //    + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \""
            //    + newThumbnailPath + "\", \"size\": " + fileSize + ", \"ext\": \"" + fileExt + "\"}";
            return("success|" + newFilePath + "");
        }
        catch
        {
            return("上传过程中发生意外错误");
        }
    }