Asp.net给图片加上水印效果

时间:2023-01-30 20:14:35

    private void Btn_Upload_Click(object sender, System.EventArgs e)
        {
            if(UploadFile.PostedFile.FileName.Trim()!="")
            {
                //上传文件
                string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
                string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                string path = Server.MapPath(".") + "/UploadFile/" + fileName + extension;
                UploadFile.PostedFile.SaveAs(path);

                //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
                System.Drawing.Image image = System.Drawing.Image.FromFile(path);
                Graphics g = Graphics.FromImage(image);
                g.DrawImage(image, 0, 0, image.Width, image.Height);
                Font f = new Font("Verdana", 32);
                Brush b = new SolidBrush(Color.White);
                string addText = AddText.Value.Trim();
                g.DrawString(addText, f, b, 10, 10);
                g.Dispose();

                //加图片水印
                System.Drawing.Image image = System.Drawing.Image.FromFile(path);
                System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/Alex.gif");
                Graphics g = Graphics.FromImage(image);
                g.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                g.Dispose();

                //保存加水印过后的图片,删除原始图片
                string newPath = Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;
                image.Save(newPath);
                image.Dispose();
                if(File.Exists(path))
                {
                    File.Delete(path);
                }

                Response.Redirect(newPath);
            }
        }


//生成缩略图#region //生成缩略图
        /**//// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="str_originalImagePath">源图路径(物理路径)</param>
        /// <param name="str_ThumbnailPath">缩略图路径(物理路径)</param>
        /// <param name="i_width">缩略图宽度</param>
        /// <param name="i_height">缩略图高度</param>
        /// <param name="str_mode">生成缩略图的模式</param>
        public static void MakeThumbnail(string str_originalImagePath,string str_ThumbnailPath,int i_width,int i_height,string str_mode)
        {
            System.Drawing.Image img_OriginalImage = Image.FromFile(str_originalImagePath);
            int i_ToWidth = i_width;
            int i_ToHeight = i_height;

            int x = 0;
            int y = 0;

            int i_OriginalWidth = img_OriginalImage.Width;
            int i_OriginalHeight = img_OriginalImage.Height;

            switch(str_mode)
            {
                case "HW": //按照指定的高度和宽度进行缩放(可能变形)
                    break;
                case "W" : //指定宽度,高度按照比例缩放
                    i_ToHeight = img_OriginalImage.Height*i_width/img_OriginalImage.Width;
                    break;
                case "H" : //指定高度,宽度按照比例缩放
                    i_ToWidth = img_OriginalImage.Width*i_Height/img_OriginalImage.Height;
                    break;
                case "CUT" : //按照指定的高度和宽度剪裁(不变形)
                    if ((double)img_OriginalImage.Width/(double)img_OriginalImage.Height > (double)i_ToWidth/(double)i_ToHeight)
                    {
                        i_OriginalHeight = img_OriginalImage.Height;
                        i_OriginalWidth = img_OriginalImage.Height*i_ToWidth/i_ToHeight;
                        y = 0;
                        x = (img_OriginalImage.Width-i_OriginalWidth)/2;
                    }
                    else
                    {
                        i_OriginalWidth = img_OriginalImage.Width;
                        i_OriginalHeight = img_OriginalImage.Width*i_Height/i_ToWidth;
                        x = 0;
                        y = (img_OriginalImage.Height-i_OriginalHeight)/2;
                    }
                    break;
                default:
                    break;
            }

            //新建一个BMP图片
            Image img_BitMap = new System.Drawing.Bitmap(i_ToWidth,i_ToHeight);

            //新建一个画板
            System.Drawing.Graphics gp = new System.Drawing.Graphics.FromImage(img_BitMap);

            //设置高质量插值法
            gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量、低速度呈现平滑程度
            gp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            gp.Clear(Color.Transparent);

            //指定位置并按大小绘制原图片的指定部分
            gp.DrawImage(img_OriginalImage,new Rectangle(0,0,i_ToWidth,i_ToHeight),new Rectangle(x,y,i_OrignalWidth,i_OriginalHeight),GraphicsUnit.Pixel);

            try
            {
                //以JPG格式保存图片
                img_BitMap.Save(str_ThumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch(Exception e)
            {
                throw e;
            }
            finally
            {
                img_OriginalImage.Dispose();
                img_BitMap.Dispose();
                gp.Dispose();
            }

        }
        #endregion