Example #1
0
        /// <summary>
        /// 上传略缩图并添加水印
        /// </summary>
        /// <param name="files">FileUpload控件</param>
        /// <returns></returns>
        public static void ImageUpLoad(FileUpload files, Int32 Width, Int32 Height, Watermark watermark, ref string picpath)
        {
            //System.Drawing.Image xImage;
            System.Drawing.Bitmap xBitmap;
            int PhotoHeight, PhotoWidth;

            //Rectangle NewPhoto;
            System.Drawing.Imaging.ImageFormat xObject;
            picpath = "";

            if (files.FileName != "")
            {
                string ext = EKFileUpload.getExtend(files.FileName);
                ext = ext.ToLower();
                if (ext == "gif" || ext == "jpg" || ext == "jpeg" || ext == "bmp" || ext == "png")
                {
                    long FLength = files.PostedFile.ContentLength / 1024;
                    if (FLength > MS_ConfigBLL.UploadSize)
                    {
                        EKMessageBox.Show("文件上传失败!因为上传的文件超过了" + MS_ConfigBLL.UploadSize + "KB!");
                    }
                    else
                    {
                        string createfilename = EKFileUpload.createFileName();
                        string FileName       = "/" + MS_ConfigBLL.UploadPath + "/" + EKFileUpload.getMyPath() + createfilename + "." + ext;
                        string SaveFile       = EKFileUpload.FilePath + FileName;
                        //EKFileUpload.doCreatrDir(SaveFile);
                        //files.SaveAs(SaveFile);
                        picpath = FileName.Replace("//", "/");

                        switch (ext)
                        {
                        case ".gif":
                            xObject = System.Drawing.Imaging.ImageFormat.Gif;
                            break;

                        case ".bmp":
                            xObject = System.Drawing.Imaging.ImageFormat.Bmp;
                            break;

                        case ".png":
                            xObject = System.Drawing.Imaging.ImageFormat.Png;
                            break;

                        default:
                            xObject = System.Drawing.Imaging.ImageFormat.Jpeg;
                            break;
                        }

                        xBitmap = new Bitmap(files.FileContent);//------------------

                        PhotoHeight = xBitmap.Height;
                        PhotoWidth  = xBitmap.Width;
                        Int32 TempHeight = 0;
                        Int32 TempWidth  = 0;
                        if (PhotoWidth <= Width && PhotoHeight <= Height)
                        {
                            TempWidth  = PhotoWidth;
                            TempHeight = PhotoHeight;
                        }
                        else
                        {
                            if (PhotoWidth > Width)//图片宽度大于设定宽度
                            {
                                TempWidth  = Width;
                                TempHeight = Convert.ToInt32(Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth) * Convert.ToDecimal(Width));
                                if (TempHeight > Height)
                                {
                                    TempWidth  = Convert.ToInt32(Convert.ToDecimal(TempWidth) / Convert.ToDecimal(TempHeight) * Convert.ToDecimal(Height));
                                    TempHeight = Height;
                                }
                            }
                            else
                            {
                                if (PhotoHeight > Height)
                                {
                                    TempHeight = Height;
                                    TempWidth  = Convert.ToInt32(Convert.ToDecimal(PhotoWidth) / Convert.ToDecimal(PhotoHeight) * Convert.ToDecimal(Height));
                                }
                            }
                            //System.Web.HttpContext.Current.Response.Write(TempHeight.ToString()+",");
                            //System.Web.HttpContext.Current.Response.Write(TempWidth.ToString());
                            //System.Web.HttpContext.Current.Response.End();
                            if (TempHeight <= 0)
                            {
                                TempHeight = 1;
                            }
                            if (TempWidth <= 0)
                            {
                                TempWidth = 1;
                            }
                        }

                        System.Drawing.Image bitmap = new System.Drawing.Bitmap(TempWidth, TempHeight);
                        //新建一个画板
                        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
                        //设置高质量插值法
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        //设置高质量,低速度呈现平滑程度
                        g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        //清空一下画布
                        g.Clear(Color.Empty);
                        //在指定位置画图
                        g.DrawImage(xBitmap, new System.Drawing.Rectangle(0, 0, TempWidth, TempHeight), new System.Drawing.Rectangle(1, 1, xBitmap.Width - 1, xBitmap.Height - 1), System.Drawing.GraphicsUnit.Pixel);

                        //添加水印
                        if (watermark != null)
                        {
                            if (watermark.MarkType == "text")
                            {
                                //声明字体对象
                                Font cFont = null;
                                //用来测试水印文本长度得尺子
                                SizeF size = new SizeF();

                                int[] sizes = new int[] { watermark.Size, 48, 32, 16, 8, 6, 4 };

                                //探测出一个适合图片大小得字体大小,以适应水印文字大小得自适应
                                for (int i = 0; i < 7; i++)
                                {
                                    if (sizes[i] == 0)
                                    {
                                        continue;
                                    }
                                    //创建一个字体对象
                                    cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Regular);
                                    //是否加粗、倾斜
                                    if (watermark.TextBlod && watermark.TextItalic)
                                    {
                                        cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Bold | FontStyle.Italic);
                                    }
                                    else if (watermark.TextBlod)
                                    {
                                        cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Bold);
                                    }
                                    else if (watermark.TextItalic)
                                    {
                                        cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Italic);
                                    }

                                    //测量文本大小
                                    size = g.MeasureString(watermark.Text, cFont);
                                    //匹配第一个符合要求得字体大小
                                    if ((ushort)size.Width < (ushort)TempWidth)
                                    {
                                        break;
                                    }
                                }

                                Brush brush = new SolidBrush(Color.FromArgb(Convert.ToInt32(watermark.Transparency * 255), watermark.TextColor));

                                //添加水印 文字
                                g.DrawString(watermark.Text, cFont, brush, watermark.Position(watermark.Place, new SizeF(TempWidth, TempHeight), size));
                            }
                            else if (watermark.MarkType == "image")
                            {
                                //获得水印图像
                                System.Drawing.Image markImg = System.Drawing.Image.FromFile(EKFileUpload.FilePath + watermark.ImgPath);

                                //创建颜色矩阵
                                float[][] ptsArray =
                                {
                                    new float[] { 1, 0, 0,                      0, 0 },
                                    new float[] { 0, 1, 0,                      0, 0 },
                                    new float[] { 0, 0, 1,                      0, 0 },
                                    new float[] { 0, 0, 0, watermark.Transparency, 0 },                  //注意:此处为0.0f为完全透明,1.0f为完全不透明
                                    new float[] { 0, 0, 0,                      0, 1 }
                                };
                                ColorMatrix colorMatrix = new ColorMatrix(ptsArray);
                                //新建一个Image属性
                                ImageAttributes imageAttributes = new ImageAttributes();
                                //将颜色矩阵添加到属性
                                imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default,
                                                               ColorAdjustType.Default);

                                //原图过小
                                if (markImg.Width > TempWidth || markImg.Height > TempHeight)
                                {
                                    System.Drawing.Image.GetThumbnailImageAbort callb = null;
                                    //对水印图片生成缩略图,缩小到原图得1/4
                                    System.Drawing.Image new_img = markImg.GetThumbnailImage(TempWidth / 4, markImg.Height * TempWidth / 4 / markImg.Width, callb, new System.IntPtr());

                                    //添加水印图片
                                    g.DrawImage(new_img, watermark.Position(watermark.Place, new Size(TempWidth, TempHeight), new_img.Size), 0, 0, new_img.Width, new_img.Height, GraphicsUnit.Pixel, imageAttributes);
                                    //释放缩略图
                                    new_img.Dispose();
                                }
                                else
                                {
                                    //添加水印图片
                                    g.DrawImage(markImg, watermark.Position(watermark.Place, new Size(TempWidth, TempHeight), markImg.Size), 0, 0, markImg.Width, markImg.Height, GraphicsUnit.Pixel, imageAttributes);
                                }
                            }
                        }

                        // 以下代码为保存图片时,设置压缩质量
                        EncoderParameters encoderParams = new EncoderParameters();
                        long[]            quality       = new long[1];
                        quality[0] = 95;
                        EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                        encoderParams.Param[0] = encoderParam;
                        //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
                        ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                        ImageCodecInfo   jpegICI  = null;
                        for (int x = 0; x < arrayICI.Length; x++)
                        {
                            if (arrayICI[x].FormatDescription.Equals("jpeg"))
                            {
                                jpegICI = arrayICI[x];
                                //设置JPEG编码
                                break;
                            }
                        }
                        if (jpegICI != null)
                        {
                            //System.Web.HttpContext.Current.Response.Write("a");
                            EKFileUpload.doCreatrDir(SaveFile);
                            bitmap.Save(SaveFile, jpegICI, encoderParams);
                        }
                        else
                        {
                            //System.Web.HttpContext.Current.Response.Write("b");
                            EKFileUpload.doCreatrDir(SaveFile);
                            bitmap.Save(SaveFile, xObject);
                        }
                        g.Dispose();

                        bitmap.Dispose();

                        xBitmap.Dispose();
                    }
                }
                //picpath = FileName;
            }
            else
            {
                EKMessageBox.Show("文件上传失败,因为文件格式不允许上传!");
                return;
            }
        }
Example #2
0
        /// <summary>
        /// 添加水印并替换原图
        /// </summary>
        /// <param name="imagepath">图片路径</param>
        /// <param name="watermark">水印信息</param>
        /// <param name="width">生成图片的宽的上限,0为保持原图</param>
        /// <param name="height">生成图片的高的上限,0为保持原图</param>
        public static void AddWatermark(string imagepath, Watermark watermark, int Width, int Height)
        {
            System.Drawing.Imaging.ImageFormat xObject;
            string ext = EKFileUpload.getExtend(imagepath);

            ext = ext.ToLower();
            switch (ext)
            {
            case ".gif":
                xObject = System.Drawing.Imaging.ImageFormat.Gif;
                break;

            case ".bmp":
                xObject = System.Drawing.Imaging.ImageFormat.Bmp;
                break;

            case ".png":
                xObject = System.Drawing.Imaging.ImageFormat.Png;
                break;

            default:
                xObject = System.Drawing.Imaging.ImageFormat.Jpeg;
                break;
            }

            Bitmap xBitmap = new Bitmap(imagepath);

            System.Drawing.Image    bitmap;
            System.Drawing.Graphics g;
            CreateThumbnail(Width, Height, xBitmap, out bitmap, out g);
            //添加水印
            if (watermark != null)
            {
                int TempWidth  = bitmap.Width;
                int TempHeight = bitmap.Height;

                if (watermark.MarkType == "text")
                {
                    //声明字体对象
                    Font cFont = null;
                    //用来测试水印文本长度得尺子
                    SizeF size = new SizeF();

                    int[] sizes = new int[] { watermark.Size, 60, 48, 40, 36, 32, 30, 28, 24, 22, 20, 18, 16, 14, 12, 8, 6, 4 };

                    //探测出一个适合图片大小得字体大小,以适应水印文字大小得自适应
                    for (int i = 0; i < 18; i++)
                    {
                        if (sizes[i] == 0)
                        {
                            continue;
                        }
                        //创建一个字体对象
                        cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Regular);
                        //是否加粗、倾斜
                        if (watermark.TextBlod && watermark.TextItalic)
                        {
                            cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Bold | FontStyle.Italic);
                        }
                        else if (watermark.TextBlod)
                        {
                            cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Bold);
                        }
                        else if (watermark.TextItalic)
                        {
                            cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Italic);
                        }

                        //测量文本大小
                        size = g.MeasureString(watermark.Text, cFont);
                        //匹配第一个符合要求得字体大小
                        if ((ushort)size.Width < (ushort)TempWidth)
                        {
                            break;
                        }
                    }

                    Brush brush = new SolidBrush(Color.FromArgb(Convert.ToInt32(watermark.Transparency * 255), watermark.TextColor));

                    //添加水印 文字
                    g.DrawString(watermark.Text, cFont, brush, watermark.Position(watermark.Place, new SizeF(TempWidth, TempHeight), size));
                }
                else if (watermark.MarkType == "image")
                {
                    //获得水印图像
                    System.Drawing.Image markImg = System.Drawing.Image.FromFile(EKFileUpload.FilePath + watermark.ImgPath);

                    //创建颜色矩阵
                    float[][] ptsArray =
                    {
                        new float[] { 1, 0, 0,                      0, 0 },
                        new float[] { 0, 1, 0,                      0, 0 },
                        new float[] { 0, 0, 1,                      0, 0 },
                        new float[] { 0, 0, 0, watermark.Transparency, 0 },                              //注意:此处为0.0f为完全透明,1.0f为完全不透明
                        new float[] { 0, 0, 0,                      0, 1 }
                    };
                    ColorMatrix colorMatrix = new ColorMatrix(ptsArray);
                    //新建一个Image属性
                    ImageAttributes imageAttributes = new ImageAttributes();
                    //将颜色矩阵添加到属性
                    imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default,
                                                   ColorAdjustType.Default);

                    //原图过小
                    if (markImg.Width > TempWidth || markImg.Height > TempHeight)
                    {
                        System.Drawing.Image.GetThumbnailImageAbort callb = null;
                        //对水印图片生成缩略图,缩小到原图得1/4
                        System.Drawing.Image new_img = markImg.GetThumbnailImage(TempWidth / 4, markImg.Height * TempWidth / 4 / markImg.Width, callb, new System.IntPtr());

                        //添加水印图片
                        g.DrawImage(new_img, watermark.Position(watermark.Place, new Size(TempWidth, TempHeight), new_img.Size), 0, 0, new_img.Width, new_img.Height, GraphicsUnit.Pixel, imageAttributes);
                        //释放缩略图
                        new_img.Dispose();
                    }
                    else
                    {
                        //添加水印图片
                        g.DrawImage(markImg, watermark.Position(watermark.Place, new Size(TempWidth, TempHeight), markImg.Size), 0, 0, markImg.Width, markImg.Height, GraphicsUnit.Pixel, imageAttributes);
                    }
                }
            }
            //释放原图对象

            xBitmap.Dispose();
            //保存图片
            ImageSave(imagepath, bitmap, xObject);

            g.Dispose();

            bitmap.Dispose();
        }